From a56f33c81cf394d88e17b2682911e07b7add0d84 Mon Sep 17 00:00:00 2001 From: Clemens Schwaighofer Date: Wed, 20 Oct 2021 09:36:18 +0900 Subject: [PATCH] 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 --- src/Amazon/AWS/AWS.php | 4 +- src/Amazon/AmazonIncentives.php | 3 + src/Amazon/Client/Client.php | 10 +-- src/Amazon/Config/Config.php | 24 +++---- src/Amazon/Config/ConfigInterface.php | 24 +++---- src/Amazon/Debug/AmazonDebug.php | 74 ++++++++++++++++++---- test/aws_gift_card_tests.php | 91 ++++++++++++++++----------- 7 files changed, 151 insertions(+), 79 deletions(-) diff --git a/src/Amazon/AWS/AWS.php b/src/Amazon/AWS/AWS.php index 703447b..2fa5b7a 100644 --- a/src/Amazon/AWS/AWS.php +++ b/src/Amazon/AWS/AWS.php @@ -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); } diff --git a/src/Amazon/AmazonIncentives.php b/src/Amazon/AmazonIncentives.php index d7c8217..43bcc0d 100644 --- a/src/Amazon/AmazonIncentives.php +++ b/src/Amazon/AmazonIncentives.php @@ -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()); } // ********************************************************************* diff --git a/src/Amazon/Client/Client.php b/src/Amazon/Client/Client.php index e44201f..b5ff2bc 100644 --- a/src/Amazon/Client/Client.php +++ b/src/Amazon/Client/Client.php @@ -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 diff --git a/src/Amazon/Config/Config.php b/src/Amazon/Config/Config.php index 443f222..d99883a 100644 --- a/src/Amazon/Config/Config.php +++ b/src/Amazon/Config/Config.php @@ -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; } diff --git a/src/Amazon/Config/ConfigInterface.php b/src/Amazon/Config/ConfigInterface.php index 8410663..0403148 100644 --- a/src/Amazon/Config/ConfigInterface.php +++ b/src/Amazon/Config/ConfigInterface.php @@ -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 diff --git a/src/Amazon/Debug/AmazonDebug.php b/src/Amazon/Debug/AmazonDebug.php index 207a322..9323c15 100644 --- a/src/Amazon/Debug/AmazonDebug.php +++ b/src/Amazon/Debug/AmazonDebug.php @@ -1,6 +1,7 @@ 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) { diff --git a/test/aws_gift_card_tests.php b/test/aws_gift_card_tests.php index 63e4252..0c31bd7 100644 --- a/test/aws_gift_card_tests.php +++ b/test/aws_gift_card_tests.php @@ -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 "
" . print_r($error['log'][$error['log_id'] ?? ''] ?? [], true) . "
"; + } +} + // 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 "
"; } -$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 "/
" . print_r($error['log'][$error['log_id'] ?? ''] ?? [], true) . "
"; - } + printException('getAvailableFunds', $e->getCode(), $error, $debug_print); fwrite($fp, writeLog($error)); }; print "
"; @@ -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: " . dateTr($expiration_date) . ", " . "CLAIM CODE: " . $claim_code . ""; if ($debug_print === true) { print "
" . print_r($aws_test, true) . "
"; @@ -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 "/
" . print_r($error['log'][$error['log_id'] ?? ''] ?? [], true) . "
"; - } + printException('buyGiftCard', $e->getCode(), $error, $debug_print); fwrite($fp, writeLog($error)); } print "
"; @@ -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 "/
" . print_r($error['log'][$error['log_id'] ?? ''] ?? [], true) . "
"; + print "
" . print_r($error['log'][$error['log_id'] ?? ''] ?? [], true) . "
"; } 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: " . dateTr($expiration_date) . ", " . "CLAIM CODE: " . $claim_code . ""; if ($debug_print === true) { print "
" . print_r($aws_test, true) . "
"; @@ -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 "/
" . print_r($error['log'][$error['log_id'] ?? ''] ?? [], true) . "
"; - } + printException('cancelGiftCard', $e->getCode(), $error, $debug_print); fwrite($fp, writeLog($error)); } print "
"; @@ -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: " . dateTr($expiration_date) . ", " . "CLAIM CODE: " . $claim_code . ""; if ($debug_print === true) { print "
" . print_r($aws_test, true) . "
"; @@ -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 "/
" . print_r($error['log'][$error['log_id'] ?? ''] ?? [], true) . "
"; - } + printException('buyGiftCard', $e->getCode(), $error, $debug_print); fwrite($fp, writeLog($error)); } print "
"; @@ -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 "/
" . print_r($error['log'][$error['log_id'] ?? ''] ?? [], true) . "
"; + print "
" . print_r($error['log'][$error['log_id'] ?? ''] ?? [], true) . "
"; } fwrite($fp, writeLog($error)); }