Skip to content

Improve error message when server returns invalid data #122

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

Merged
merged 3 commits into from
Aug 6, 2017
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
13 changes: 6 additions & 7 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,16 @@ sudo: false

matrix:
include:
- rvm: 2.3.1
- rvm: 2.4.1
- rvm: 2.4.1
script:
- bundle exec danger
- rvm: 2.3.1
- rvm: 2.3.0
- rvm: 2.2.5
- rvm: 2.4.0
- rvm: jruby-9.1.12.0
- rvm: jruby-head
- rvm: 2.2.7
- rvm: 2.3.4
- rvm: rbx-2
- rvm: ruby-head
- rvm: jruby-head
- rvm: jruby-9.1.7.0
allow_failures:
- rvm: ruby-head
- rvm: jruby-head
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
### 0.8.6 (Next)

* [#122](https://github.com/codegram/hyperclient/pull/122): Improve error message when server returns invalid data - [@ivoanjo](https://github.com/ivoanjo).
* Your contribution here.

### 0.8.5 (July 5, 2017)
Expand Down
2 changes: 1 addition & 1 deletion features/steps/default_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Spinach::Features::DefaultConfig < Spinach::FeatureSteps
end

step 'I send some data to the API' do
stub_request(:post, 'http://api.example.org/posts')
stub_request(:post, 'http://api.example.org/posts').to_return(headers: { 'Content-Type' => 'application/hal+json' })
assert_equal 200, api._links.posts._post(title: 'My first blog post')._response.status
end

Expand Down
29 changes: 28 additions & 1 deletion lib/hyperclient/resource.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
require 'forwardable'

module Hyperclient
# Public: Exception that is raised when passing in invalid representation data
# for the resource.
class InvalidRepresentationError < ArgumentError
attr_reader :representation

def initialize(error_description, representation)
super(error_description)
@representation = representation
end
end

# Public: Represents a resource from your API. Its responsability is to
# ease the way you access its attributes, links and embedded resources.
class Resource
Expand Down Expand Up @@ -29,8 +40,9 @@ class Resource
# representation - The hash with the HAL representation of the Resource.
# entry_point - The EntryPoint object to inject the configutation.
def initialize(representation, entry_point, response = nil)
representation = representation ? representation.dup : {}
representation = validate(representation)
links = representation['_links'] || {}

@_links = LinkCollection.new(links, links['curies'], entry_point)
@_embedded = ResourceCollection.new(representation['_embedded'], entry_point)
@_attributes = Attributes.new(representation)
Expand Down Expand Up @@ -68,6 +80,21 @@ def fetch(key, *args)

private

# Internal: Ensures the received representation is a valid Hash-lookalike.
def validate(representation)
return {} unless representation

if representation.respond_to?(:to_hash)
representation.to_hash.dup
else
raise InvalidRepresentationError.new(
"Invalid representation for resource (got #{representation.class}, expected Hash). " \
"Is your web server returning JSON HAL data with a 'Content-Type: application/hal+json' header?",
representation
)
end
end

# Internal: Returns the self Link of the Resource. Used to handle the HTTP
# methods.
def _self_link
Expand Down
6 changes: 6 additions & 0 deletions test/hyperclient/resource_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ module Hyperclient

resource._response.body.must_equal body
end

describe 'with an invalid representation' do
it 'raises an InvalidRepresentationError' do
proc { Resource.new('invalid representation data', entry_point) }.must_raise InvalidRepresentationError
end
end
end

describe '_links' do
Expand Down