11use std:: path:: Path ;
22
3- use anyhow:: Context ;
43use constants:: { DEFAULT_ORIGIN , TRUNK_PUBLIC_API_ADDRESS_ENV } ;
54use http:: { header:: HeaderMap , HeaderValue } ;
65use reqwest:: { header, Client , Response , StatusCode } ;
6+ use serde:: de:: DeserializeOwned ;
77use tokio:: fs;
88
99use crate :: call_api:: CallApi ;
@@ -124,10 +124,7 @@ impl ApiClient {
124124 & self . org_url_slug ,
125125 ) ?;
126126
127- response
128- . json :: < message:: CreateBundleUploadResponse > ( )
129- . await
130- . context ( "Failed to get response body as json." )
127+ self . deserialize_response :: < message:: CreateBundleUploadResponse > ( response) . await
131128 } ,
132129 log_progress_message : |time_elapsed, _| {
133130 format ! ( "Reporting bundle upload initiation to Trunk services is taking longer than expected. It has taken {} seconds so far." , time_elapsed. as_secs( ) )
@@ -168,10 +165,7 @@ impl ApiClient {
168165 & self . org_url_slug ,
169166 ) ?;
170167
171- response
172- . json :: < message:: GetQuarantineConfigResponse > ( )
173- . await
174- . context ( "Failed to get response body as json." )
168+ self . deserialize_response :: < message:: GetQuarantineConfigResponse > ( response) . await
175169 } ,
176170 log_progress_message : |time_elapsed, _| {
177171 format ! ( "Getting quarantine configuration from Trunk services is taking longer than expected. It has taken {} seconds so far." , time_elapsed. as_secs( ) )
@@ -244,12 +238,14 @@ impl ApiClient {
244238 let error_message = "Failed to send telemetry metrics." ;
245239 if !response. status ( ) . is_client_error ( ) {
246240 response. error_for_status ( ) . map_err ( |reqwest_error| {
247- anyhow:: Error :: from ( reqwest_error) . context ( error_message)
241+ tracing:: warn!( "{}" , error_message) ;
242+ anyhow:: Error :: from ( reqwest_error)
248243 } ) . map ( |_| ( ) )
249244 } else {
245+ tracing:: warn!( "{}" , error_message) ;
250246 match response. error_for_status ( ) {
251247 Ok ( ..) => Err ( anyhow:: Error :: msg ( error_message) ) ,
252- Err ( error) => Err ( anyhow:: Error :: from ( error) . context ( error_message ) ) ,
248+ Err ( error) => Err ( anyhow:: Error :: from ( error) ) ,
253249 }
254250 }
255251 } ,
@@ -267,6 +263,17 @@ impl ApiClient {
267263 . call_api ( )
268264 . await
269265 }
266+
267+ async fn deserialize_response < MessageType : DeserializeOwned > (
268+ & self ,
269+ response : Response ,
270+ ) -> Result < MessageType , anyhow:: Error > {
271+ let deserialized: reqwest:: Result < MessageType > = response. json :: < MessageType > ( ) . await ;
272+ if deserialized. is_err ( ) {
273+ tracing:: warn!( "Failed to get response body as json." ) ;
274+ }
275+ deserialized. map_err ( anyhow:: Error :: from)
276+ }
270277}
271278
272279#[ derive( Debug , Clone , Copy ) ]
@@ -306,7 +313,8 @@ pub(crate) fn status_code_help<T: FnMut(&Response) -> String>(
306313 if !response. status ( ) . is_client_error ( ) {
307314 response. error_for_status ( ) . map_err ( |reqwest_error| {
308315 let error_message = format ! ( "{base_error_message}{HELP_TEXT}" ) ;
309- anyhow:: Error :: from ( reqwest_error) . context ( error_message)
316+ tracing:: warn!( "{}" , error_message) ;
317+ anyhow:: Error :: from ( reqwest_error)
310318 } )
311319 } else {
312320 let domain: Option < String > = url:: Url :: parse ( api_host)
@@ -333,9 +341,10 @@ pub(crate) fn status_code_help<T: FnMut(&Response) -> String>(
333341
334342 let error_message_with_help = format ! ( "{error_message}{HELP_TEXT}" ) ;
335343
344+ tracing:: warn!( "{}" , error_message_with_help) ;
336345 match response. error_for_status ( ) {
337346 Ok ( ..) => Err ( anyhow:: Error :: msg ( error_message_with_help) ) ,
338- Err ( error) => Err ( anyhow:: Error :: from ( error) . context ( error_message_with_help ) ) ,
347+ Err ( error) => Err ( anyhow:: Error :: from ( error) ) ,
339348 }
340349 }
341350}
@@ -443,7 +452,7 @@ mod tests {
443452 . await
444453 . unwrap_err( )
445454 . to_string( )
446- . contains( "Failed to get quarantine bulk test. " ) ) ;
455+ . contains( "501 Not Implemented " ) ) ;
447456 assert_eq ! ( CALL_COUNT . load( Ordering :: Relaxed ) , 1 ) ;
448457 }
449458
@@ -487,7 +496,7 @@ mod tests {
487496 . await
488497 . unwrap_err( )
489498 . to_string( )
490- . contains( "Failed to get quarantine bulk test. " ) ) ;
499+ . contains( "500 Internal Server Error " ) ) ;
491500 assert_eq ! ( CALL_COUNT . load( Ordering :: Relaxed ) , 6 ) ;
492501 }
493502
@@ -526,6 +535,6 @@ mod tests {
526535 . await
527536 . unwrap_err( )
528537 . to_string( )
529- . contains( "Quarantining config not found " ) ) ;
538+ . contains( "404 Not Found " ) ) ;
530539 }
531540}
0 commit comments