@@ -176,12 +176,10 @@ impl HttpIO for NativeHttp {
176
176
tracing:: Span :: current ( ) . set_attribute ( status_code. key , status_code. value ) ;
177
177
}
178
178
179
- Ok ( Response :: from_reqwest (
180
- response?
181
- . error_for_status ( )
182
- . map_err ( |err| err. without_url ( ) ) ?,
183
- )
184
- . await ?)
179
+ // Get the response
180
+ let response = response?;
181
+
182
+ Response :: from_reqwest_with_error_handling ( response) . await
185
183
}
186
184
}
187
185
@@ -282,4 +280,70 @@ mod tests {
282
280
let resp = make_request ( & url1, & native_http) . await ;
283
281
assert_eq ! ( resp. headers. get( "x-cache-lookup" ) . unwrap( ) , "MISS" ) ;
284
282
}
283
+ #[ tokio:: test]
284
+ async fn test_native_http_error_with_body ( ) {
285
+ let server = start_mock_server ( ) ;
286
+
287
+ // Mock a 404 response with an error message body
288
+ server. mock ( |when, then| {
289
+ when. method ( httpmock:: Method :: GET ) . path ( "/error-with-body" ) ;
290
+ then. status ( 404 ) . body ( "{\" error\" :\" Resource not found\" }" ) ;
291
+ } ) ;
292
+
293
+ let native_http = NativeHttp :: init ( & Default :: default ( ) , & Default :: default ( ) ) ;
294
+ let port = server. port ( ) ;
295
+ let request_url = format ! ( "http://localhost:{}/error-with-body" , port) ;
296
+
297
+ // Create a request that will result in a 404 error
298
+ let request = reqwest:: Request :: new ( Method :: GET , request_url. parse ( ) . unwrap ( ) ) ;
299
+ let result = native_http. execute ( request) . await ;
300
+
301
+ // Assert that we get an error
302
+ assert ! ( result. is_err( ) ) ;
303
+
304
+ // Convert the error to a string to check its content
305
+ let error = result. unwrap_err ( ) ;
306
+ let error_string = format ! ( "{:?}" , error) ;
307
+ // Check that the error contains both the status code and the error message
308
+ assert ! (
309
+ error_string. contains( "404" ) ,
310
+ "Error should contain status code"
311
+ ) ;
312
+ assert ! (
313
+ error_string. contains( "Resource not found" ) ,
314
+ "Error should contain the error message body"
315
+ ) ;
316
+ }
317
+
318
+ #[ tokio:: test]
319
+ async fn test_native_http_error_without_body ( ) {
320
+ let server = start_mock_server ( ) ;
321
+
322
+ // Mock a 500 response with an empty body
323
+ server. mock ( |when, then| {
324
+ when. method ( httpmock:: Method :: GET )
325
+ . path ( "/error-without-body" ) ;
326
+ then. status ( 500 ) . body ( "" ) ;
327
+ } ) ;
328
+
329
+ let native_http = NativeHttp :: init ( & Default :: default ( ) , & Default :: default ( ) ) ;
330
+ let port = server. port ( ) ;
331
+ let request_url = format ! ( "http://localhost:{}/error-without-body" , port) ;
332
+
333
+ // Create a request that will result in a 500 error
334
+ let request = reqwest:: Request :: new ( Method :: GET , request_url. parse ( ) . unwrap ( ) ) ;
335
+ let result = native_http. execute ( request) . await ;
336
+
337
+ // Assert that we get an error
338
+ assert ! ( result. is_err( ) ) ;
339
+
340
+ // Convert the error to a string to check its content
341
+ let error = result. unwrap_err ( ) ;
342
+ let error_string = format ! ( "{:?}" , error) ;
343
+ // Check that the error contains the status code but the body is empty
344
+ assert ! (
345
+ error_string. contains( "500" ) ,
346
+ "Error should contain status code"
347
+ ) ;
348
+ }
285
349
}
0 commit comments