From 7874f9798b1bbe6e272381f50242671e2747d089 Mon Sep 17 00:00:00 2001 From: Patrik Ragnarsson Date: Fri, 5 Apr 2024 15:38:37 +0200 Subject: [PATCH] Populate ClientError, ServerError with info Add the status code and the response body to the error message. To be able to make sense of these errors when they happen. --- lib/createsend/createsend.rb | 6 +++--- test/createsend_test.rb | 33 +++++++++++++++++++++++++++----- test/fixtures/error_response.txt | 1 + test/helper.rb | 4 ++-- 4 files changed, 34 insertions(+), 10 deletions(-) create mode 100644 test/fixtures/error_response.txt diff --git a/lib/createsend/createsend.rb b/lib/createsend/createsend.rb index 91ff055..7db44e5 100644 --- a/lib/createsend/createsend.rb +++ b/lib/createsend/createsend.rb @@ -284,12 +284,12 @@ def handle_response(response) # :nodoc: when 429 raise TooManyRequests.new when 400...500 - raise ClientError.new + raise ClientError.new("#{response.code}: #{response}") when 500...600 - raise ServerError.new + raise ServerError.new("#{response.code}: #{response}") else response end end end -end \ No newline at end of file +end diff --git a/test/createsend_test.rb b/test/createsend_test.rb index 4668f4b..ce7058e 100644 --- a/test/createsend_test.rb +++ b/test/createsend_test.rb @@ -1,4 +1,4 @@ -require File.dirname(__FILE__) + '/helper' +require_relative "helper" class CreateSendTest < Test::Unit::TestCase @@ -221,7 +221,7 @@ class CreateSendTest < Test::Unit::TestCase countries.size.should be == 245 assert countries.include? "Australia" end - + should "get system date" do stub_get(@auth, "systemdate.json", "systemdate.json") systemdate = @cs.systemdate.SystemDate @@ -234,7 +234,7 @@ class CreateSendTest < Test::Unit::TestCase timezones.size.should be == 97 assert timezones.include? "(GMT+12:00) Fiji" end - + should "get all administrators" do stub_get(@auth, "admins.json", "administrators.json") administrators = @cs.administrators @@ -278,6 +278,29 @@ class CreateSendTest < Test::Unit::TestCase @template = CreateSend::Template.new @auth, '98y2e98y289dh89h938389' end + should "raise ClientError with the response body" do + error_fixture = "custom_api_error.json" + error_body = fixture_file(error_fixture) + + stub_get(@auth, "countries.json", error_fixture, "418") + + assert_raise_with_message(CreateSend::ClientError, "418: #{error_body}") do + @cs.countries + end + end + + should "raise ServerError with response body" do + use_json_content_type = false + error_fixture = "error_response.txt" + error_body = fixture_file(error_fixture) + + stub_get(@auth, "countries.json", error_fixture, "500", use_json_content_type) + + assert_raise_with_message(CreateSend::ServerError, "500: #{error_body}") do + @cs.countries + end + end + { ["400", "Bad Request"] => CreateSend::BadRequest, ["401", "Unauthorized"] => CreateSend::Unauthorized, ["404", "Not Found"] => CreateSend::NotFound, @@ -294,7 +317,7 @@ class CreateSendTest < Test::Unit::TestCase context "#{status.first}, a post" do should "raise a #{exception.name} error" do - stub_post(@auth, "clients.json", (status.first == '400' or status.first == '401') ? 'custom_api_error.json' : nil, status) + stub_post(@auth, "clients.json", (status.first == '400' or status.first == '401') ? 'custom_api_error.json' : nil, status) lambda { CreateSend::Client.create @auth, "Client Company Name", "(GMT+10:00) Canberra, Melbourne, Sydney", "Australia" }.should raise_error(exception) end @@ -303,7 +326,7 @@ class CreateSendTest < Test::Unit::TestCase context "#{status.first}, a put" do should "raise a #{exception.name} error" do stub_put(@auth, "templates/#{@template.template_id}.json", (status.first == '400' or status.first == '401') ? 'custom_api_error.json' : nil, status) - lambda { @template.update "Template One Updated", "http://templates.org/index.html", + lambda { @template.update "Template One Updated", "http://templates.org/index.html", "http://templates.org/files.zip" }.should raise_error(exception) end end diff --git a/test/fixtures/error_response.txt b/test/fixtures/error_response.txt new file mode 100644 index 0000000..5310b71 --- /dev/null +++ b/test/fixtures/error_response.txt @@ -0,0 +1 @@ +some error, not json diff --git a/test/helper.rb b/test/helper.rb index 56a1972..797c736 100644 --- a/test/helper.rb +++ b/test/helper.rb @@ -37,11 +37,11 @@ def createsend_url(auth, url) result end -def stub_request(method, auth, url, filename, status=nil) +def stub_request(method, auth, url, filename, status=nil, json=true) options = {:body => ""} options.merge!({:body => fixture_file(filename)}) if filename options.merge!({:status => status}) if status - options.merge!(:content_type => "application/json; charset=utf-8") + options.merge!(:content_type => "application/json; charset=utf-8") if json createsend_url = createsend_url(auth, url) FakeWeb.register_uri(method, createsend_url, options) end