Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use serde_path_to_error for better parsing errors #13

Merged
merged 1 commit into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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))
jonas-w marked this conversation as resolved.
Show resolved Hide resolved
}
}
}

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