Add README file, add status to exception return

Because status RESEND/FAILURE has to be checked we also add this to the
Exception return json string.
Also handle unset errors (eg when we get an Rate Limit error)

Move the _ENV check into mtheod

test now has some basic mock tests
This commit is contained in:
2021-10-19 09:39:14 +09:00
parent b00d545a10
commit 71abd4d06e
6 changed files with 193 additions and 37 deletions

View File

@@ -3,6 +3,7 @@
namespace Amazon\Client;
use Amazon\Exceptions\AmazonErrors;
use Amazon\Debug\AmazonDebug;
class Client implements ClientInterface
{
@@ -36,11 +37,15 @@ class Client implements ClientInterface
if (curl_getinfo($handle, CURLINFO_HTTP_CODE) !== self::HTTP_OK) {
$err = curl_errno($handle);
AmazonDebug::writeLog(['CURL_REQUEST_RESULT' => $result]);
// extract all the error codes from Amazon
$error_code = json_decode($result)->errorCode;
$error_type = json_decode($result)->errorType;
$message = json_decode($result)->message;
$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';
throw AmazonErrors::getError(
$error_status,
$error_code,
$error_type,
$message,

View File

@@ -44,12 +44,39 @@ class Config implements ConfigInterface
?string $currency,
?bool $debug,
) {
$this->setAccessKey($key ?: $_ENV['AWS_GIFT_CARD_KEY'] ?? '');
$this->setSecret($secret ?: $_ENV['AWS_GIFT_CARD_SECRET'] ?? '');
$this->setPartner($partner ?: $_ENV['AWS_GIFT_CARD_PARTNER_ID'] ?? '');
$this->setEndpoint($endpoint ?: $_ENV['AWS_GIFT_CARD_ENDPOINT'] ?? '');
$this->setCurrency($currency ?: $_ENV['AWS_GIFT_CARD_CURRENCY'] ?? '');
$this->setDebug($debug ?: (!empty($_ENV['AWS_DEBUG']) ? true : false));
$this->setAccessKey($key ?: $this->parseEnv('AWS_GIFT_CARD_KEY'));
$this->setSecret($secret ?: $this->parseEnv('AWS_GIFT_CARD_SECRET'));
$this->setPartner($partner ?: $this->parseEnv('AWS_GIFT_CARD_PARTNER_ID'));
$this->setEndpoint($endpoint ?: $this->parseEnv('AWS_GIFT_CARD_ENDPOINT'));
$this->setCurrency($currency ?: $this->parseEnv('AWS_GIFT_CARD_CURRENCY'));
$this->setDebug($debug ?: $this->parseEnv('AWS_DEBUG'));
}
/**
* string key to search, returns entry from _ENV
* if not matchin key, returns empty
*
* @param string $key To search in _ENV array
* @return string|bool Returns either string or true/false (DEBUG flag)
*/
private function parseEnv(string $key)
{
$return = '';
switch ($key) {
case 'AWS_DEBUG':
$return = !empty($_ENV['AWS_DEBUG']) ? true : false;
break;
case 'AWS_GIFT_CARD_KEY':
case 'AWS_GIFT_CARD_SECRET':
case 'AWS_GIFT_CARD_PARTNER_ID':
case 'AWS_GIFT_CARD_ENDPOINT':
case 'AWS_GIFT_CARD_CURRENCY':
$return = $_ENV[$key] ?? '';
break;
default:
break;
}
return $return;
}
/**

View File

@@ -58,6 +58,17 @@ interface ConfigInterface
* @return $this
*/
public function setPartner(string $partner): ConfigInterface;
/**
* @return bool
*/
public function getDebug(): bool;
/**
* @param bool $debug
* @return $this
*/
public function setDebug(bool $debug): ConfigInterface;
}
// __END__

View File

@@ -7,13 +7,15 @@ use RuntimeException;
class AmazonErrors extends RuntimeException
{
/**
* @param string $error_code errorCode from Amazon
* @param string $error_type errorType from Amazon
* @param string $error_status agcodResponse->status from Amazon
* @param string $error_code errorCode from Amazon
* @param string $error_type errorType from Amazon
* @param string $message
* @param string $_error_code
* @return AmazonErrors
*/
public static function getError(
string $error_status,
string $error_code,
string $error_type,
string $message,
@@ -22,6 +24,7 @@ class AmazonErrors extends RuntimeException
// NOTE: if xdebug.show_exception_trace is set to 1 this will print ERRORS
return new static(
json_encode([
'status' => $error_status,
'code' => $error_code,
'type' => $error_type,
'message' => $message,