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

Nylas API returns Invalid Exception Message: message in case of errors #405

Closed
vsp4 opened this issue Mar 3, 2023 · 1 comment · Fixed by #442
Closed

Nylas API returns Invalid Exception Message: message in case of errors #405

vsp4 opened this issue Mar 3, 2023 · 1 comment · Fixed by #442

Comments

@vsp4
Copy link

vsp4 commented Mar 3, 2023

Describe the bug
Nylas API seems to return a html response for error cases unless 'Accept: application/json' is explicitly specified https://developer.nylas.com/docs/api/#post/oauth/token

This leads to invalid exception messages as this gem is expecting a json response back.

I do not see 'Accept: application/json' in default_headers in https://github.com/nylas/nylas-ruby/blob/main/lib/nylas/http_client.rb#L180-L186 Either setting this within gem or returning a default application/json in case content-type: application/json from nylas api should fix this.

To Reproduce
Ruby Code

begin
  nylas = Nylas::API.new(
    app_id: "CLIENT_ID",
    app_secret: "CLIENT_SECRET",
    access_token: "ACCESS_TOKEN"
  )
  nylas.exchange_code_for_token("abc")
rescue => e
  p e
  p e.message # This should contain valid info and not just 'message'
end

Output:
#<Nylas::InvalidRequest: message>
"message"

Curl with Accept: application/json

 curl --request POST \
  --url https://api.nylas.com/oauth/token \
  --header 'Accept: application/json' \
  --header 'Content-Type: application/json' \
  -u "<CLIENT_SECRET>:"\
  --data '{
  "client_id": "<NYLAS_CLIENT_ID",
  "client_secret": "<NYLAS_CLIENT_SECRET>",
  "grant_type": "<GRANT_TYPE>",
  "code": "<CODE>"
}'

Curl without Accept: application/json

 curl --request POST \
  --url https://api.nylas.com/oauth/token \
  --header 'Content-Type: application/json' \
  -u "<CLIENT_SECRET>:"\
  --data '{
  "client_id": "<NYLAS_CLIENT_ID",
  "client_secret": "<NYLAS_CLIENT_SECRET>",
  "grant_type": "<GRANT_TYPE>",
  "code": "<CODE>"
}'

Expected behavior
Exceptions should contain correct information and messages so calling clients can appropriately classify / take decisions

def self.parse_error_response(response)
new(
response["type"],
response["message"],
response["server_error"]
)
end

SDK Version:
nylas (5.14.0)

Additional context
Add any other context about the problem here.

@vsp4 vsp4 added the bug label Mar 3, 2023
@mrashed-dev
Copy link
Contributor

Thanks for opening this issue @vsp4! We will fix this issue and include it in an upcoming release. Thanks for bringing this to our attention!

mrashed-dev added a commit that referenced this issue Dec 21, 2023
…d-exception-message-message-in-case-of-errors
mrashed-dev added a commit that referenced this issue Dec 21, 2023
We set a content-header type by default, but not an accept header. We should add it in to prevent some error messages not being formatted as JSON. Closes #405.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants