Skip to content

Commit

Permalink
fix(integration): Fix error handling for Bad Gateway errors received …
Browse files Browse the repository at this point in the history
…with HTTP 200
  • Loading branch information
vincent-pochet committed Feb 21, 2025
1 parent 14674f7 commit 03fc635
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class PullTaxesAndApplyJob < ApplicationJob
queue_as "providers"

retry_on BaseService::ThrottlingError, wait: :polynomially_longer, attempts: 25
retry_on LagoHttpClient::HttpError, wait: :polynomially_longer, attempts: 6

def perform(invoice:)
Invoices::ProviderTaxes::PullTaxesAndApplyService.call!(invoice:)
Expand Down
16 changes: 16 additions & 0 deletions app/services/integrations/aggregator/base_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module Aggregator
class BaseService < BaseService
BASE_URL = "https://api.nango.dev/"
REQUEST_LIMIT_ERROR_CODE = "SSS_REQUEST_LIMIT_EXCEEDED"
BAD_REQUEST_ERROR = "502 Bad Gateway"

def initialize(integration:, options: {})
@integration = integration
Expand Down Expand Up @@ -152,6 +153,21 @@ def message(error)
def request_limit_error?(http_error)
http_error.error_body.include?(REQUEST_LIMIT_ERROR_CODE)
end

def bad_gateway_error?(http_error)
http_error.error_body.include?(BAD_REQUEST_ERROR)
end

def parse_response(response)
JSON.parse(response.body)
rescue JSON::ParserError
if response.body.include?(BAD_REQUEST_ERROR)
# NOTE: Sometimes, Anrock is responding with an HTTP 200 with a payload containing a 502 error...
raise(LagoHttpClient::HttpError.new(502, response.body, http_client.uri))
end

raise
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@ def call
throttle!(:anrok)

response = http_client.post_with_response(payload, headers)
body = JSON.parse(response.body)
body = parse_response(response)

process_response(body)

result
rescue LagoHttpClient::HttpError => e
raise RequestLimitError(e) if request_limit_error?(e)
raise e if bad_gateway_error?(e)

code = code(e)
message = message(e)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@ def call
throttle!(:anrok)

response = http_client.post_with_response(payload, headers)
body = JSON.parse(response.body)
body = parse_response(response)

process_response(body)
assign_external_customer_id

result
rescue LagoHttpClient::HttpError => e
raise RequestLimitError(e) if request_limit_error?(e)
raise Integrations::Aggregator::RequestLimitError(e) if request_limit_error?(e)
raise e if bad_gateway_error?(e)

code = code(e)
message = message(e)
Expand Down
4 changes: 3 additions & 1 deletion lib/lago_http_client/lago_http_client/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ module LagoHttpClient
class Client
RESPONSE_SUCCESS_CODES = [200, 201, 202, 204].freeze

attr_reader :uri

def initialize(url, read_timeout: nil)
@uri = URI(url)
@http_client = Net::HTTP.new(uri.host, uri.port)
Expand Down Expand Up @@ -109,7 +111,7 @@ def get(headers: {}, params: nil, body: nil)

private

attr_reader :uri, :http_client
attr_reader :http_client

def raise_error(response)
raise(::LagoHttpClient::HttpError.new(response.code, response.body, uri))
Expand Down
10 changes: 10 additions & 0 deletions spec/fixtures/integration_aggregator/bad_gateway_error.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<html>
<head>
<title>502 Bad Gateway</title>
</head>
<body>
<center><h1>502 Bad Gateway</h1></center>
<hr />
<center>cloudflare</center>
</body>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@
before do
allow(lago_client).to receive(:post_with_response).with(kind_of(Array), headers).and_return(response)
allow(response).to receive(:body).and_return(body)
allow(lago_client).to receive(:uri).and_return(endpoint)
end

context "when taxes are successfully fetched" do
Expand Down Expand Up @@ -252,6 +253,17 @@
end
end
end

context 'when the body contains a bad gateway error' do
let(:body) do
path = Rails.root.join("spec/fixtures/integration_aggregator/bad_gateway_error.html")
File.read(path)
end

it "raises an HTTP error" do
expect { service_call }.to raise_error(LagoHttpClient::HttpError)
end
end
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@
before do
allow(lago_client).to receive(:post_with_response).with(array_including(params), headers).and_return(response)
allow(response).to receive(:body).and_return(body)
allow(lago_client).to receive(:uri).and_return(endpoint)
end

context "when taxes are successfully fetched for finalized invoice" do
Expand Down Expand Up @@ -180,6 +181,17 @@

expect(integration_customer.reload.external_customer_id).to eq(nil)
end

context 'when the body contains a bad gateway error' do
let(:body) do
path = Rails.root.join("spec/fixtures/integration_aggregator/bad_gateway_error.html")
File.read(path)
end

it "raises an HTTP error" do
expect { service_call }.to raise_error(LagoHttpClient::HttpError)
end
end
end
end

Expand Down

0 comments on commit 03fc635

Please sign in to comment.