@@ -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
356376end
357377# rubocop:enable all
0 commit comments