Skip to content

Commit 2dd7aee

Browse files
include headers in response
1 parent 5195572 commit 2dd7aee

File tree

2 files changed

+53
-19
lines changed

2 files changed

+53
-19
lines changed

lib/nylas/handler/http_client.rb

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def execute(method:, path:, timeout:, headers: {}, query: {}, payload: nil, api_
4141
content_type = response.headers["content-type"].downcase
4242
end
4343

44-
parsed_response = parse_json_evaluate_error(result.code.to_i, response.body, path, content_type)
44+
parsed_response = parse_json_evaluate_error(result.code.to_i, response.body, path, content_type, response.headers)
4545
# Include headers in the response
4646
parsed_response[:headers] = response.headers unless parsed_response.nil?
4747
parsed_response
@@ -311,37 +311,36 @@ def handle_response(http, get_request, path, &block)
311311
end
312312

313313
# Parses the response from the Nylas API and evaluates for errors.
314-
def parse_json_evaluate_error(http_code, response, path, content_type = nil)
314+
def parse_json_evaluate_error(http_code, response, path, content_type = nil, headers = nil)
315315
begin
316316
response = parse_response(response) if content_type == "application/json"
317317
rescue Nylas::JsonParseError
318-
handle_failed_response(http_code, response, path)
318+
handle_failed_response(http_code, response, path, headers)
319319
raise
320320
end
321321

322-
handle_failed_response(http_code, response, path)
322+
handle_failed_response(http_code, response, path, headers)
323323
response
324324
end
325325

326326
# Handles failed responses from the Nylas API.
327-
def handle_failed_response(http_code, response, path)
327+
def handle_failed_response(http_code, response, path, headers = nil)
328328
return if HTTP_SUCCESS_CODES.include?(http_code)
329329

330+
puts response.inspect
331+
puts response.class.name
330332
case response
331333
when Hash
332-
raise error_hash_to_exception(response, http_code, path)
334+
raise error_hash_to_exception(response, http_code, path, headers)
333335
else
334336
raise NylasApiError.parse_error_response(response, http_code)
335337
end
336338
end
337339

338340
# Converts error hashes to exceptions.
339-
def error_hash_to_exception(response, status_code, path)
341+
def error_hash_to_exception(response, status_code, path, headers = nil)
340342
return if !response || !response.key?(:error)
341343

342-
# Safely get headers without risking KeyError
343-
headers = response.key?(:headers) ? response[:headers] : nil
344-
345344
if %W[#{api_uri}/v3/connect/token #{api_uri}/v3/connect/revoke].include?(path)
346345
NylasOAuthError.new(response[:error], response[:error_description], response[:error_uri],
347346
response[:error_code], status_code)

spec/nylas/handler/http_client_spec.rb

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)