nylas / nylas-ruby

Ruby bindings for the Nylas Platform APIs
https://nylas.com/docs
MIT License
101 stars 130 forks source link

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

Closed vsp4 closed 11 months ago

vsp4 commented 1 year ago

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 https://github.com/nylas/nylas-ruby/blob/ef90d46f92d3e4467b991d7fb74156194c5186a7/lib/nylas/errors.rb#L49-L55

SDK Version: nylas (5.14.0)

Additional context Add any other context about the problem here.

mrashed-dev commented 1 year ago

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!