Skip to content

Commit

Permalink
Populate ClientError, ServerError with info
Browse files Browse the repository at this point in the history
Add the status code and the response body to the error message.
To be able to make sense of these errors when they happen.
  • Loading branch information
dentarg committed Apr 5, 2024
1 parent 86a5230 commit 7874f97
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 10 deletions.
6 changes: 3 additions & 3 deletions lib/createsend/createsend.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
end
33 changes: 28 additions & 5 deletions test/createsend_test.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require File.dirname(__FILE__) + '/helper'
require_relative "helper"

class CreateSendTest < Test::Unit::TestCase

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/error_response.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
some error, not json
4 changes: 2 additions & 2 deletions test/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)

This comment has been minimized.

Copy link
@walro

walro Apr 5, 2024

don't see a false case?

This comment has been minimized.

Copy link
@walro

walro Apr 5, 2024

ah, it gets delegated to

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
Expand Down

0 comments on commit 7874f97

Please sign in to comment.