Debug log updates, error message updates on curl failure

Config setup works with null data if no data is set.

Debugger is now launched in the AamzonIncentive class at the beginning
Debugger id/debug flag set is now one static call
Debugger methods all have proper PHPdoc documentationn added

Fixed Client/Curl error messages to include the original error message
too. Also made sure that a possible null endpoint is not throwing errors
This commit is contained in:
2021-10-20 09:36:18 +09:00
parent f3b17bbf4c
commit a56f33c81c
7 changed files with 151 additions and 79 deletions

View File

@@ -34,8 +34,6 @@ class AWS
public function __construct(Config $config)
{
$this->config = $config;
AmazonDebug::setFlag($config->getDebug());
AmazonDebug::setId();
AmazonDebug::writeLog([__METHOD__ => date('Y-m-d H:m:s.u')]);
}
@@ -154,7 +152,7 @@ class AWS
'Service' => $k_service_hexis,
]]);
$url = 'https://' . $endpoint . '/' . $service_operation;
$url = 'https://' . (string)$endpoint . '/' . $service_operation;
$headers = $this->buildHeaders($payload, $authorization_value, $date_time_string, $service_target);
return (new Client())->request($url, $headers, $payload);
}

View File

@@ -39,6 +39,7 @@ namespace Amazon;
use Amazon\AWS\AWS;
use Amazon\Config\Config;
use Amazon\Exceptions\AmazonErrors;
use Amazon\Debug\AmazonDebug;
class AmazonIncentives
{
@@ -65,6 +66,8 @@ class AmazonIncentives
// load AWS settings
// fail here if settings missing
$this->config = new Config($key, $secret, $partner, $endpoint, $currency, $debug);
// init debug
AmazonDebug::setDebug($this->config->getDebug());
}
// *********************************************************************

View File

@@ -79,19 +79,19 @@ class Client implements ClientInterface
case CURLE_COULDNT_CONNECT:
case CURLE_COULDNT_RESOLVE_HOST:
case CURLE_OPERATION_TIMEOUTED:
$msg = 'Could not connect to AWS (' . $url . '). Please check your '
. 'internet connection and try again.';
$message = 'Could not connect to AWS (' . $url . '). Please check your '
. 'internet connection and try again. [' . $message . ']';
break;
case CURLE_SSL_CACERT:
case CURLE_SSL_PEER_CERTIFICATE:
$msg = 'Could not verify AWS SSL certificate. Please make sure '
$message = 'Could not verify AWS SSL certificate. Please make sure '
. 'that your network is not intercepting certificates. '
. '(Try going to ' . $url . 'in your browser.) '
. 'If this problem persists,';
. '[' . $message . ']';
break;
case 0:
default:
$msg = 'Unexpected error communicating with AWS. ' . $message;
$message = 'Unexpected error communicating with AWS: ' . $message;
}
// throw an error like in the normal reqeust, but set to CURL error

View File

@@ -80,9 +80,9 @@ class Config implements ConfigInterface
}
/**
* @return string
* @return string|null
*/
public function getEndpoint(): string
public function getEndpoint(): ?string
{
return $this->endpoint;
}
@@ -100,9 +100,9 @@ class Config implements ConfigInterface
}
/**
* @return string
* @return string|null
*/
public function getAccessKey(): string
public function getAccessKey(): ?string
{
return $this->access_key;
}
@@ -119,9 +119,9 @@ class Config implements ConfigInterface
}
/**
* @return string
* @return string|null
*/
public function getSecret(): string
public function getSecret(): ?string
{
return $this->secret_key;
}
@@ -138,9 +138,9 @@ class Config implements ConfigInterface
}
/**
* @return string
* @return string|null
*/
public function getCurrency(): string
public function getCurrency(): ?string
{
return $this->currency;
}
@@ -158,9 +158,9 @@ class Config implements ConfigInterface
}
/**
* @return string
* @return string|null
*/
public function getPartner(): string
public function getPartner(): ?string
{
return $this->partner_id;
}
@@ -177,9 +177,9 @@ class Config implements ConfigInterface
}
/**
* @return bool
* @return bool|null
*/
public function getDebug(): bool
public function getDebug(): ?bool
{
return $this->debug;
}

View File

