Skip to content

Commit

Permalink
Merge pull request #167 from mollie/GH-165
Browse files Browse the repository at this point in the history
Add HTTP response details to RequestError
  • Loading branch information
justincase authored Mar 20, 2024
2 parents bde1a0b + 09489be commit c695d86
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 3 deletions.
4 changes: 2 additions & 2 deletions lib/mollie/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,11 @@ def perform_http_call(http_method, api_method, id = nil, http_body = {}, query =
{} # No Content
when 404
json = JSON.parse(response.body)
exception = ResourceNotFoundError.new(json)
exception = ResourceNotFoundError.new(json, response)
raise exception
else
json = JSON.parse(response.body)
exception = Mollie::RequestError.new(json)
exception = Mollie::RequestError.new(json, response)
raise exception
end
end
Expand Down
15 changes: 14 additions & 1 deletion lib/mollie/exception.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,30 @@ class Exception < StandardError
class RequestError < Mollie::Exception
attr_accessor :status, :title, :detail, :field, :links

def initialize(error)
def initialize(error, response = nil)
exception.status = error['status']
exception.title = error['title']
exception.detail = error['detail']
exception.field = error['field']
exception.links = error['_links']
self.response = response
end

def to_s
"#{status} #{title}: #{detail}"
end

def http_headers
response.to_hash if response
end

def http_body
response.body if response
end

private

attr_accessor :response
end

class ResourceNotFoundError < RequestError
Expand Down
87 changes: 87 additions & 0 deletions test/mollie/exception_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
require 'helper'

module Mollie
class ExceptionTest < Test::Unit::TestCase
def test_attributes
stub_request(:post, 'https://api.mollie.com/v2/payments')
.to_return(status: 422, headers: { "Content-Type" => "application/hal+json}" }, body: %(
{
"status": 422,
"title": "Unprocessable Entity",
"detail": "The amount is higher than the maximum",
"field": "amount",
"_links": {
"documentation": {
"href": "https://docs.mollie.com/errors",
"type": "text/html"
}
}
}
))

exception = assert_raise(Mollie::RequestError) do
Mollie::Payment.create(
amount: { value: "1000000000.00", currency: "EUR" },
description: "Order #66",
redirect_url: "https://www.example.org/payment/completed",
)
end

assert_equal 422, exception.status
assert_equal "Unprocessable Entity", exception.title
assert_equal "The amount is higher than the maximum", exception.detail
assert_equal "amount", exception.field
assert_equal "https://docs.mollie.com/errors", exception.links["documentation"]["href"]
assert_equal "text/html", exception.links["documentation"]["type"]
end

def test_exception_message
stub_request(:get, 'https://api.mollie.com/v2/payments/tr_WDqYK6vllg')
.to_return(status: 401, headers: { "Content-Type" => "application/hal+json}" }, body: %(
{
"status": 401,
"title": "Unauthorized Request",
"detail": "Missing authentication, or failed to authenticate",
"_links": {
"documentation": {
"href": "https://docs.mollie.com/overview/authentication",
"type": "text/html"
}
}
}
))

exception = assert_raise(Mollie::RequestError) { Payment.get('tr_WDqYK6vllg') }
assert_equal '401 Unauthorized Request: Missing authentication, or failed to authenticate', exception.message
end

def test_http_attributes
body = %({
"status": 422,
"title": "Unprocessable Entity",
"detail": "The amount is higher than the maximum",
"field": "amount",
"_links": {
"documentation": {
"href": "https://docs.mollie.com/errors",
"type": "text/html"
}
}
})

stub_request(:post, 'https://api.mollie.com/v2/payments')
.to_return(status: 422, headers: { "Content-Type" => "application/hal+json" }, body: body)

exception = assert_raise(Mollie::RequestError) do
Mollie::Payment.create(
amount: { value: "1000000000.00", currency: "EUR" },
description: "Order #66",
redirect_url: "https://www.example.org/payment/completed",
)
end

assert_equal({ "content-type" => ["application/hal+json"] }, exception.http_headers)
assert_equal(body, exception.http_body)
end
end
end

0 comments on commit c695d86

Please sign in to comment.