Skip to content

Commit

Permalink
Merge pull request #129 from mollie/GH-128
Browse files Browse the repository at this point in the history
Raise specific exception when resource does not exist
  • Loading branch information
vernondegoede authored Feb 1, 2020
2 parents 9819e19 + f6c5af4 commit 88be910
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/mollie/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ def perform_http_call(http_method, api_method, id = nil, http_body = {}, query =
Util.nested_underscore_keys(JSON.parse(response.body))
when 204
{} # No Content
when 404
json = JSON.parse(response.body)
exception = ResourceNotFoundError.new(json)
raise exception
else
json = JSON.parse(response.body)
exception = Mollie::RequestError.new(json)
Expand Down
3 changes: 3 additions & 0 deletions lib/mollie/exception.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,7 @@ def to_s
"#{status} #{title}: #{detail}"
end
end

class ResourceNotFoundError < RequestError
end
end
30 changes: 30 additions & 0 deletions test/mollie/client_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -135,5 +135,35 @@ def test_error_response
assert_equal(json['field'], e.field)
assert_equal(json['_links'], e.links)
end

def test_404_response
response = <<-JSON
{
"status": 404,
"title": "Not Found",
"detail": "No payment exists with token tr_WDqYK6vllg.",
"_links": {
"documentation": {
"href": "https://docs.mollie.com/guides/handling-errors",
"type": "text/html"
}
}
}
JSON

json = JSON.parse(response)
stub_request(:post, 'https://api.mollie.com/v2/my-method')
.to_return(status: 404, body: response, headers: {})

e = assert_raise ResourceNotFoundError.new(JSON.parse(response)) do
client.perform_http_call('POST', 'my-method', nil, {})
end

assert_equal(json['status'], e.status)
assert_equal(json['title'], e.title)
assert_equal(json['detail'], e.detail)
assert_equal(json['field'], e.field)
assert_equal(json['_links'], e.links)
end
end
end

0 comments on commit 88be910

Please sign in to comment.