Skip to content

Commit

Permalink
feat: use serde_path_to_error for better parsing errors
Browse files Browse the repository at this point in the history
Co-authored-by: Martin Kröning <[email protected]>
  • Loading branch information
jonas-w and mkroening committed Nov 14, 2024
1 parent d7d9bb5 commit 8024e49
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 9 deletions.
11 changes: 11 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions edu-ws/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ html-escape = "0.2"
reqwest = { version = "0.12", default-features = false, features = ["json"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
serde_path_to_error = "0.1"
serde_repr = "0.1"
serde_with = "3"
thiserror = "2"
Expand Down
28 changes: 19 additions & 9 deletions edu-ws/src/ws.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use url::Url;

use crate::{
response::{content::Section, course::Course, info::Info},
serde::{NumBool, UntaggedResultHelper},
serde::NumBool,
token::Token,
};

Expand All @@ -35,7 +35,7 @@ pub enum RequestError {
#[error(transparent)]
HttpError(#[from] reqwest::Error),
#[error(transparent)]
Decode(#[from] serde_json::Error),
Decode(#[from] serde_path_to_error::Error<serde_json::Error>),
}

impl RequestError {
Expand Down Expand Up @@ -121,13 +121,23 @@ impl Client {
.text()
.await
.unwrap();

let ret = serde_json::from_str::<'_, UntaggedResultHelper<T, Error>>(&response)
.inspect(|_| debug!(response))
.inspect_err(|err| error!(?err, response, "Could not deserialize response"))?
.0?;

Ok(ret)
debug!(response);

let de = &mut serde_json::Deserializer::from_str(&response);
let ok_err = match serde_path_to_error::deserialize(de) {
Ok(value) => return Ok(value),
Err(err) => err,
};

let de = &mut serde_json::Deserializer::from_str(&response);
match serde_path_to_error::deserialize(de) {
Ok(value) => Err(RequestError::WsError(value)),
Err(err) => {
error!(%ok_err, "Could not deserialize response");
error!(%err, "Could not deserialize error");
Err(RequestError::Decode(ok_err))
}
}
}

pub async fn get_info(&self) -> Result<Info> {
Expand Down

0 comments on commit 8024e49

Please sign in to comment.