Browse Source

Note deserialization failure path

Mark Rousskov 5 years ago
parent
commit
d6bd972a5b
3 changed files with 20 additions and 1 deletions
  1. 10 0
      Cargo.lock
  2. 1 0
      Cargo.toml
  3. 9 1
      src/lib.rs

+ 10 - 0
Cargo.lock

@@ -1103,6 +1103,14 @@ dependencies = [
  "serde 1.0.104 (registry+https://github.com/rust-lang/crates.io-index)",
 ]
 
+[[package]]
+name = "serde_path_to_error"
+version = "0.1.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "serde 1.0.104 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
 [[package]]
 name = "serde_urlencoded"
 version = "0.6.1"
@@ -1327,6 +1335,7 @@ dependencies = [
  "rust_team_data 1.0.0 (git+https://github.com/rust-lang/team)",
  "serde 1.0.104 (registry+https://github.com/rust-lang/crates.io-index)",
  "serde_json 1.0.48 (registry+https://github.com/rust-lang/crates.io-index)",
+ "serde_path_to_error 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
  "tokio 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)",
  "tokio-postgres 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)",
  "toml 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -1711,6 +1720,7 @@ dependencies = [
 "checksum serde 1.0.104 (registry+https://github.com/rust-lang/crates.io-index)" = "414115f25f818d7dfccec8ee535d76949ae78584fc4f79a6f45a904bf8ab4449"
 "checksum serde_derive 1.0.104 (registry+https://github.com/rust-lang/crates.io-index)" = "128f9e303a5a29922045a830221b8f78ec74a5f544944f3d5984f8ec3895ef64"
 "checksum serde_json 1.0.48 (registry+https://github.com/rust-lang/crates.io-index)" = "9371ade75d4c2d6cb154141b9752cf3781ec9c05e0e5cf35060e1e70ee7b9c25"
+"checksum serde_path_to_error 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "359b895005d818163c78a24d272cc98567cce80c2461cf73f513da1d296c0b62"
 "checksum serde_urlencoded 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)" = "9ec5d77e2d4c73717816afac02670d5c4f534ea95ed430442cad02e7a6e32c97"
 "checksum sha2 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)" = "27044adfd2e1f077f649f59deb9490d3941d674002f7d062870a60ebe9bd47a0"
 "checksum siphasher 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "83da420ee8d1a89e640d0948c646c1c088758d3a3c538f943bfa97bdac17929d"

+ 1 - 0
Cargo.toml

@@ -31,6 +31,7 @@ chrono = { version = "0.4", features = ["serde"] }
 tokio-postgres = { version = "0.5", features = ["with-chrono-0_4"] }
 postgres-native-tls = "0.3"
 native-tls = "0.2"
+serde_path_to_error = "0.1.2"
 
 [dependencies.serde]
 version = "1"

+ 9 - 1
src/lib.rs

@@ -62,7 +62,15 @@ impl From<anyhow::Error> for WebhookError {
 }
 
 pub fn deserialize_payload<T: serde::de::DeserializeOwned>(v: &str) -> anyhow::Result<T> {
-    Ok(serde_json::from_str(&v).with_context(|| format!("input: {:?}", v))?)
+    let mut deserializer = serde_json::Deserializer::from_str(&v);
+    let res: Result<T, _> = serde_path_to_error::deserialize(&mut deserializer);
+    match res {
+        Ok(r) => Ok(r),
+        Err(e) => {
+            let ctx = format!("at {:?}", e.path());
+            Err(e.into_inner()).context(ctx)
+        }
+    }
 }
 
 pub async fn webhook(