Skip to content

[Bug]: Incorrect initialisation of AdyenException #891

Description

@yamotu

Description

AdyenException is not initialised correctly when the server responds with an error (validation or entity was not found)

Steps to reproduce

  1. Install SDK
  2. Initialize Adyen\Client client for Legal Entity Management (LEM) services. Use test env.
  3. Initialize Adyen\Service\LegalEntityManagement\TransferInstrumentsApi service. Use Client for LEM services.
  4. Call the Adyen\Service\LegalEntityManagement\TransferInstrumentsApi::getTransferInstrument() service method with a non-existent transfer instrument id. (would say: SE322KH223222F5GXZFNM3BGP)

I also had the same behaviour for the Adyen\Service\BalancePlatform\AccountHoldersApi::getAccountHolder() service action

Actual behavior

  1. The Adyen\AdyenException exception was thrown, and the exception's properties are not initialized correctly; they had the default values except for the message.
    The message property contains a JSON string with the response body.
Image
  1. The Adyen\AdyenException class does not have the invalidFields property. (But according to documentation, it's a part of the exception: invalidFields)

Expected behavior

  1. The Adyen\AdyenException initialized correctly and has set code, adyenErrorCode, pspReference, and status properties.

  2. The Adyen\AdyenException class has the invalidFields property, or as a possible solution, it would be nice to have a separate class with that property for the 422 errors. (For example, AdyenUnprocessableEntityException)

  3. Also, I have a question about the status code. Is it expected behavior of the API that for non-existent resources, it returns a 422 status code instead of a 404?

Code snippet or screenshots (if applicable)

I did some research and found out that the Adyen\HttpClient\CurlClient class contains the following function, which is used to handle errors and throw the AdyenException class:

protected function handleResultError($result)
    {
        $decodeResult = json_decode($result, true);
        if (isset($decodeResult['message']) && isset($decodeResult['errorCode'])) {
            throw new AdyenException(
                $decodeResult['message'],
                $decodeResult['status'],
                null,
                $decodeResult['status'],
                $decodeResult['errorType'],
                $decodeResult['pspReference'] ?? null,
                $decodeResult['errorCode']
            );
        }
        throw new AdyenException($result);
    }

Response from the server(API):

{
  "type" : "https://docs.adyen.com/errors/not-found",
  "title" : "Entity was not found",
  "status" : 422,
  "requestId" : "NO_PSP_REF_1111111111111111",
  "invalidFields" : [ {
    "name" : "id",
    "message" : "id is invalid"
  } ],
  "errorCode" : "30_112"
}

The reason:

The response from the server does not contain the message property; instead, it contains the title property.
In that case, the condition isset($decodeResult['message']) && isset($decodeResult['errorCode']) will evaluate to false, the exception is not initialized correctly, and the second throw executed (with a message that contains a raw JSON string)

Image

Adyen PHP API Library version

29.0.0 and 28.3.0

PHP version

8.4

Operating System

Linux

Additional context

No response

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions