diff --git a/Cargo.toml b/Cargo.toml index 0e4dc7c..3c28ea5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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 } @@ -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] diff --git a/src/error.rs b/src/error.rs index 94db0cc..1c60dde 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,5 +1,6 @@ //! Error and error information. +use reqwest::StatusCode; use serde::{Deserialize, Serialize}; use std::fmt; use url::ParseError; @@ -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, diff --git a/src/util/client/mod.rs b/src/util/client/mod.rs index a3955ba..e2b3be7 100644 --- a/src/util/client/mod.rs +++ b/src/util/client/mod.rs @@ -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, + })), } } }