File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -60,3 +60,26 @@ impl From<serde_json::Error> for Error {
6060 Error :: new ( ErrorKind :: FlagsmithAPIError , e. to_string ( ) )
6161 }
6262}
63+
64+ #[ cfg( test) ]
65+ mod tests {
66+ use super :: * ;
67+
68+ #[ test]
69+ fn test_http_error_includes_status_and_body ( ) {
70+ let error = Error :: http (
71+ StatusCode :: BAD_GATEWAY ,
72+ "{\" detail\" :\" upstream unavailable\" }" . to_string ( ) ,
73+ ) ;
74+
75+ assert_eq ! ( error. kind, ErrorKind :: FlagsmithAPIError ) ;
76+ assert_eq ! (
77+ error. msg,
78+ "HTTP Api error: 502 Bad Gateway, {\" detail\" :\" upstream unavailable\" }"
79+ ) ;
80+ assert_eq ! (
81+ error. to_string( ) ,
82+ "Flagsmith API error: HTTP Api error: 502 Bad Gateway, {\" detail\" :\" upstream unavailable\" }"
83+ ) ;
84+ }
85+ }
Original file line number Diff line number Diff line change @@ -713,7 +713,8 @@ fn test_flagsmith_api_error_is_returned_if_something_goes_wrong_with_the_request
713713 when. method ( GET )
714714 . path ( "/api/v1/flags/" )
715715 . header ( "X-Environment-Key" , ENVIRONMENT_KEY ) ;
716- then. status ( 502 ) . json_body ( { } ) ; // returning 502
716+ then. status ( 502 )
717+ . json_body ( serde_json:: json!( { "detail" : "bad gateway" } ) ) ; // returning 502
717718 } ) ;
718719 let url = mock_server. url ( "/api/v1/" ) ;
719720 let flagsmith_options = FlagsmithOptions {
@@ -724,7 +725,11 @@ fn test_flagsmith_api_error_is_returned_if_something_goes_wrong_with_the_request
724725
725726 // When
726727 let err = flagsmith. get_environment_flags ( ) . err ( ) . unwrap ( ) ;
728+
729+ // Then: the error carries the HTTP status and the response body
727730 assert_eq ! ( err. kind, flagsmith:: error:: ErrorKind :: FlagsmithAPIError ) ;
731+ assert ! ( err. msg. contains( "502 Bad Gateway" ) , "unexpected msg: {}" , err. msg) ;
732+ assert ! ( err. msg. contains( "bad gateway" ) , "unexpected msg: {}" , err. msg) ;
728733}
729734
730735#[ rstest]
You can’t perform that action at this time.
0 commit comments