Description
AdyenException is not initialised correctly when the server responds with an error (validation or entity was not found)
Steps to reproduce
- Install SDK
- Initialize
Adyen\Client client for Legal Entity Management (LEM) services. Use test env.
- Initialize
Adyen\Service\LegalEntityManagement\TransferInstrumentsApi service. Use Client for LEM services.
- 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
- 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.
- The
Adyen\AdyenException class does not have the invalidFields property. (But according to documentation, it's a part of the exception: invalidFields)
Expected behavior
-
The Adyen\AdyenException initialized correctly and has set code, adyenErrorCode, pspReference, and status properties.
-
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)
-
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)
Adyen PHP API Library version
29.0.0 and 28.3.0
PHP version
8.4
Operating System
Linux
Additional context
No response
Description
AdyenException is not initialised correctly when the server responds with an error (validation or entity was not found)
Steps to reproduce
Adyen\Clientclient for Legal Entity Management (LEM) services. Usetestenv.Adyen\Service\LegalEntityManagement\TransferInstrumentsApiservice. Use Client for LEM services.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 actionActual behavior
Adyen\AdyenExceptionexception was thrown, and the exception's properties are not initialized correctly; they had the default values except for the message.The
messageproperty contains a JSON string with the response body.Adyen\AdyenExceptionclass does not have theinvalidFieldsproperty. (But according to documentation, it's a part of the exception: invalidFields)Expected behavior
The
Adyen\AdyenExceptioninitialized correctly and has setcode,adyenErrorCode,pspReference, andstatusproperties.The
Adyen\AdyenExceptionclass has theinvalidFieldsproperty, or as a possible solution, it would be nice to have a separate class with that property for the422errors. (For example,AdyenUnprocessableEntityException)Also, I have a question about the status code. Is it expected behavior of the API that for non-existent resources, it returns a
422status code instead of a404?Code snippet or screenshots (if applicable)
I did some research and found out that the
Adyen\HttpClient\CurlClientclass contains the following function, which is used to handle errors and throw theAdyenExceptionclass: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
messageproperty; instead, it contains thetitleproperty.In that case, the condition
isset($decodeResult['message']) && isset($decodeResult['errorCode'])will evaluate tofalse, the exception is not initialized correctly, and the secondthrowexecuted (with a message that contains a raw JSON string)Adyen PHP API Library version
29.0.0 and 28.3.0
PHP version
8.4
Operating System
Linux
Additional context
No response