Skip to content

Commit d8bdd1c

Browse files
kraju3AaronDDM
andauthored
Handle response objects of instance String for API errors (#509)
* Handle response objects of instance String for API errors * Added a test case for throw error * Add base64 * Update nylas.gemspec * Added changelog --------- Co-authored-by: Aaron de Mello <314152+AaronDDM@users.noreply.github.com>
1 parent a8ca3f7 commit d8bdd1c

File tree

4 files changed

+37
-3
lines changed

4 files changed

+37
-3
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Changelog
22

3+
### Unreleased
4+
* Fixed issue where errors were not properly thrown due to an instance type of `String` instead of `Hash`
5+
36
### 6.2.2 / 2024-12-02
47
* Added support for private Scheduling configuration.
58
* Added ability to add optional `content_id` to support inline image on `send`.

lib/nylas/handler/http_client.rb

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -198,10 +198,24 @@ def error_hash_to_exception(response, status_code, path)
198198

199199
def throw_error(response, status_code)
200200
error_obj = response[:error]
201-
provider_error = error_obj.fetch(:provider_error, nil)
202201

203-
NylasApiError.new(error_obj[:type], error_obj[:message], status_code, provider_error,
204-
response[:request_id])
202+
# If `error_obj` is just a string, turn it into a hash with default keys.
203+
if error_obj.is_a?(String)
204+
error_obj = {
205+
type: "NylasApiError",
206+
message: error_obj
207+
}
208+
end
209+
210+
provider_error = error_obj.fetch(:provider_error, nil) if error_obj.is_a?(Hash)
211+
212+
NylasApiError.new(
213+
error_obj[:type],
214+
error_obj[:message],
215+
status_code,
216+
provider_error,
217+
response[:request_id]
218+
)
205219
end
206220

207221
# Adds query parameters to a URL.

nylas.gemspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Gem::Specification.new do |gem|
1111
gem.license = "MIT"
1212

1313
# Runtime dependencies
14+
gem.add_runtime_dependency "base64"
1415
gem.add_runtime_dependency "mime-types", "~> 3.5", ">= 3.5.1"
1516
gem.add_runtime_dependency "ostruct", "~> 0.6"
1617
gem.add_runtime_dependency "rest-client", ">= 2.0.0", "< 3.0"

spec/nylas/handler/http_client_spec.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,22 @@ class TestHttpClient
251251
expect(err_obj.type).to eq("api_error")
252252
end
253253

254+
it "raises NylasApiError if the error response is a string" do
255+
response = {
256+
request_id: "request-id",
257+
error: "Bad Gateway"
258+
}
259+
260+
err_obj = http_client.send(:error_hash_to_exception, response, 502, "https://test.api.nylas.com/foo")
261+
262+
expect(err_obj).to be_a(Nylas::NylasApiError)
263+
expect(err_obj.message).to eq("Bad Gateway")
264+
expect(err_obj.request_id).to eq("request-id")
265+
expect(err_obj.provider_error).to eq(nil)
266+
expect(err_obj.status_code).to eq(502)
267+
expect(err_obj.type).to eq("NylasApiError")
268+
end
269+
254270
it "raises the correct error for OAuth" do
255271
response = {
256272
error: "invalid_request",

0 commit comments

Comments
 (0)