Skip to content

Commit c6a0655

Browse files
committed
Improve error reporting #3
1 parent 754a1bf commit c6a0655

File tree

3 files changed

+7
-4
lines changed

3 files changed

+7
-4
lines changed

sqlsonnet/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ serde_with = "3.8.1"
2020
thiserror.workspace = true
2121
tracing-subscriber.workspace = true
2222
sqlsonnet-macros.workspace = true
23+
serde_path_to_error = "0.1.16"
2324

2425
[features]
2526
fancy = ["miette/fancy", "miette/syntect-highlighter"]

sqlsonnet/src/error.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,11 @@ pub struct JsonError {
5858
pub span: miette::SourceOffset,
5959
}
6060
impl JsonError {
61-
pub fn from(json: &str, e: serde_json::Error) -> Self {
61+
pub fn from(json: &str, e: serde_path_to_error::Error<serde_json::Error>) -> Self {
62+
let orig = e.inner();
6263
Self {
63-
reason: e.to_string(),
64-
span: miette::SourceOffset::from_location(json, e.line(), e.column()),
64+
reason: format!("{}; path `{}`", orig, e.path()),
65+
span: miette::SourceOffset::from_location(json, orig.line(), orig.column()),
6566
src: miette::NamedSource::new("source.json", json.into()),
6667
}
6768
}

sqlsonnet/src/lib.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ macro_rules! impl_conversions {
3636
}
3737
/// Convert from JSON.
3838
pub fn from_json(json: &str) -> Result<Self, Error> {
39-
Ok(serde_json::from_str(&json)
39+
let mut deserializer = serde_json::Deserializer::from_str(&json);
40+
Ok(serde_path_to_error::deserialize(&mut deserializer)
4041
.map_err(|e| crate::error::JsonError::from(&json, e))?)
4142
}
4243
/// Convert from Jsonnet.

0 commit comments

Comments
 (0)