From e0b8d7f0e8e2d64d6be8dda587838572a0e6f7f6 Mon Sep 17 00:00:00 2001 From: Mark Mulder Date: Thu, 29 Mar 2018 16:33:34 +0200 Subject: [PATCH] Expose error details on 500 Found this out while reproducing a 500: ``` (byebug) JSON.parse(last_response.body) {"code"=>500, "error"=>"Did not find any valid email for customer [snipped]"} ``` --- lib/help_scout.rb | 3 ++- spec/helpscout_spec.rb | 17 ++++++++++++++--- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/lib/help_scout.rb b/lib/help_scout.rb index 9db842b..9c384bf 100644 --- a/lib/help_scout.rb +++ b/lib/help_scout.rb @@ -240,7 +240,8 @@ def request(method, path, options) when HTTP_NOT_FOUND raise NotFoundError when HTTP_INTERNAL_SERVER_ERROR - raise InternalServerError + error_message = JSON.parse(last_response.body)["error"] + raise InternalServerError, error_message when HTTP_TOO_MANY_REQUESTS retry_after = last_response.headers["Retry-After"] error_message = "Rate limit of 200 RPM or 12 POST/PUT/DELETE requests per 5 seconds reached. Next request possible in #{retry_after} seconds." diff --git a/spec/helpscout_spec.rb b/spec/helpscout_spec.rb index 3f985e2..3b28d87 100644 --- a/spec/helpscout_spec.rb +++ b/spec/helpscout_spec.rb @@ -65,11 +65,22 @@ end context 'with a 500 status code' do - it 'returns InternalServerError' do + it 'returns InternalServerError with body' do + expected_error_message = "Did not find any valid email for customer 1111" + url = 'https://api.helpscout.net/v1/conversations/1337.json' - stub_request(:get, url).to_return(status: 500) + stub_request(:post, url). + to_return( + status: 500, + body: { + "code": 500, + "error": expected_error_message, + }.to_json, + ) - expect { client.get_conversation(1337) }.to raise_error(HelpScout::InternalServerError) + expect { + client.create_thread(conversation_id: 1337, thread: {}) + }.to raise_error(HelpScout::InternalServerError, expected_error_message) end end