Skip to content

Commit be551a7

Browse files
committed
Handle 400, 422, 500 responses
1 parent 09a5d1f commit be551a7

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

lib/adyen/client.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,13 +215,19 @@ def call_adyen_api(service, action, request_data, headers, version, _with_applic
215215
end
216216
# check for API errors
217217
case response.status
218+
when 400
219+
raise Adyen::FormatError.new('Invalid format or fields', request_data, response.body)
218220
when 401
219221
raise Adyen::AuthenticationError.new(
220222
'Invalid API authentication; https://docs.adyen.com/user-management/how-to-get-the-api-key', request_data
221223
)
222224
when 403
223225
raise Adyen::PermissionError.new('Missing user permissions; https://docs.adyen.com/user-management/user-roles',
224226
request_data, response.body)
227+
when 422
228+
raise Adyen::ValidationError.new('Validation error', request_data, response.body)
229+
when 500..599
230+
raise Adyen::ServerError.new('Internal server error', request_data, response.body)
225231
end
226232

227233
# delete has no response.body (unless it throws an error)

spec/client_spec.rb

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,4 +302,55 @@
302302
expect(client.service_url_base('Disputes'))
303303
.to eq('https://ca-test.adyen.com/ca/services/DisputesService')
304304
end
305+
306+
it 'raises FormatError on 400 response and checks content' do
307+
client = Adyen::Client.new(api_key: 'api_key', env: :test)
308+
mock_faraday_connection = double(Faraday::Connection)
309+
error_body = {
310+
status: 400,
311+
errorCode: "702",
312+
message: "Structure of CreateCheckoutSessionRequest contains the following unknown fields: [paymentMethod]",
313+
errorType: "validation"
314+
}
315+
mock_response = Faraday::Response.new(status: 400, body: error_body)
316+
317+
allow(Faraday).to receive(:new).and_return(mock_faraday_connection)
318+
allow(mock_faraday_connection).to receive_message_chain(:headers, :[]=)
319+
allow(mock_faraday_connection).to receive(:post).and_return(mock_response)
320+
321+
expect {
322+
client.checkout.payments_api.payments({})
323+
}.to raise_error(Adyen::FormatError) do |error|
324+
expect(error.code).to eq(400)
325+
expect(error.msg).to eq('Invalid format or fields')
326+
expect(error.response[:errorCode]).to eq('702')
327+
end
328+
end
329+
330+
it 'raises FormatError on 422 response and checks content' do
331+
client = Adyen::Client.new(api_key: 'api_key', env: :test)
332+
mock_faraday_connection = double(Faraday::Connection)
333+
error_body = {
334+
type: "https://docs.adyen.com/errors/validation",
335+
title: "The request is missing required fields or contains invalid data.",
336+
status: 422,
337+
detail: "It is mandatory to specify a legalEntityId when creating a new account holder.",
338+
invalidFields: [{ "name" => "legalEntityId", "message" => "legalEntityId is not provided" }],
339+
errorCode: "30_011"
340+
}
341+
mock_response = Faraday::Response.new(status: 422, body: error_body)
342+
343+
allow(Faraday).to receive(:new).and_return(mock_faraday_connection)
344+
allow(mock_faraday_connection).to receive_message_chain(:headers, :[]=)
345+
allow(mock_faraday_connection).to receive(:post).and_return(mock_response)
346+
347+
expect {
348+
client.checkout.payments_api.payments({})
349+
}.to raise_error(Adyen::ValidationError) do |error|
350+
expect(error.code).to eq(422)
351+
expect(error.msg).to eq('Validation error')
352+
expect(error.response[:errorCode]).to eq('30_011')
353+
end
354+
end
355+
305356
end

0 commit comments

Comments
 (0)