Update decodeErrorMessage, Client error exception

If the exception json is not a json, return the array with message set
to the original string.

Updated the Client error result:
If Rate Exceeded error, set special T001 with RESEND status
Else set normal and ifn othing set to E999 with FAILURE
This commit is contained in:
2021-10-19 10:52:04 +09:00
parent 71abd4d06e
commit 7b90cd862d
4 changed files with 194 additions and 98 deletions

View File

@@ -81,21 +81,6 @@ class AmazonIncentives
// PUBLIC METHODS
// *********************************************************************
// public function activateGiftCard(): array
// {
// return [];
// }
// public function deactivateGiftCard(string $card_id): array
// {
// return [];
// }
// public function activationStatusCheck(string $card_id): array
// {
// return [];
// }
/**
* @param float $value
* @param string $creation_request_id AWS creationRequestId
@@ -161,8 +146,21 @@ class AmazonIncentives
*/
public static function decodeExceptionMessage(string $message): array
{
return json_decode($message, true);
$message_ar = json_decode($message, true);
// if we have an error, build empty block and only fill message
if (json_last_error()) {
$message_ar = [
'status' => '',
'code' => '',
'type' => '',
'message' => $message,
'log_id' => '',
'log' => []
];
}
return $message_ar;
}
// *********************************************************************
// PUBLIC TEST METHODS
// *********************************************************************

View File

@@ -40,10 +40,20 @@ class Client implements ClientInterface
AmazonDebug::writeLog(['CURL_REQUEST_RESULT' => $result]);
// extract all the error codes from Amazon
$result_ar = json_decode($result, true);
$error_status = $result_ar['agcodResponse']['status'] ?? 'FAILURE';
$error_code = $result_ar['errorCode'] ?? 'E999';
$error_type = $result_ar['errorType'] ?? 'OtherUnknownError';
$message = $result_ar['message'] ?? 'Unknown error occured';
// if message is 'Rate exceeded', set different error
if (($result_ar['message'] ?? '') == 'Rate exceeded') {
$error_status = 'RESEND';
$error_code = 'T001';
$error_type = 'RateExceeded';
$message = $result_ar['message'];
} else {
// for all other error messages
$error_status = $result_ar['agcodResponse']['status'] ?? 'FAILURE';
$error_code = $result_ar['errorCode'] ?? 'E999';
$error_type = $result_ar['errorType'] ?? 'OtherUnknownError';
$message = $result_ar['message'] ?? 'Unknown error occured';
}
// throw Error here with all codes
throw AmazonErrors::getError(
$error_status,
$error_code,
@@ -84,7 +94,14 @@ class Client implements ClientInterface
$msg = 'Unexpected error communicating with AWS. ' . $message;
}
throw new \RuntimeException($msg);
// throw an error like in the normal reqeust, but set to CURL error
throw AmazonErrors::getError(
'FAILURE',
'C001',
'CurlError',
$message,
$errno
);
}
}