Skip to content

Commit

Permalink
Merge pull request #56 from gocardless/andrei/handle-bad-gateway-errors
Browse files Browse the repository at this point in the history
Add special treating for 502 errors

Create class for Bad Gateway error and treat the case separately.

Bump version to 0.4.4
  • Loading branch information
andrei authored Jan 7, 2019
2 parents 151cc2c + ccd6bca commit 1e99343
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 1 deletion.
10 changes: 10 additions & 0 deletions lib/companies_house/bad_gateway_error.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# frozen_string_literal: true

module CompaniesHouse
# Raised when a request returned a 502
class BadGatewayError < APIError
def initialize(response = nil)
super("Bad gateway error", response)
end
end
end
1 change: 1 addition & 0 deletions lib/companies_house/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class Client

def initialize(config)
raise ArgumentError, "Missing API key" unless config[:api_key]

@api_key = config[:api_key]
@endpoint = URI(config[:endpoint] || ENDPOINT)
@open_timeout = config[:open_timeout] || 60
Expand Down
3 changes: 3 additions & 0 deletions lib/companies_house/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
require "companies_house/authentication_error"
require "companies_house/rate_limit_error"
require "companies_house/timeout_error"
require "companies_house/bad_gateway_error"

require "virtus"
require "uri"
Expand Down Expand Up @@ -88,6 +89,8 @@ def parse(response, resource_type, resource_id)
raise CompaniesHouse::NotFoundError.new(resource_type, resource_id, response)
when "429"
raise CompaniesHouse::RateLimitError, response
when "502"
raise CompaniesHouse::BadGatewayError, response
else
raise CompaniesHouse::APIError.new("Unknown API response", response)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/companies_house/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module CompaniesHouse
VERSION = "0.4.3"
VERSION = "0.4.4"
end
8 changes: 8 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,14 @@ def notifications_of
it_behaves_like "an error response"
end

context "502" do
let(:status) { 502 }
let(:message) { "Bad gateway error - HTTP 502" }
let(:error_class) { CompaniesHouse::BadGatewayError }

it_behaves_like "an error response"
end

context "any other code" do
let(:status) { 342 }
let(:message) { "Unknown API response - HTTP 342" }
Expand Down

0 comments on commit 1e99343

Please sign in to comment.