Skip to content
New issue

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

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

Already on GitHub? # to your account

Improve error message when server returns invalid data #122

Merged
merged 3 commits into from
Aug 6, 2017

Conversation

ivoanjo
Copy link
Contributor

@ivoanjo ivoanjo commented Aug 6, 2017

As discussed in #121, the error message you get when a web server returns non-json hal data or the Content-Type is incorrect is rather unclear on what happened.

This commit adds a check when creating a Resource that the received representation is something that makes sense.

This turns the error message for an example such as Hyperclient.new('http://www.google.com/').foo from

/lib/hyperclient/collection.rb:78:in `method_missing': undefined method
`fetch' for #<String:0x005654b3898628> (NoMethodError)
        from /lib/hyperclient/resource.rb:87:in `block in method_missing'
        from /lib/hyperclient/resource.rb:85:in `each'
        from /lib/hyperclient/resource.rb:85:in `method_missing'
        from /lib/hyperclient/link.rb:129:in `method_missing'
        from example.rb:3:in `<main>'

to

/lib/hyperclient/resource.rb:90:in `validate': Invalid representation
for resource (got String, expected Hash). Is your web server returning
JSON HAL data with a 'Content-Type: application/hal+json' header?
(Hyperclient::InvalidRepresentationError)
        from /lib/hyperclient/resource.rb:43:in `initialize'
        from /lib/hyperclient/link.rb:179:in `new'
        from /lib/hyperclient/link.rb:179:in `http_method'
        from /lib/hyperclient/link.rb:89:in `_get'
        from /lib/hyperclient/link.rb:84:in `_resource'
        from /lib/hyperclient/link.rb:128:in `method_missing'
        from example.rb:3:in `<main>'

e.g. we now validate immediately after the request, and provide meaningful information and even a hint towards the possibly-missing header.

ivoanjo added 3 commits August 6, 2017 11:29
As discussed in #121, the error message you get when a web server
returns non-json hal data or the Content-Type is incorrect is rather
unclear on what happened.

This commit adds a check when creating a Resource that the received
representation is something that makes sense.

This turns the error message for an example such as
`Hyperclient.new('http://www.google.com/').foo` from

```
/lib/hyperclient/collection.rb:78:in `method_missing': undefined method
`fetch' for #<String:0x005654b3898628> (NoMethodError)
        from /lib/hyperclient/resource.rb:87:in `block in method_missing'
        from /lib/hyperclient/resource.rb:85:in `each'
        from /lib/hyperclient/resource.rb:85:in `method_missing'
        from /lib/hyperclient/link.rb:129:in `method_missing'
        from example.rb:3:in `<main>'
```

to

```
/lib/hyperclient/resource.rb:90:in `validate': Invalid representation
for resource (got String, expected Hash). Is your web server returning
JSON HAL data with a 'Content-Type: application/hal+json' header?
(Hyperclient::InvalidRepresentationError)
        from /lib/hyperclient/resource.rb:43:in `initialize'
        from /lib/hyperclient/link.rb:179:in `new'
        from /lib/hyperclient/link.rb:179:in `http_method'
        from /lib/hyperclient/link.rb:89:in `_get'
        from /lib/hyperclient/link.rb:84:in `_resource'
        from /lib/hyperclient/link.rb:128:in `method_missing'
        from example.rb:3:in `<main>'
```

e.g. we now validate immediately after the request, and provide
meaningful information and even a hint towards the possibly-missing
header.
@dblock
Copy link
Collaborator

dblock commented Aug 6, 2017

Great job, merging.

@dblock dblock merged commit a928527 into master Aug 6, 2017
@dblock dblock deleted the improve-errors-on-nonhal-response branch August 25, 2019 15:29
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants