@@ -318,6 +318,35 @@ class TestHttpClient
318318 expect ( response ) . to eq ( response_json . merge ( headers : mock_headers ) )
319319 end
320320
321+ it "returns an error with headers" do
322+ response_json = {
323+ foo : "bar" ,
324+ error : {
325+ type : "api_error" ,
326+ message : "An unexpected error occurred" ,
327+ provider_error : "This is the provider error"
328+ }
329+ }
330+ request_params = { method : :get , path : "https://test.api.nylas.com/foo" , timeout : 30 }
331+ mock_headers = {
332+ "content-type" => "application/json" ,
333+ "x-request-id" => "123" ,
334+ "some-header" => "value"
335+ }
336+ mock_response = instance_double ( "HTTParty::Response" ,
337+ body : response_json . to_json ,
338+ headers : mock_headers ,
339+ code : 429 )
340+
341+ allow ( HTTParty ) . to receive ( :get ) . and_return ( mock_response )
342+
343+ expect do
344+ http_client . send ( :execute , **request_params )
345+ end . to raise_error ( Nylas ::NylasApiError ) { |error |
346+ expect ( error . headers ) . to eq ( mock_headers )
347+ }
348+ end
349+
321350 it "raises a timeout error" do
322351 request_params = { method : :get , path : "https://test.api.nylas.com/foo" , timeout : 30 }
323352 allow ( HTTParty ) . to receive ( :get ) . and_raise ( Net ::OpenTimeout )
@@ -462,22 +491,22 @@ class TestHttpClient
462491 type : "api_error" ,
463492 message : "An unexpected error occurred" ,
464493 provider_error : "This is the provider error"
465- } ,
466- headers : {
467- "x-request-id" : "request-id-from-headers" ,
468- "x-ratelimit-limit" : "100" ,
469- "x-ratelimit-remaining" : "99"
470494 }
471495 }
496+ headers = {
497+ "x-request-id" : "request-id-from-headers" ,
498+ "x-ratelimit-limit" : "100" ,
499+ "x-ratelimit-remaining" : "99"
500+ }
472501
473- err_obj = http_client . send ( :error_hash_to_exception , response , 400 , "https://test.api.nylas.com/foo" )
502+ err_obj = http_client . send ( :error_hash_to_exception , response , 400 , "https://test.api.nylas.com/foo" , headers )
474503
475504 expect ( err_obj ) . to be_a ( Nylas ::NylasApiError )
476505 expect ( err_obj . message ) . to eq ( "An unexpected error occurred" )
477506 expect ( err_obj . request_id ) . to eq ( "request-id" )
478507 expect ( err_obj . provider_error ) . to eq ( "This is the provider error" )
479508 expect ( err_obj . type ) . to eq ( "api_error" )
480- expect ( err_obj . headers ) . to eq ( response [ : headers] )
509+ expect ( err_obj . headers ) . to eq ( headers )
481510 end
482511 end
483512
@@ -555,11 +584,17 @@ class TestHttpClient
555584 provider_error : "This is the provider error"
556585 }
557586 }
587+ headers = {
588+ "x-request-id" : "request-id-from-headers" ,
589+ "x-ratelimit-limit" : "100" ,
590+ }
558591
559592 expect do
560593 http_client . send ( :parse_json_evaluate_error , 400 , response . to_json ,
561- "https://test.api.nylas.com/foo" , "application/json" )
562- end . to raise_error ( Nylas ::NylasApiError )
594+ "https://test.api.nylas.com/foo" , "application/json" , headers )
595+ end . to raise_error ( Nylas ::NylasApiError ) { |error |
596+ expect ( error . headers ) . to eq ( headers )
597+ }
563598 end
564599
565600 it "raises a NylasApiError for a non-JSON response" do
0 commit comments