Skip to content

Commit 69aa873

Browse files
committed
Format error message using response payload
1 parent d172442 commit 69aa873

File tree

2 files changed

+66
-20
lines changed

2 files changed

+66
-20
lines changed

lib/adyen/client.rb

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,7 @@ def service_url_base(service)
113113

114114
if @live_url_prefix.nil? && (@env == :live) && supports_live_url_prefix
115115
raise ArgumentError,
116-
"Please set Client.live_url_prefix to the portion \
117-
of your merchant-specific URL prior to '-[service]-live.adyenpayments.com'"
116+
"Please set Client.live_url_prefix to the portion \n of your merchant-specific URL prior to '-[service]-live.adyenpayments.com'"
118117
end
119118

120119
if @env == :live && supports_live_url_prefix
@@ -215,19 +214,21 @@ def call_adyen_api(service, action, request_data, headers, version, _with_applic
215214
end
216215
# check for API errors
217216
case response.status
218-
when 400
219-
raise Adyen::FormatError.new('Invalid format or fields', request_data, response.body)
220-
when 401
221-
raise Adyen::AuthenticationError.new(
222-
'Invalid API authentication; https://docs.adyen.com/user-management/how-to-get-the-api-key', request_data
223-
)
224-
when 403
225-
raise Adyen::PermissionError.new('Missing user permissions; https://docs.adyen.com/user-management/user-roles',
226-
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 - status #{response.status}", request_data, response.body)
217+
when 400
218+
full_message = build_error_message(response.body, 'Invalid format or fields')
219+
raise Adyen::FormatError.new(full_message, request_data, response.body)
220+
when 401
221+
full_message = build_error_message(response.body, 'Authentication error')
222+
raise Adyen::AuthenticationError.new(full_message, request_data)
223+
when 403
224+
full_message = build_error_message(response.body, 'Authorisation error')
225+
raise Adyen::PermissionError.new(full_message, request_data, response.body)
226+
when 422
227+
full_message = build_error_message(response.body, 'Validation error')
228+
raise Adyen::ValidationError.new(full_message, request_data, response.body)
229+
when 500..599
230+
full_message = build_error_message(response.body, 'Internal server error')
231+
raise Adyen::ServerError.new(full_message, request_data, response.body)
231232
end
232233

233234
# delete has no response.body (unless it throws an error)
@@ -352,6 +353,25 @@ def validate_auth_type(service, request_data)
352353
'Checkout service requires API-key or oauth_token'
353354
end
354355
end
356+
357+
# build the error message from the response payload
358+
def build_error_message(response_body, default_message)
359+
full_message = default_message
360+
begin
361+
error_details = response_body
362+
# check different attributes to support both RFC 7807 and legacy models
363+
message = error_details[:detail] || error_details[:message]
364+
error_code = error_details[:errorCode]
365+
if message && error_code
366+
full_message = "#{message} ErrorCode: #{error_code}"
367+
elsif message
368+
full_message = message
369+
end
370+
rescue JSON::ParserError
371+
# If the body isn't valid JSON, we fall back to the default message
372+
end
373+
full_message
374+
end
355375
end
356376
end
357377
# rubocop:enable all

spec/client_spec.rb

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -322,12 +322,12 @@
322322
client.checkout.payments_api.payments({})
323323
}.to raise_error(Adyen::FormatError) do |error|
324324
expect(error.code).to eq(400)
325-
expect(error.msg).to eq('Invalid format or fields')
325+
expect(error.msg).to eq('Structure of CreateCheckoutSessionRequest contains the following unknown fields: [paymentMethod] ErrorCode: 702')
326326
expect(error.response[:errorCode]).to eq('702')
327327
end
328328
end
329329

330-
it 'raises ValidationError on 422 response and checks content' do
330+
it 'raises ValidationError on 422 response with RestServiceError (based on RFC 7807)' do
331331
client = Adyen::Client.new(api_key: 'api_key', env: :test)
332332
mock_faraday_connection = double(Faraday::Connection)
333333
error_body = {
@@ -348,8 +348,34 @@
348348
client.checkout.payments_api.payments({})
349349
}.to raise_error(Adyen::ValidationError) do |error|
350350
expect(error.code).to eq(422)
351-
expect(error.msg).to eq('Validation error')
351+
expect(error.msg).to eq('It is mandatory to specify a legalEntityId when creating a new account holder. ErrorCode: 30_011')
352352
expect(error.response[:errorCode]).to eq('30_011')
353+
expect(error.response[:invalidFields]).to have_attributes(size: 1)
354+
end
355+
end
356+
357+
it 'raises ValidationError on 422 response with ServiceError (legacy)' do
358+
client = Adyen::Client.new(api_key: 'api_key', env: :test)
359+
mock_faraday_connection = double(Faraday::Connection)
360+
error_body = {
361+
status: 422,
362+
errorCode: "14_030",
363+
message: "Return URL is missing.",
364+
errorType: "validation",
365+
pspReference: "8816118280275544"
366+
}
367+
mock_response = Faraday::Response.new(status: 422, body: error_body)
368+
369+
allow(Faraday).to receive(:new).and_return(mock_faraday_connection)
370+
allow(mock_faraday_connection).to receive_message_chain(:headers, :[]=)
371+
allow(mock_faraday_connection).to receive(:post).and_return(mock_response)
372+
373+
expect {
374+
client.checkout.payments_api.payments({})
375+
}.to raise_error(Adyen::ValidationError) do |error|
376+
expect(error.code).to eq(422)
377+
expect(error.msg).to eq('Return URL is missing. ErrorCode: 14_030')
378+
expect(error.response[:errorCode]).to eq('14_030')
353379
end
354380
end
355381

@@ -359,7 +385,7 @@
359385
error_body = {
360386
status: 500,
361387
errorCode: "999",
362-
message: "Unexpected error",
388+
message: "Unexpected error.",
363389
errorType: "server error"
364390
}
365391
mock_response = Faraday::Response.new(status: 500, body: error_body)
@@ -372,7 +398,7 @@
372398
client.checkout.payments_api.payments({})
373399
}.to raise_error(Adyen::ServerError) do |error|
374400
expect(error.code).to eq(500)
375-
expect(error.msg).to eq('Internal server error - status 500')
401+
expect(error.msg).to eq('Unexpected error. ErrorCode: 999')
376402
end
377403
end
378404
end

0 commit comments

Comments
 (0)