Skip to content

Commit

Permalink
Add a field to store the HTTP status in Errors.
Browse files Browse the repository at this point in the history
Having the status code chucked in the string message made it hard to
machine-read it. The only uses of the "message" field where simply
copying the code in there. I moved that to the display impl.
  • Loading branch information
jbtrystram committed May 24, 2022
1 parent 89a31dc commit 45add3d
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ tracing = "0.1"
url = "2"

http = { version = "0.2", optional = true }
http-serde = { version = "1.1.0", optional = true }
nom = { version = "6", optional = true }
openid = { version = "0.9.1", optional = true }
opentelemetry = { version = "0.17", optional = true }
Expand All @@ -37,10 +38,10 @@ reqwest = { version = "0.11", features = ["json"], optional = true }
reqwest-wasm-ext = { git = "https://github.com/ctron/reqwest-wasm-ext.git", optional = true }

[features]
default = ["reqwest", "openid", "telemetry", "nom"]
default = ["reqwest", "openid", "telemetry", "nom", "http-serde"]
telemetry = ["opentelemetry", "opentelemetry-http", "http"]
# alternate default target for wasm
wasm = ["reqwest", "reqwest-wasm-ext", "nom"]
wasm = ["reqwest", "reqwest-wasm-ext", "nom", "http-serde"]
reqwest = ["dep:reqwest", "reqwest-wasm-ext"]

[dev-dependencies]
Expand Down
4 changes: 4 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! Error and error information.
use reqwest::StatusCode;
use serde::{Deserialize, Serialize};
use std::fmt;
use url::ParseError;
Expand All @@ -9,6 +10,9 @@ use url::ParseError;
pub struct ErrorInformation {
/// A machine processable error type.
pub error: String,
/// A machine processable HTTP Status code.
#[serde(with = "http_serde::status_code")]
pub status: StatusCode,
/// A human readable error message.
#[serde(default)]
pub message: String,
Expand Down
8 changes: 7 additions & 1 deletion src/util/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,15 +194,21 @@ pub trait Client {
Ok(json) => ErrorInformation {
error: json,
message: format!("HTTP {}", code),
status: code,
},
Err(_) => ErrorInformation {
error: String::default(),
message: format!("HTTP error {}", code),
status: code,
},
};
Err(ClientError::Service(error))
}
code => Err(ClientError::Request(format!("Unexpected code {:?}", code))),
code => Err(ClientError::Service(ErrorInformation {
error: String::default(),
message: format!("Unexpected HTTP code {:?}", code),
status: code,
})),
}
}
}

0 comments on commit 45add3d

Please sign in to comment.