From f12eb648c9f00ac4bcecbe0407321f88cb7cc039 Mon Sep 17 00:00:00 2001 From: Peter Postma Date: Fri, 20 Dec 2024 10:35:36 +0100 Subject: [PATCH 1/2] When receiving an invalid JSON response (e.g. 503 with HTML), catch this and raise a Mollie::RequestError. --- lib/mollie/client.rb | 12 +++++++++++- test/mollie/client_test.rb | 22 ++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/lib/mollie/client.rb b/lib/mollie/client.rb index 63d1686..068c243 100644 --- a/lib/mollie/client.rb +++ b/lib/mollie/client.rb @@ -133,7 +133,7 @@ def perform_http_call(http_method, api_method, id = nil, http_body = {}, query = exception = ResourceNotFoundError.new(json, response) raise exception else - json = JSON.parse(response.body) + json = parse_body(response) exception = Mollie::RequestError.new(json, response) raise exception end @@ -162,5 +162,15 @@ def build_nested_query(value, prefix = nil) def escape(s) URI.encode_www_form_component(s) end + + def parse_body(response) + JSON.parse(response.body) + rescue JSON::ParserError => e + { + "status" => response.code.to_i, + "title" => response.message, + "detail" => "Unable to parse JSON: #{e.message}" + } + end end end diff --git a/test/mollie/client_test.rb b/test/mollie/client_test.rb index afe8894..2341f40 100644 --- a/test/mollie/client_test.rb +++ b/test/mollie/client_test.rb @@ -177,5 +177,27 @@ def test_404_response assert_equal(json['field'], e.field) assert_equal(json['_links'], e.links) end + + def test_invalid_response + response = <<-JSON + { + "status": 500, + "title": "Internal server error", + "detail": "Unable to parse JSON: 859: unexpected token at '

Internal server error

'" + } + JSON + + json = JSON.parse(response) + stub_request(:post, 'https://api.mollie.com/v2/my-method') + .to_return(status: [500, "Internal server error"], body: "

Internal server error

", headers: {}) + + e = assert_raise Mollie::RequestError.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) + end end end From 103359ec7f95b6da40c548f31a15f33091def153 Mon Sep 17 00:00:00 2001 From: Peter Postma Date: Fri, 20 Dec 2024 11:40:08 +0100 Subject: [PATCH 2/2] Change test `test_invalid_response` so it will work on different versions of Ruby. --- test/mollie/client_test.rb | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) diff --git a/test/mollie/client_test.rb b/test/mollie/client_test.rb index 2341f40..641385e 100644 --- a/test/mollie/client_test.rb +++ b/test/mollie/client_test.rb @@ -179,25 +179,16 @@ def test_404_response end def test_invalid_response - response = <<-JSON - { - "status": 500, - "title": "Internal server error", - "detail": "Unable to parse JSON: 859: unexpected token at '

Internal server error

'" - } - JSON - - json = JSON.parse(response) stub_request(:post, 'https://api.mollie.com/v2/my-method') .to_return(status: [500, "Internal server error"], body: "

Internal server error

", headers: {}) - e = assert_raise Mollie::RequestError.new(JSON.parse(response)) do + e = assert_raise Mollie::RequestError 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(500, e.status) + assert_equal('Internal server error', e.title) + assert_match(/Unable to parse JSON:.*/, e.detail) end end end