@@ -68,6 +68,7 @@ use http::{
68
68
StatusCode ,
69
69
Uri ,
70
70
} ;
71
+ use http_body_util:: BodyExt ;
71
72
use itertools:: Itertools ;
72
73
use minitrace:: {
73
74
future:: FutureExt as _,
@@ -478,40 +479,34 @@ impl HttpError {
478
479
}
479
480
}
480
481
481
- #[ cfg( any( test, feature = "testing" ) ) ]
482
482
pub async fn error_message_from_bytes (
483
- bytes : hyper:: body:: Bytes ,
484
- ) -> ( Cow < ' static , str > , Cow < ' static , str > ) {
483
+ bytes : & hyper:: body:: Bytes ,
484
+ ) -> anyhow :: Result < ( Cow < ' static , str > , Cow < ' static , str > ) > {
485
485
let ResponseErrorMessage { code, message } =
486
- serde_json:: from_slice ( & bytes) . unwrap_or_else ( |_| {
487
- panic ! (
488
- "Couldn't deserialize as json: {}" ,
489
- String :: from_utf8_lossy( & bytes)
490
- )
491
- } ) ;
492
-
493
- ( code, message)
494
- }
486
+ serde_json:: from_slice ( bytes) . context ( format ! (
487
+ "Couldn't deserialize as json: {}" ,
488
+ String :: from_utf8_lossy( bytes)
489
+ ) ) ?;
495
490
496
- // Tests might parse a response back into a message
497
- #[ cfg( any( test, feature = "testing" ) ) ]
498
- pub async fn from_response ( response : Response ) -> Self {
499
- use http_body_util:: BodyExt ;
491
+ Ok ( ( code, message) )
492
+ }
500
493
494
+ pub async fn from_response ( response : Response ) -> anyhow:: Result < Self > {
501
495
let ( parts, body) = response. into_parts ( ) ;
502
496
let ( code, message) = Self :: error_message_from_bytes (
503
- body. collect ( )
497
+ & body
498
+ . collect ( )
504
499
. await
505
500
. expect ( "Couldn't collect body" )
506
501
. to_bytes ( ) ,
507
502
)
508
- . await ;
503
+ . await ? ;
509
504
510
- Self {
505
+ Ok ( Self {
511
506
status_code : parts. status ,
512
507
error_code : code,
513
508
msg : message,
514
- }
509
+ } )
515
510
}
516
511
}
517
512
@@ -1202,7 +1197,7 @@ mod tests {
1202
1197
use crate :: http:: HttpError ;
1203
1198
1204
1199
#[ tokio:: test]
1205
- async fn test_http_response_error_internal_server_error ( ) {
1200
+ async fn test_http_response_error_internal_server_error ( ) -> anyhow :: Result < ( ) > {
1206
1201
let err_text = "some random error" ;
1207
1202
let err = anyhow:: anyhow!( err_text) ;
1208
1203
let err_clone = anyhow:: anyhow!( err_text) ;
@@ -1222,14 +1217,15 @@ mod tests {
1222
1217
// Check the Response contains the ResponseErrorMessage
1223
1218
let http_response_err: HttpResponseError = err_clone. into ( ) ;
1224
1219
let response = http_response_err. into_response ( ) ;
1225
- let error = HttpError :: from_response ( response) . await ;
1220
+ let error = HttpError :: from_response ( response) . await ? ;
1226
1221
assert_eq ! ( error. status_code( ) , StatusCode :: INTERNAL_SERVER_ERROR ) ;
1227
1222
assert_eq ! ( error. error_code( ) , "InternalServerError" ) ;
1228
1223
assert_eq ! ( error. msg, INTERNAL_SERVER_ERROR_MSG ) ;
1224
+ Ok ( ( ) )
1229
1225
}
1230
1226
1231
1227
#[ tokio:: test]
1232
- async fn test_http_error_400 ( ) {
1228
+ async fn test_http_error_400 ( ) -> anyhow :: Result < ( ) > {
1233
1229
let status_code = StatusCode :: BAD_REQUEST ;
1234
1230
let error_code = "ErrorCode" ;
1235
1231
let msg = "Nice error message!" ;
@@ -1258,9 +1254,10 @@ mod tests {
1258
1254
// Check the Response contains the ResponseErrorMessage
1259
1255
let http_response_err: HttpResponseError = err_clone. into ( ) ;
1260
1256
let response = http_response_err. into_response ( ) ;
1261
- let error = HttpError :: from_response ( response) . await ;
1257
+ let error = HttpError :: from_response ( response) . await ? ;
1262
1258
assert_eq ! ( error. status_code( ) , status_code) ;
1263
1259
assert_eq ! ( error. error_code( ) , error_code) ;
1264
1260
assert_eq ! ( error. message( ) , msg) ;
1261
+ Ok ( ( ) )
1265
1262
}
1266
1263
}
0 commit comments