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

Use JSON (result.body) for exception messages in requests #67

Merged
merged 3 commits into from
Jun 7, 2016

Conversation

ianks
Copy link
Contributor

@ianks ianks commented May 12, 2016

Previously, Auth0::Exception was being fed a Hash for its message content.
All messages are stringified, so when a user accesses the error message,
they are left with a string representation of a hash. Here is an example.

def some_method
  auth0_client.user('notarealuser')
rescue Auth0::Exception => e
  puts e.message # => "{\"statusCode\"=>400, \"error\"=>\"Bad Request\", \"message\"=>\"Path validation error: 'String does not match pattern ^.+\\\\|.+$: 3241312' on property id (The user_id of the user to retrieve).\", \"errorCode\"=>\"invalid_uri\"}"

  # cannot parse this error, :(
end

These hash representations do not have an efficient, robust parsing mechanism.
Therefore, I propose we simply use the JSON representation from the response
body as the message, so users can parse the message and do what they need
with it. A new example would work like this:

def some_method
  auth0_client.user('notarealuser')
rescue Auth0::Exception => e
  JSON.parse(e.message) #=> {"statusCode"=>400, "error"=>"Bad Request", "message"=>"Path validation error: 'String does not match pattern ^.+\\|.+$: 3241312' on property id (The user_id of the user to retrieve).", "errorCode"=>"invalid_uri"}

  # yay!
end

Previously, Auth0::Exception was being fed a Hash for its message content.
All messages are stringified, so when a user accesses the error message,
they are left with a string representation of a hash. Here is an example.

```ruby
def some_method
  auth0_client.user('notarealuser')
rescue Auth0::Exception => e
  puts e.message # => "{\"statusCode\"=>400, \"error\"=>\"Bad Request\", \"message\"=>\"Path validation error: 'String does not match pattern ^.+\\\\|.+$: 3241312' on property id (The user_id of the user to retrieve).\", \"errorCode\"=>\"invalid_uri\"}"

  # cannot parse this error, :(
end
```

These hash representations do not have an efficient, robust parsing mechanism.
Therefore, I propose we simply use the JSON representation from the response
body as the message, so users can parse the message and do what they need
with it. A new example would work like this:

```ruby
def some_method
  auth0_client.user('notarealuser')
rescue Auth0::Exception => e
  JSON.parse(e.message) #=> {"statusCode"=>400, "error"=>"Bad Request", "message"=>"Path validation error: 'String does not match pattern ^.+\\|.+$: 3241312' on property id (The user_id of the user to retrieve).", "errorCode"=>"invalid_uri"}

  # yay!
end
```
@ianks
Copy link
Contributor Author

ianks commented May 13, 2016

@auth0 I fixed the travis build as well. listen is not compatible with versions of ruby less than 2.2.3, so I prevented listen from being installed on travis.

@ntotten ntotten merged commit 9c509ec into auth0:master Jun 7, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants