Skip to content
Open
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
32 changes: 32 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use std::convert::From;
use std::error;
use std::fmt;

use reqwest::StatusCode;

/// Wraps several types of errors.
#[derive(Debug)]
pub struct Error {
Expand All @@ -22,6 +24,13 @@ impl Error{
msg
}
}

pub fn http(status: StatusCode, body: String) -> Error {
Error {
kind: ErrorKind::FlagsmithAPIError,
msg: format!("HTTP Api error: {status}, {body}")
}
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Expand Down Expand Up @@ -51,3 +60,26 @@ impl From<serde_json::Error> for Error {
Error::new(ErrorKind::FlagsmithAPIError, e.to_string())
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_http_error_includes_status_and_body() {
let error = Error::http(
StatusCode::BAD_GATEWAY,
"{\"detail\":\"upstream unavailable\"}".to_string(),
);

assert_eq!(error.kind, ErrorKind::FlagsmithAPIError);
assert_eq!(
error.msg,
"HTTP Api error: 502 Bad Gateway, {\"detail\":\"upstream unavailable\"}"
);
assert_eq!(
error.to_string(),
"Flagsmith API error: HTTP Api error: 502 Bad Gateway, {\"detail\":\"upstream unavailable\"}"
);
}
}
8 changes: 3 additions & 5 deletions src/flagsmith/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -434,13 +434,11 @@ fn get_json_response(
request = request.body(body.unwrap());
};
let response = request.send()?;
if response.status().is_success() {
let status = response.status();
if status.is_success() {
return Ok(response.json()?);
} else {
return Err(error::Error::new(
error::ErrorKind::FlagsmithAPIError,
response.text()?,
));
return Err(error::Error::http(status, response.text()?));
}
}

Expand Down
7 changes: 6 additions & 1 deletion tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -713,7 +713,8 @@ fn test_flagsmith_api_error_is_returned_if_something_goes_wrong_with_the_request
when.method(GET)
.path("/api/v1/flags/")
.header("X-Environment-Key", ENVIRONMENT_KEY);
then.status(502).json_body({}); // returning 502
then.status(502)
.json_body(serde_json::json!({"detail": "bad gateway"})); // returning 502
});
let url = mock_server.url("/api/v1/");
let flagsmith_options = FlagsmithOptions {
Expand All @@ -724,7 +725,11 @@ fn test_flagsmith_api_error_is_returned_if_something_goes_wrong_with_the_request

// When
let err = flagsmith.get_environment_flags().err().unwrap();

// Then: the error carries the HTTP status and the response body
assert_eq!(err.kind, flagsmith::error::ErrorKind::FlagsmithAPIError);
assert!(err.msg.contains("502 Bad Gateway"), "unexpected msg: {}", err.msg);
assert!(err.msg.contains("bad gateway"), "unexpected msg: {}", err.msg);
}

#[rstest]
Expand Down
Loading