Skip to content
This repository has been archived by the owner on Dec 22, 2023. It is now read-only.

Commit

Permalink
🚧 Prepare the new WG error enum
Browse files Browse the repository at this point in the history
  • Loading branch information
eigenein committed Jul 3, 2023
1 parent d02e79f commit 7e3266c
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 1 deletion.
22 changes: 22 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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ lz4_flex = { version = "0.11.1", default-features = false, features = ["std", "s
maud = { version = "0.25.0", features = ["axum"] }
moka = { version = "0.11.2", default-features = false, features = ["future", "atomic64"] }
mongodb = { version = "2.6.0", features = ["tracing", "bson-uuid-1", "bson-chrono-0_4"] }
monostate = "0.1.6"
phf = { version = "0.11.2", features = ["macros"] }
quick-xml = { version = "0.29.0", features = ["serialize"] }
rayon = "1.7.0"
Expand Down
2 changes: 1 addition & 1 deletion src/prelude.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pub use anyhow::{anyhow, ensure, Context, Error};
pub use anyhow::{anyhow, bail, ensure, Context, Error};
pub use chrono::{TimeZone, Utc};
pub use tracing::{debug, error, info, instrument, trace, warn};

Expand Down
35 changes: 35 additions & 0 deletions src/wg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,19 @@ pub enum WgResponse<D> {
Err { error: WgError },
}

#[derive(Deserialize, Debug, thiserror::Error)]
#[serde(untagged)]
pub enum NewWgError {
#[error("invalid token")]
InvalidToken {
code: monostate::MustBe!(407),
message: monostate::MustBe!("INVALID_ACCESS_TOKEN"),
},

#[error("Wargaming.net API error `{code}/{message}`")]
Other { code: u16, message: String },
}

impl Wg {
pub fn new(application_id: &str) -> Result<Self> {
let client = ClientBuilder::new()
Expand Down Expand Up @@ -165,4 +178,26 @@ mod tests {
)?;
Ok(())
}

#[test]
fn parse_invalid_token_error_ok() -> Result {
// language=json
let error: NewWgError =
serde_json::from_str(r#"{"code": 407, "message": "INVALID_ACCESS_TOKEN"}"#)?;
match error {
NewWgError::InvalidToken { .. } => Ok(()),
_ => bail!(error),
}
}

#[test]
fn parse_other_error_ok() -> Result {
// language=json
let error: NewWgError =
serde_json::from_str(r#"{"code": 418, "message": "I_AM_A_TEAPOT"}"#)?;
match error {
NewWgError::Other { code: 418, .. } => Ok(()),
_ => bail!(error),
}
}
}

0 comments on commit 7e3266c

Please sign in to comment.