Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle response objects of instance String for API errors #509

Merged
merged 5 commits into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

### Unreleased
* Fixed issue where errors were not properly thrown due to an instance type of `String` instead of `Hash`

### 6.2.2 / 2024-12-02
* Added support for private Scheduling configuration.
* Added ability to add optional `content_id` to support inline image on `send`.
Expand Down
20 changes: 17 additions & 3 deletions lib/nylas/handler/http_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -198,10 +198,24 @@ def error_hash_to_exception(response, status_code, path)

def throw_error(response, status_code)
error_obj = response[:error]
provider_error = error_obj.fetch(:provider_error, nil)

NylasApiError.new(error_obj[:type], error_obj[:message], status_code, provider_error,
response[:request_id])
# If `error_obj` is just a string, turn it into a hash with default keys.
if error_obj.is_a?(String)
error_obj = {
type: "NylasApiError",
message: error_obj
}
end

provider_error = error_obj.fetch(:provider_error, nil) if error_obj.is_a?(Hash)

NylasApiError.new(
error_obj[:type],
error_obj[:message],
status_code,
provider_error,
response[:request_id]
)
end

# Adds query parameters to a URL.
Expand Down
1 change: 1 addition & 0 deletions nylas.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Gem::Specification.new do |gem|
gem.license = "MIT"

# Runtime dependencies
gem.add_runtime_dependency "base64"
gem.add_runtime_dependency "mime-types", "~> 3.5", ">= 3.5.1"
gem.add_runtime_dependency "ostruct", "~> 0.6"
gem.add_runtime_dependency "rest-client", ">= 2.0.0", "< 3.0"
Expand Down
16 changes: 16 additions & 0 deletions spec/nylas/handler/http_client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,22 @@ class TestHttpClient
expect(err_obj.type).to eq("api_error")
end

it "raises NylasApiError if the error response is a string" do
response = {
request_id: "request-id",
error: "Bad Gateway"
}

err_obj = http_client.send(:error_hash_to_exception, response, 502, "https://test.api.nylas.com/foo")

expect(err_obj).to be_a(Nylas::NylasApiError)
expect(err_obj.message).to eq("Bad Gateway")
expect(err_obj.request_id).to eq("request-id")
expect(err_obj.provider_error).to eq(nil)
expect(err_obj.status_code).to eq(502)
expect(err_obj.type).to eq("NylasApiError")
end

it "raises the correct error for OAuth" do
response = {
error: "invalid_request",
Expand Down
Loading