@@ -5,9 +5,9 @@ namespace Amazon\Config;
interface ConfigInterface
{
/**
* @return String
* @return string|null
*/
public function getEndpoint(): string;
public function getEndpoint(): ?string;
/**
* @param string $endpoint
@@ -16,9 +16,9 @@ interface ConfigInterface
public function setEndpoint(string $endpoint): ConfigInterface;
/**
* @return String
* @return string|null
*/
public function getAccessKey(): string;
public function getAccessKey(): ?string;
/**
* @param string $key
@@ -27,9 +27,9 @@ interface ConfigInterface
public function setAccessKey(string $key): ConfigInterface;
/**
* @return String
* @return string|null
*/
public function getSecret(): string;
public function getSecret(): ?string;
/**
* @param string $secret
@@ -38,9 +38,9 @@ interface ConfigInterface
public function setSecret(string $secret): ConfigInterface;
/**
* @return String
* @return string|null
*/
public function getCurrency(): string;
public function getCurrency(): ?string;
/**
* @param string $currency
@@ -49,9 +49,9 @@ interface ConfigInterface
public function setCurrency(string $currency): ConfigInterface;
/**
* @return String
* @return string|null
*/
public function getPartner(): string;
public function getPartner(): ?string;
/**
* @param string $partner
@@ -60,9 +60,9 @@ interface ConfigInterface
public function setPartner(string $partner): ConfigInterface;
/**
* @return bool
* @return bool|null
*/
public function getDebug(): bool;
public function getDebug(): ?bool;
/**
* @param bool $debug

View File

@@ -1,6 +1,7 @@
<?php
// simple write all into an array that we can poll in the return group
// to activate AmazonDebug::setDebug(true) must be called once
namespace Amazon\Debug;
@@ -10,11 +11,15 @@ class AmazonDebug
private static $debug = false;
private static $id = null;
public function __construct()
{
}
public static function setId(?string $id = null): void
/**
* set the ID for current run
* if debug is off, nothing will be set and id is null
* This is run on setFlag, if debug is true
*
* @param string|null $id If not set, will default to uniqid() call
* @return void
*/
private static function setId(?string $id = null): void
{
if (self::$debug === false) {
return;
@@ -25,24 +30,69 @@ class AmazonDebug
self::$id = $id;
}
public static function getId(): string
/**
* set the debug flag.
* This is automatically run in Amazon\AmazonIncentives::__construct
* No need to run manuall
*
* @param boolean $debug Can only be True or False
* @param string|null $id If not set, will default to uniqid() call
* @return void
*/
public static function setDebug(bool $debug, ?string $id = null): void
{
self::$debug = $debug;
if (self::$debug === false) {
return;
}
self::setId($id);
}
/**
* returns current debug flag status
*
* @return boolean True if debug is on, False if debug is off
*/
public static function getDebug(): bool
{
return self::$debug;
}
/**
* get the current set ID, can return null if debug is off
*
* @return string|null Current set ID for this log run
*/
public static function getId(): ?string
{
return self::$id;
}
public static function setFlag(bool $debug): void
{
self::$debug = $debug;
}
/**
* write a log entry
* Data is as array key -> value
* Will be pushed as new array entry int log
* Main key is the set Id for this run
*
* @param array $data Any array data to store in the log
* @return void
*/
public static function writeLog(array $data): void
{
if (self::$debug === false) {
return;
}
self::$log[self::$id][] = $data;
self::$log[self::getId()][] = $data;
}
/**
* get all logs written since first class run
* or get all log entries for given ID
*
* @param string|null $id If set returns only this id logs
* or empty array if not found
* @return array Always array, empty if not data or not found
*/
public static function getLog(?string $id = null): array
{
if ($id === null) {

View File

@@ -6,7 +6,7 @@
* write log as string from array data
* includes timestamp
*
* @param array $data
* @param array $data Debug log array data to add to the json string
* @return string
*/
function writeLog(array $data): string
@@ -17,6 +17,41 @@ function writeLog(array $data): string
]) . "\n";
}
/**
* translate the UTC amazon date string to Y-m-d H:i:s standard
*
* @param string $date A UTC string date from Amazon
* @return string
*/
function dateTr(string $date): string
{
return date('Y-m-d H:i:s', strtotime($date));
}
/**
* print exception string
*
* @param string $call_request Call request, eg buyGiftCard
* @param integer $error_code $e Exception error code
* @param array $error Array from the Exception message json string
* @param boolean $debug_print If we should show the debug log
* @return void
*/
function printException(
string $call_request,
int $error_code,
array $error,
bool $debug_print
): void {
print "AWS: " . $call_request . ": " . $error['status']
. " [" . $error_code . "]: "
. $error['code'] . " | " . $error['type']
. " | " . $error['message'];
if ($debug_print === true) {
print "<pre>" . print_r($error['log'][$error['log_id'] ?? ''] ?? [], true) . "</pre>";
}
}
// general auto loader
require 'autoloader.php';
// env file loader
@@ -76,6 +111,8 @@ AED for UAE
*/
$fp = fopen('log/debug.' . date('YmdHis') . '.log', 'w');
// run info test (prints ENV vars)
$run_info_test = false;
// run test to get funds info
@@ -83,7 +120,7 @@ $run_fund_test = true;
// run the normal get/cancel gift card tests
$run_gift_tests = true;
// run mock error check tests
$run_mocks = true;
$run_mocks = false;
// should we print debug info
$debug_print = false;
@@ -101,8 +138,6 @@ if ($run_info_test === true) {
print "<hr>";
}
$fp = fopen('log/debug.' . date('YmdHis') . '.log', 'w');
// check balance
if ($run_fund_test === true) {
try {
@@ -117,13 +152,7 @@ if ($run_fund_test === true) {
fwrite($fp, writeLog((array)$aws_test));
} catch (Exception $e) {
$error = Amazon\AmazonIncentives::decodeExceptionMessage($e->getMessage());
print "AWS: getAvailableFunds: " . $error['status']
. " [" . $e->getCode() . "]: "
. $error['code'] . " | " . $error['type']
. " | " . $error['message'] . ": ";
if ($debug_print === true) {
print "/<pre>" . print_r($error['log'][$error['log_id'] ?? ''] ?? [], true) . "</pre>";
}
printException('getAvailableFunds', $e->getCode(), $error, $debug_print);
fwrite($fp, writeLog($error));
};
print "<br>";
@@ -135,15 +164,19 @@ if ($run_fund_test === true) {
if ($run_gift_tests === true) {
// create card
$value = 1000;
$creation_request_id = '';
$gift_card_id = '';
try {
// we must be sure we pass FLOAT there
$aws_test = Amazon\AmazonIncentives::make()->buyGiftCard((float)$value);
$creation_request_id = $aws_test->getCreationRequestId();
$gift_card_id = $aws_test->getId();
$claim_code = $aws_test->getClaimCode();
$expiration_date = $aws_test->getExpirationDate();
$request_status = $aws_test->getStatus();
print "AWS: buyGiftCard: " . $request_status . ": "
. "creationRequestId: " . $creation_request_id . ", gcId: " . $gift_card_id . ", "
. "EXPIRE DATE: <b>" . dateTr($expiration_date) . "</b>, "
. "CLAIM CODE: <b>" . $claim_code . "</b>";
if ($debug_print === true) {
print "<pre>" . print_r($aws_test, true) . "</pre>";
@@ -151,13 +184,7 @@ if ($run_gift_tests === true) {
fwrite($fp, writeLog((array)$aws_test));
} catch (\Exception $e) {
$error = Amazon\AmazonIncentives::decodeExceptionMessage($e->getMessage());
print "AWS: buyGiftCard: " . $error['status']
. " [" . $e->getCode() . "]: "
. $error['code'] . " | " . $error['type']
. " | " . $error['message'] . ": ";
if ($debug_print === true) {
print "/<pre>" . print_r($error['log'][$error['log_id'] ?? ''] ?? [], true) . "</pre>";
}
printException('buyGiftCard', $e->getCode(), $error, $debug_print);
fwrite($fp, writeLog($error));
}
print "<br>";
@@ -177,9 +204,9 @@ if ($run_gift_tests === true) {
print "AWS: cancelGiftCard: " . $error['status']
. " [" . $e->getCode() . "]: "
. $error['code'] . " | " . $error['type']
. " | " . $error['message'] . ": ";
. " | " . $error['message'];
if ($debug_print === true) {
print "/<pre>" . print_r($error['log'][$error['log_id'] ?? ''] ?? [], true) . "</pre>";
print "<pre>" . print_r($error['log'][$error['log_id'] ?? ''] ?? [], true) . "</pre>";
}
fwrite($fp, writeLog($error));
}
@@ -192,9 +219,11 @@ if ($run_gift_tests === true) {
$creation_request_id = $aws_test->getCreationRequestId();
$gift_card_id = $aws_test->getId();
$claim_code = $aws_test->getClaimCode();
$expiration_date = $aws_test->getExpirationDate();
$request_status = $aws_test->getStatus();
print "AWS: buyGiftCard: CODE A: " . $request_status . ": "
. "creationRequestId: " . $creation_request_id . ", gcId: " . $gift_card_id . ", "
. "EXPIRE DATE: <b>" . dateTr($expiration_date) . "</b>, "
. "CLAIM CODE: <b>" . $claim_code . "</b>";
if ($debug_print === true) {
print "<pre>" . print_r($aws_test, true) . "</pre>";
@@ -202,13 +231,7 @@ if ($run_gift_tests === true) {
fwrite($fp, writeLog((array)$aws_test));
} catch (\Exception $e) {
$error = Amazon\AmazonIncentives::decodeExceptionMessage($e->getMessage());
print "AWS: cancelGiftCard: " . $error['status']
. " [" . $e->getCode() . "]: "
. $error['code'] . " | " . $error['type']
. " | " . $error['message'] . ": ";
if ($debug_print === true) {
print "/<pre>" . print_r($error['log'][$error['log_id'] ?? ''] ?? [], true) . "</pre>";
}
printException('cancelGiftCard', $e->getCode(), $error, $debug_print);
fwrite($fp, writeLog($error));
}
print "<br>";
@@ -216,8 +239,11 @@ if ($run_gift_tests === true) {
try {
$aws_test = Amazon\AmazonIncentives::make()->buyGiftCard((float)$value, $creation_request_id);
$request_status = $aws_test->getStatus();
// same?
$expiration_date = $aws_test->getExpirationDate();
print "AWS: buyGiftCard: SAME CODE A AGAIN: " . $request_status . ": "
. "creationRequestId: " . $creation_request_id . ", gcId: " . $gift_card_id . ", "
. "EXPIRE DATE: <b>" . dateTr($expiration_date) . "</b>, "
. "CLAIM CODE: <b>" . $claim_code . "</b>";
if ($debug_print === true) {
print "<pre>" . print_r($aws_test, true) . "</pre>";
@@ -225,13 +251,7 @@ if ($run_gift_tests === true) {
fwrite($fp, writeLog((array)$aws_test));
} catch (\Exception $e) {
$error = Amazon\AmazonIncentives::decodeExceptionMessage($e->getMessage());
print "AWS: cancelGiftCard: " . $error['status']
. " [" . $e->getCode() . "]: "
. $error['code'] . " | " . $error['type']
. " | " . $error['message'] . ": ";
if ($debug_print === true) {
print "/<pre>" . print_r($error['log'][$error['log_id'] ?? ''] ?? [], true) . "</pre>";
}
printException('buyGiftCard', $e->getCode(), $error, $debug_print);
fwrite($fp, writeLog($error));
}
print "<br>";
@@ -246,6 +266,7 @@ if ($mock_debug === true) {
$mock_value = 500;
$mock['F0000'] = [ 'ret' => '', 'st' => 'SUCCESS']; // success mock
$mock['F1000'] = [ 'ret' => 'F100', 'st' => 'FAILURE']; // SimpleAmountIsNull, etc
$mock['F2003'] = [ 'ret' => 'F200', 'st' => 'FAILURE']; // InvalidAmountInput
$mock['F2004'] = [ 'ret' => 'F200', 'st' => 'FAILURE']; // InvalidAmountValue
$mock['F2005'] = [ 'ret' => 'F200', 'st' => 'FAILURE']; // InvalidCurrencyCodeInput
@@ -295,7 +316,7 @@ if ($mock_debug === true) {
print $mock_failure;
}
if ($mock_debug === true) {
print "/<pre>" . print_r($error['log'][$error['log_id'] ?? ''] ?? [], true) . "</pre>";
print "<pre>" . print_r($error['log'][$error['log_id'] ?? ''] ?? [], true) . "</pre>";
}
fwrite($fp, writeLog($error));
}