Skip to content

Commit

Permalink
Improve Display output of RequestTokenError::ServerResponse
Browse files Browse the repository at this point in the history
BREAKING CHANGE: This change requires types implementing the `ErrorResponse`
trait to also implement the `Display` trait.
  • Loading branch information
ramosbugs committed Feb 21, 2024
1 parent d675e81 commit 3d69361
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 14 deletions.
15 changes: 10 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3028,11 +3028,15 @@ where
///
/// Server Error Response
///
/// See [Section 5.2](https://datatracker.ietf.org/doc/html/rfc6749#section-5.2) of RFC 6749.
/// This trait exists separately from the `StandardErrorResponse` struct
/// to support customization by clients, such as supporting interoperability with
/// non-standards-complaint OAuth2 providers
/// non-standards-complaint OAuth2 providers.
///
pub trait ErrorResponse: Debug + DeserializeOwned + Serialize {}
/// The [`Display`] trait implementation for types implementing [`ErrorResponse`] should be a
/// human-readable string suitable for printing (e.g., within a [`RequestTokenError`]).
///
pub trait ErrorResponse: Debug + Display + DeserializeOwned + Serialize {}

///
/// Error types enum.
Expand All @@ -3041,7 +3045,7 @@ pub trait ErrorResponse: Debug + DeserializeOwned + Serialize {}
/// this error type. This value must match the error type from the relevant OAuth 2.0 standards
/// (RFC 6749 or an extension).
///
pub trait ErrorResponseType: Debug + DeserializeOwned + Serialize {}
pub trait ErrorResponseType: Debug + Display + DeserializeOwned + Serialize {}

///
/// Error response returned by server after requesting an access token.
Expand Down Expand Up @@ -3129,8 +3133,9 @@ where
}

if let Some(error_uri) = self.error_uri() {
formatted.push_str(" / See ");
formatted.push_str(" (see ");
formatted.push_str(error_uri);
formatted.push(')');
}

write!(f, "{}", formatted)
Expand All @@ -3150,7 +3155,7 @@ where
/// Error response returned by authorization server. Contains the parsed `ErrorResponse`
/// returned by the server.
///
#[error("Server returned error response")]
#[error("Server returned error response: {0}")]
ServerResponse(T),
///
/// An error occurred while sending the request or receiving the response (e.g., network
Expand Down
29 changes: 20 additions & 9 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1011,7 +1011,10 @@ fn test_exchange_code_with_simple_json_error() {
format!("{:?}", token_err)
);
// Test Display trait for RequestTokenError
assert_eq!("Server returned error response", format!("{}", token_err));
assert_eq!(
"Server returned error response: invalid_request: stuff happened",
token_err.to_string()
);
}

#[test]
Expand Down Expand Up @@ -1141,7 +1144,7 @@ fn test_exchange_code_with_invalid_token_type() {
fn test_exchange_code_with_400_status_code() {
let body = r#"{"error":"invalid_request","error_description":"Expired code."}"#;
let client = new_client();
let token = client
let token_err = client
.exchange_code(AuthorizationCode::new("ccc".to_string()))
.request(mock_http_client(
vec![
Expand All @@ -1161,12 +1164,12 @@ fn test_exchange_code_with_400_status_code() {
.collect(),
body: body.to_string().into_bytes(),
},
));

assert!(token.is_err());
))
.err()
.unwrap();

match token.err().unwrap() {
RequestTokenError::ServerResponse(error_response) => {
match token_err {
RequestTokenError::ServerResponse(ref error_response) => {
assert_eq!(
BasicErrorResponseType::InvalidRequest,
*error_response.error()
Expand All @@ -1179,6 +1182,11 @@ fn test_exchange_code_with_400_status_code() {
}
other => panic!("Unexpected error: {:?}", other),
}

assert_eq!(
"Server returned error response: invalid_request: Expired code.",
token_err.to_string(),
);
}

#[test]
Expand Down Expand Up @@ -1457,7 +1465,7 @@ fn test_extension_with_simple_json_error() {
assert!(token.is_err());

let token_err = token.err().unwrap();
match &token_err {
match token_err {
RequestTokenError::ServerResponse(ref error_response) => {
assert_eq!(ColorfulErrorResponseType::TooLight, *error_response.error());
assert_eq!(
Expand Down Expand Up @@ -1494,7 +1502,10 @@ fn test_extension_with_simple_json_error() {
format!("{:?}", token_err)
);
// Test Display trait for RequestTokenError
assert_eq!("Server returned error response", format!("{}", token_err));
assert_eq!(
"Server returned error response: too_light: stuff happened (see https://errors)",
token_err.to_string()
);
}

mod custom_errors {
Expand Down

0 comments on commit 3d69361

Please sign in to comment.