From 4fae1d71a13eed963e221d6343371612f4cc6fb4 Mon Sep 17 00:00:00 2001 From: Kevin Hellemun Date: Thu, 21 Sep 2017 10:00:55 +0200 Subject: [PATCH 01/18] Base excaption handler done. --- src/Exception/ApiException.php | 26 +++++++++++++- src/Exception/BadRequestException.php | 9 +++++ src/Exception/ExceptionFactory.php | 38 ++++++++++++++++++++ src/Exception/ForbiddenException.php | 9 +++++ src/Exception/MethodNotAllowedException.php | 9 +++++ src/Exception/NotFoundException.php | 9 +++++ src/Exception/PleaseContactBunqException.php | 9 +++++ src/Exception/ToManyRequestsException.php | 15 ++++++++ src/Exception/UnauthorizedException.php | 9 +++++ src/Exception/UnknownApiErrorException.php | 9 +++++ src/Http/Handler/ResponseHandlerError.php | 5 +-- 11 files changed, 144 insertions(+), 3 deletions(-) create mode 100644 src/Exception/BadRequestException.php create mode 100644 src/Exception/ExceptionFactory.php create mode 100644 src/Exception/ForbiddenException.php create mode 100644 src/Exception/MethodNotAllowedException.php create mode 100644 src/Exception/NotFoundException.php create mode 100644 src/Exception/PleaseContactBunqException.php create mode 100644 src/Exception/ToManyRequestsException.php create mode 100644 src/Exception/UnauthorizedException.php create mode 100644 src/Exception/UnknownApiErrorException.php diff --git a/src/Exception/ApiException.php b/src/Exception/ApiException.php index c9e7ed67..5abb9f24 100644 --- a/src/Exception/ApiException.php +++ b/src/Exception/ApiException.php @@ -3,6 +3,30 @@ /** */ -class ApiException extends BunqException +class ApiException extends \Exception { + /** + * @var int + */ + private $responseCode; + + /** + * ApiException constructor. + * + * @param int $responseCode + */ + public function __construct($message, $args = []) + { + $this->responseCode = $args[0]; + + parent::__construct(vsprintf($message, $args)); + } + + /** + * @return int + */ + public function getResponseCode() + { + return $this->responseCode; + } } diff --git a/src/Exception/BadRequestException.php b/src/Exception/BadRequestException.php new file mode 100644 index 00000000..0151c8b6 --- /dev/null +++ b/src/Exception/BadRequestException.php @@ -0,0 +1,9 @@ +getStatusCode(), @@ -68,7 +69,7 @@ public function execute(ResponseInterface $response) ] ); } else { - throw new ApiException( + throw ExceptionFactory::createExceptionForResponse( self::ERROR_UNKNOWN, [ $response->getStatusCode(), From 5d7e1fb87397d731959de5face937910e2367ad1 Mon Sep 17 00:00:00 2001 From: Kevin Hellemun Date: Thu, 21 Sep 2017 10:02:09 +0200 Subject: [PATCH 02/18] Modified test. --- tests/BunqSdkTestBase.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/tests/BunqSdkTestBase.php b/tests/BunqSdkTestBase.php index 027d0612..c921e4de 100644 --- a/tests/BunqSdkTestBase.php +++ b/tests/BunqSdkTestBase.php @@ -42,9 +42,7 @@ protected static function ensureApiContextValid() { try { $apiContext = ApiContext::restore(static::FILENAME_CONTEXT_CONFIG); - User::listing($apiContext); - } catch (ApiException $exception) { - $apiContext = self::createApiContext(); + $apiContext->ensureSessionActive(); } catch (BunqException $exception) { $apiContext = self::createApiContext(); } From 70a32934034ad86c0ba7eaaf3415cb2d828b50ca Mon Sep 17 00:00:00 2001 From: Kevin Hellemun Date: Thu, 21 Sep 2017 10:07:00 +0200 Subject: [PATCH 03/18] Some cleaning :clap:. --- src/Exception/BadRequestException.php | 4 ++-- src/Exception/ExceptionFactory.php | 20 +++++++++++++++++--- src/Exception/ForbiddenException.php | 4 ++-- src/Exception/MethodNotAllowedException.php | 4 ++-- src/Exception/NotFoundException.php | 4 ++-- src/Exception/PleaseContactBunqException.php | 4 ++-- src/Exception/ToManyRequestsException.php | 10 ++-------- src/Exception/UnauthorizedException.php | 4 ++-- src/Exception/UnknownApiErrorException.php | 4 ++-- 9 files changed, 33 insertions(+), 25 deletions(-) diff --git a/src/Exception/BadRequestException.php b/src/Exception/BadRequestException.php index 0151c8b6..ba8a9ffe 100644 --- a/src/Exception/BadRequestException.php +++ b/src/Exception/BadRequestException.php @@ -1,8 +1,8 @@ Date: Thu, 21 Sep 2017 10:09:11 +0200 Subject: [PATCH 04/18] More cleaning :clap:. --- src/Exception/ApiException.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/Exception/ApiException.php b/src/Exception/ApiException.php index 5abb9f24..c9a12626 100644 --- a/src/Exception/ApiException.php +++ b/src/Exception/ApiException.php @@ -5,6 +5,11 @@ */ class ApiException extends \Exception { + /** + * The first item index in an array. + */ + const INDEX_FIRST = 0; + /** * @var int */ @@ -17,7 +22,7 @@ class ApiException extends \Exception */ public function __construct($message, $args = []) { - $this->responseCode = $args[0]; + $this->responseCode = $args[self::INDEX_FIRST]; parent::__construct(vsprintf($message, $args)); } From 7599e8f1f81da79ed7b987a7f21f4dd1244049e7 Mon Sep 17 00:00:00 2001 From: Kevin Hellemun Date: Thu, 21 Sep 2017 13:23:25 +0200 Subject: [PATCH 05/18] Rewritten ResponseErrorHandler. --- src/Exception/ApiException.php | 10 ++- src/Exception/BunqException.php | 6 +- src/Exception/ExceptionFactory.php | 45 +++++++--- src/Exception/ToManyRequestsException.php | 9 -- src/Exception/TooManyRequestsException.php | 9 ++ src/Http/ApiClient.php | 2 +- src/Http/Handler/ResponseHandlerError.php | 97 +++++++++++++++------- 7 files changed, 117 insertions(+), 61 deletions(-) delete mode 100644 src/Exception/ToManyRequestsException.php create mode 100644 src/Exception/TooManyRequestsException.php diff --git a/src/Exception/ApiException.php b/src/Exception/ApiException.php index c9a12626..4aa6c0ba 100644 --- a/src/Exception/ApiException.php +++ b/src/Exception/ApiException.php @@ -1,9 +1,11 @@ responseCode = $args[self::INDEX_FIRST]; + $this->responseCode = $responseCode; - parent::__construct(vsprintf($message, $args)); + parent::__construct($message); } /** diff --git a/src/Exception/BunqException.php b/src/Exception/BunqException.php index baecca90..7343fecd 100644 --- a/src/Exception/BunqException.php +++ b/src/Exception/BunqException.php @@ -7,10 +7,10 @@ class BunqException extends \Exception { /** * @param string $message - * @param string[] $args + * @param string[] $responseCode */ - public function __construct($message, $args = []) + public function __construct($message, $responseCode = []) { - parent::__construct(vsprintf($message, $args)); + parent::__construct(vsprintf($message, $responseCode)); } } diff --git a/src/Exception/ExceptionFactory.php b/src/Exception/ExceptionFactory.php index 3a441749..808bf511 100644 --- a/src/Exception/ExceptionFactory.php +++ b/src/Exception/ExceptionFactory.php @@ -16,37 +16,56 @@ class ExceptionFactory const HTTP_RESPONSE_CODE_TOO_MANY_REQUESTS = 429; const HTTP_RESPONSE_CODE_INTERNAL_SERVER_ERROR = 500; + const FORMAT_RESPONSE_CODE_LINE = 'HTTP Response Code: %s'; + const GLUE_ERROR_MESSAGES = "\n"; + /** * The first item index in an array. */ const INDEX_FIRST = 0; /** - * @param string $message - * @param array $args + * @param array $messages + * @param int $responseCode * - * @return BadRequestException|ForbiddenException|MethodNotAllowedException|NotFoundException|PleaseContactBunqException|ToManyRequestsException|UnauthorizedException|UnknownApiErrorException + * @return ApiException */ - public static function createExceptionForResponse($message, $args = []) + public static function createExceptionForResponse($messages, $responseCode) { - switch ($args[self::INDEX_FIRST]) + var_dump($messages); + $errorMessage = static::generateMessageError($responseCode, $messages); + var_dump($errorMessage); + + switch ($responseCode) { case self::HTTP_RESPONSE_CODE_BAD_REQUEST: - return new BadRequestException($message, $args); + return new BadRequestException($errorMessage, $responseCode); case self::HTTP_RESPONSE_CODE_UNAUTHORIZED: - return new UnauthorizedException($message, $args); + return new UnauthorizedException($errorMessage, $responseCode); case self::HTTP_RESPONSE_CODE_FORBIDDEN: - return new ForbiddenException($message, $args); + return new ForbiddenException($errorMessage, $responseCode); case self::HTTP_RESPONSE_CODE_NOT_FOUND: - return new NotFoundException($message, $args); + return new NotFoundException($errorMessage, $responseCode); case self::HTTP_RESPONSE_CODE_METHOD_NOT_ALLOWED: - return new MethodNotAllowedException($message, $args); + return new MethodNotAllowedException($errorMessage, $responseCode); case self::HTTP_RESPONSE_CODE_TOO_MANY_REQUESTS: - return new ToManyRequestsException($message, $args); + return new TooManyRequestsException($errorMessage, $responseCode); case self::HTTP_RESPONSE_CODE_INTERNAL_SERVER_ERROR: - return new PleaseContactBunqException($message, $args); + return new PleaseContactBunqException($errorMessage, $responseCode); default: - return new UnknownApiErrorException($message, $args); + return new UnknownApiErrorException($errorMessage, $responseCode); } } + + private static function generateMessageError($responseCode, $messages) + { + $lineResponseCode = sprintf(self::FORMAT_RESPONSE_CODE_LINE, $responseCode); + + return static::glueMessages(array_merge([$lineResponseCode], $messages)); + } + + private static function glueMessages($messages) + { + return implode(self::GLUE_ERROR_MESSAGES, $messages); + } } diff --git a/src/Exception/ToManyRequestsException.php b/src/Exception/ToManyRequestsException.php deleted file mode 100644 index 2c09bd74..00000000 --- a/src/Exception/ToManyRequestsException.php +++ /dev/null @@ -1,9 +0,0 @@ - $middleware, self::OPTION_VERIFY => true, self::OPTION_CURL => [ - CURLOPT_PINNEDPUBLICKEY => $this->determinePinnedServerPublicKey(), + //CURLOPT_PINNEDPUBLICKEY => $this->determinePinnedServerPublicKey(), ], self::OPTION_PROXY => $this->apiContext->getProxy(), ] diff --git a/src/Http/Handler/ResponseHandlerError.php b/src/Http/Handler/ResponseHandlerError.php index 63d3b711..f06f1243 100644 --- a/src/Http/Handler/ResponseHandlerError.php +++ b/src/Http/Handler/ResponseHandlerError.php @@ -46,37 +46,72 @@ class ResponseHandlerError extends ResponseHandlerBase */ public function execute(ResponseInterface $response) { - $contentType = $response->getHeaderLine(self::HEADER_CONTENT_TYPE); - - if ($response->getStatusCode() === self::STATUS_CODE_OK) { - return $response; - } else { - if ($contentType === self::HEADER_CONTENT_TYPE_APPLICATION_JSON) { - $responseBody = $response->getBody(); - $responseJson = \GuzzleHttp\json_decode($responseBody, true); - - $errorDescriptions = []; - - foreach ($responseJson[self::FIELD_ERROR] as $error) { - $errorDescriptions[] = $error[self::FIELD_ERROR_DESCRIPTION]; - } - - throw ExceptionFactory::createExceptionForResponse( - self::ERROR_FROM_JSON, - [ - $response->getStatusCode(), - implode(self::SEPERATOR_ERROR, $errorDescriptions), - ] - ); - } else { - throw ExceptionFactory::createExceptionForResponse( - self::ERROR_UNKNOWN, - [ - $response->getStatusCode(), - $response->getBody(), - ] - ); - } + if (!($response->getStatusCode() == self::STATUS_CODE_OK)){ + throw ExceptionFactory::createExceptionForResponse( + $this->fetchErrorMessages($response), + $response->getStatusCode() + ); } + + return $response; + } + + private function fetchErrorMessages(ResponseInterface $response) + { + $responseBody = $response->getBody(); + $responseBodyInJson = json_decode($responseBody, true); + + if (!($responseBodyInJson == false)){ + return $this->fetchErrorDescriptions($responseBodyInJson); + } + + return [$responseBody]; + + } + + private function fetchErrorDescriptions(array $errorArray) + { + $errorDescriptions = []; + + foreach ($errorArray['Error'] as $error){ + $description = $error['error_description']; + $errorDescriptions[] = $description; + } + + return $errorDescriptions; } + //{ + // $contentType = $response->getHeaderLine(self::HEADER_CONTENT_TYPE); + // + // if ($response->getStatusCode() === self::STATUS_CODE_OK) { + // return $response; + // } else { + // if ($contentType === self::HEADER_CONTENT_TYPE_APPLICATION_JSON) { + // $responseBody = $response->getBody(); + // $responseJson = \GuzzleHttp\json_decode($responseBody, true); + // + // $errorDescriptions = []; + // + // foreach ($responseJson[self::FIELD_ERROR] as $error) { + // $errorDescriptions[] = $error[self::FIELD_ERROR_DESCRIPTION]; + // } + // + // throw ExceptionFactory::createExceptionForResponse( + // self::ERROR_FROM_JSON, + // [ + // $response->getStatusCode(), + // implode(self::SEPERATOR_ERROR, $errorDescriptions), + // ] + // ); + // } else { + // throw ExceptionFactory::createExceptionForResponse( + // self::ERROR_UNKNOWN, + // [ + // $response->getStatusCode(), + // $response->getBody(), + // ] + // ); + // } + // } + //} } From 47f7170f639ec34007778bfeb831032164939b9c Mon Sep 17 00:00:00 2001 From: Kevin Hellemun Date: Thu, 21 Sep 2017 13:29:03 +0200 Subject: [PATCH 06/18] Some cleaning :clap:. --- src/Exception/ExceptionFactory.php | 16 +++++- src/Http/Handler/ResponseHandlerError.php | 65 +++++------------------ 2 files changed, 26 insertions(+), 55 deletions(-) diff --git a/src/Exception/ExceptionFactory.php b/src/Exception/ExceptionFactory.php index 808bf511..fa59c214 100644 --- a/src/Exception/ExceptionFactory.php +++ b/src/Exception/ExceptionFactory.php @@ -16,6 +16,9 @@ class ExceptionFactory const HTTP_RESPONSE_CODE_TOO_MANY_REQUESTS = 429; const HTTP_RESPONSE_CODE_INTERNAL_SERVER_ERROR = 500; + /** + * Formatting constants + */ const FORMAT_RESPONSE_CODE_LINE = 'HTTP Response Code: %s'; const GLUE_ERROR_MESSAGES = "\n"; @@ -32,9 +35,7 @@ class ExceptionFactory */ public static function createExceptionForResponse($messages, $responseCode) { - var_dump($messages); $errorMessage = static::generateMessageError($responseCode, $messages); - var_dump($errorMessage); switch ($responseCode) { @@ -57,6 +58,12 @@ public static function createExceptionForResponse($messages, $responseCode) } } + /** + * @param $responseCode + * @param $messages + * + * @return string + */ private static function generateMessageError($responseCode, $messages) { $lineResponseCode = sprintf(self::FORMAT_RESPONSE_CODE_LINE, $responseCode); @@ -64,6 +71,11 @@ private static function generateMessageError($responseCode, $messages) return static::glueMessages(array_merge([$lineResponseCode], $messages)); } + /** + * @param $messages + * + * @return string + */ private static function glueMessages($messages) { return implode(self::GLUE_ERROR_MESSAGES, $messages); diff --git a/src/Http/Handler/ResponseHandlerError.php b/src/Http/Handler/ResponseHandlerError.php index f06f1243..1c6c3dab 100644 --- a/src/Http/Handler/ResponseHandlerError.php +++ b/src/Http/Handler/ResponseHandlerError.php @@ -10,29 +10,12 @@ */ class ResponseHandlerError extends ResponseHandlerBase { - /** - * Error constants. - */ - const ERROR_UNKNOWN = 'An error occurred with status code "%d" and message "%s"'; - const ERROR_FROM_JSON = 'An unexpected error occurred with status code "%d" and message "%s"'; - - /** - * Header constants. - */ - const HEADER_CONTENT_TYPE = 'Content-Type'; - const HEADER_CONTENT_TYPE_APPLICATION_JSON = 'application/json'; - /** * Field constants. */ const FIELD_ERROR = 'Error'; const FIELD_ERROR_DESCRIPTION = 'error_description'; - /** - * Formatting constants. - */ - const SEPERATOR_ERROR = ', '; - /** * Http status code constants. */ @@ -56,6 +39,11 @@ public function execute(ResponseInterface $response) return $response; } + /** + * @param ResponseInterface $response + * + * @return array + */ private function fetchErrorMessages(ResponseInterface $response) { $responseBody = $response->getBody(); @@ -69,49 +57,20 @@ private function fetchErrorMessages(ResponseInterface $response) } + /** + * @param array $errorArray + * + * @return array + */ private function fetchErrorDescriptions(array $errorArray) { $errorDescriptions = []; - foreach ($errorArray['Error'] as $error){ - $description = $error['error_description']; + foreach ($errorArray[self::FIELD_ERROR] as $error){ + $description = $error[self::FIELD_ERROR_DESCRIPTION]; $errorDescriptions[] = $description; } return $errorDescriptions; } - //{ - // $contentType = $response->getHeaderLine(self::HEADER_CONTENT_TYPE); - // - // if ($response->getStatusCode() === self::STATUS_CODE_OK) { - // return $response; - // } else { - // if ($contentType === self::HEADER_CONTENT_TYPE_APPLICATION_JSON) { - // $responseBody = $response->getBody(); - // $responseJson = \GuzzleHttp\json_decode($responseBody, true); - // - // $errorDescriptions = []; - // - // foreach ($responseJson[self::FIELD_ERROR] as $error) { - // $errorDescriptions[] = $error[self::FIELD_ERROR_DESCRIPTION]; - // } - // - // throw ExceptionFactory::createExceptionForResponse( - // self::ERROR_FROM_JSON, - // [ - // $response->getStatusCode(), - // implode(self::SEPERATOR_ERROR, $errorDescriptions), - // ] - // ); - // } else { - // throw ExceptionFactory::createExceptionForResponse( - // self::ERROR_UNKNOWN, - // [ - // $response->getStatusCode(), - // $response->getBody(), - // ] - // ); - // } - // } - //} } From 926fd6441340b60aa1714c8bcfcf8e430036a9cb Mon Sep 17 00:00:00 2001 From: Kevin Hellemun Date: Thu, 21 Sep 2017 13:44:40 +0200 Subject: [PATCH 07/18] More cleaning :clap:. --- src/Exception/ExceptionFactory.php | 12 ++++++------ src/Http/Handler/ResponseHandlerError.php | 21 ++++++++++----------- 2 files changed, 16 insertions(+), 17 deletions(-) diff --git a/src/Exception/ExceptionFactory.php b/src/Exception/ExceptionFactory.php index fa59c214..f0c1b4d4 100644 --- a/src/Exception/ExceptionFactory.php +++ b/src/Exception/ExceptionFactory.php @@ -35,7 +35,7 @@ class ExceptionFactory */ public static function createExceptionForResponse($messages, $responseCode) { - $errorMessage = static::generateMessageError($responseCode, $messages); + $errorMessage = static::generateErrorMessage($responseCode, $messages); switch ($responseCode) { @@ -59,12 +59,12 @@ public static function createExceptionForResponse($messages, $responseCode) } /** - * @param $responseCode - * @param $messages + * @param int $responseCode + * @param string[] $messages * * @return string */ - private static function generateMessageError($responseCode, $messages) + private static function generateErrorMessage(int $responseCode, array $messages): string { $lineResponseCode = sprintf(self::FORMAT_RESPONSE_CODE_LINE, $responseCode); @@ -72,11 +72,11 @@ private static function generateMessageError($responseCode, $messages) } /** - * @param $messages + * @param string[] $messages * * @return string */ - private static function glueMessages($messages) + private static function glueMessages(array $messages): string { return implode(self::GLUE_ERROR_MESSAGES, $messages); } diff --git a/src/Http/Handler/ResponseHandlerError.php b/src/Http/Handler/ResponseHandlerError.php index 1c6c3dab..9d69dbab 100644 --- a/src/Http/Handler/ResponseHandlerError.php +++ b/src/Http/Handler/ResponseHandlerError.php @@ -27,9 +27,9 @@ class ResponseHandlerError extends ResponseHandlerBase * @return ResponseInterface * @throws ApiException */ - public function execute(ResponseInterface $response) + public function execute(ResponseInterface $response): ResponseInterface { - if (!($response->getStatusCode() == self::STATUS_CODE_OK)){ + if ($response->getStatusCode() !== self::STATUS_CODE_OK){ throw ExceptionFactory::createExceptionForResponse( $this->fetchErrorMessages($response), $response->getStatusCode() @@ -42,27 +42,26 @@ public function execute(ResponseInterface $response) /** * @param ResponseInterface $response * - * @return array + * @return string[] */ - private function fetchErrorMessages(ResponseInterface $response) + private function fetchErrorMessages(ResponseInterface $response): array { $responseBody = $response->getBody(); $responseBodyInJson = json_decode($responseBody, true); - if (!($responseBodyInJson == false)){ + if ($responseBodyInJson != false){ return $this->fetchErrorDescriptions($responseBodyInJson); + }else{ + return [$responseBody]; } - - return [$responseBody]; - } /** - * @param array $errorArray + * @param string[] $errorArray * - * @return array + * @return string[] */ - private function fetchErrorDescriptions(array $errorArray) + private function fetchErrorDescriptions(array $errorArray): array { $errorDescriptions = []; From 84b7ba40ffe3bf61d66db0b42d55db15d2db47cd Mon Sep 17 00:00:00 2001 From: Kevin Hellemun Date: Thu, 21 Sep 2017 13:47:05 +0200 Subject: [PATCH 08/18] Im getting old :grin:. --- src/Exception/ApiException.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Exception/ApiException.php b/src/Exception/ApiException.php index 4aa6c0ba..f847e7f9 100644 --- a/src/Exception/ApiException.php +++ b/src/Exception/ApiException.php @@ -32,7 +32,7 @@ public function __construct($message, $responseCode) /** * @return int */ - public function getResponseCode() + public function getResponseCode(): int { return $this->responseCode; } From 554dd4c64347f50eadbc2fa561514620bfc1c1f6 Mon Sep 17 00:00:00 2001 From: Kevin Hellemun Date: Thu, 21 Sep 2017 13:47:37 +0200 Subject: [PATCH 09/18] :sob: --- src/Exception/ApiException.php | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/Exception/ApiException.php b/src/Exception/ApiException.php index f847e7f9..063b0ec4 100644 --- a/src/Exception/ApiException.php +++ b/src/Exception/ApiException.php @@ -7,11 +7,6 @@ */ class ApiException extends Exception { - /** - * The first item index in an array. - */ - const INDEX_FIRST = 0; - /** * @var int */ From b4a4bb60bac433fd44079f95bc7dd4292fc634fb Mon Sep 17 00:00:00 2001 From: Kevin Hellemun Date: Mon, 25 Sep 2017 09:21:25 +0200 Subject: [PATCH 10/18] Some cleaning :fire: --- src/Exception/ApiException.php | 5 ++--- src/Exception/BunqException.php | 6 +++--- src/Exception/ExceptionFactory.php | 4 ++-- src/Http/ApiClient.php | 2 +- 4 files changed, 8 insertions(+), 9 deletions(-) diff --git a/src/Exception/ApiException.php b/src/Exception/ApiException.php index 063b0ec4..bc7f41fe 100644 --- a/src/Exception/ApiException.php +++ b/src/Exception/ApiException.php @@ -13,11 +13,10 @@ class ApiException extends Exception private $responseCode; /** - * ApiException constructor. - * + * @param string $message * @param int $responseCode */ - public function __construct($message, $responseCode) + public function __construct(string $message, int $responseCode) { $this->responseCode = $responseCode; diff --git a/src/Exception/BunqException.php b/src/Exception/BunqException.php index 7343fecd..3a5c6a46 100644 --- a/src/Exception/BunqException.php +++ b/src/Exception/BunqException.php @@ -7,10 +7,10 @@ class BunqException extends \Exception { /** * @param string $message - * @param string[] $responseCode + * @param string[] $args */ - public function __construct($message, $responseCode = []) + public function __construct(string $message, array $args = []) { - parent::__construct(vsprintf($message, $responseCode)); + parent::__construct(vsprintf($message, $args)); } } diff --git a/src/Exception/ExceptionFactory.php b/src/Exception/ExceptionFactory.php index f0c1b4d4..c7cf7d3d 100644 --- a/src/Exception/ExceptionFactory.php +++ b/src/Exception/ExceptionFactory.php @@ -28,12 +28,12 @@ class ExceptionFactory const INDEX_FIRST = 0; /** - * @param array $messages + * @param string[] $messages * @param int $responseCode * * @return ApiException */ - public static function createExceptionForResponse($messages, $responseCode) + public static function createExceptionForResponse(array $messages, int $responseCode): ApiException { $errorMessage = static::generateErrorMessage($responseCode, $messages); diff --git a/src/Http/ApiClient.php b/src/Http/ApiClient.php index 23c45a07..8fa3f535 100644 --- a/src/Http/ApiClient.php +++ b/src/Http/ApiClient.php @@ -188,7 +188,7 @@ private function initializeHttpClient() self::OPTION_HANDLER => $middleware, self::OPTION_VERIFY => true, self::OPTION_CURL => [ - //CURLOPT_PINNEDPUBLICKEY => $this->determinePinnedServerPublicKey(), + CURLOPT_PINNEDPUBLICKEY => $this->determinePinnedServerPublicKey(), ], self::OPTION_PROXY => $this->apiContext->getProxy(), ] From c47a4cc339f2f2904f909b36358fb13f98f0fe1e Mon Sep 17 00:00:00 2001 From: Kevin Hellemun Date: Mon, 25 Sep 2017 10:46:28 +0200 Subject: [PATCH 11/18] Some markdown :pencil:. --- src/Exception/EXCEPTION.md | 86 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 src/Exception/EXCEPTION.md diff --git a/src/Exception/EXCEPTION.md b/src/Exception/EXCEPTION.md new file mode 100644 index 00000000..19e72cd5 --- /dev/null +++ b/src/Exception/EXCEPTION.md @@ -0,0 +1,86 @@ +## Exceptions + +When you make a request via the SDK, there is a chance of request failing +due to various reasons. When such a failure happens, an exception +corresponding to the error occurred is raised. + + +---- +#### Possible Exceptions + +* `BadRequestException` If the request returns with status code `400` +* `UnauthorizedException` If the request returns with status code `401` +* `ForbiddenException` If the request returns with status code `403` +* `NotFoundException` If the request returns with status code `404` +* `MethodNotAllowedException` If the request returns with status code `405` +* `TooManyRequestsException` If the request returns with status code `429` +* `PleaseContactBunqException` If the request returns with status code `500`. +If you get this exception, please contact us preferably via the support chat in the bunq app. +* `UnknownApiErrorException` If none of the above mentioned exceptions are raised, +this exception will be raised instead. + +For more information regarding these errors, please take a look on the documentation +page here: https://doc.bunq.com/api/1/page/errors + +--- +#### Base exception +All the exceptions have the same base exception which looks like this: +```php +responseCode; + } +} +``` +The `Exception` class which is being extended has an `getMessage()` method which is `final` and therefore cannot be +overwritten. + +This means that each exception will have the response code and the error message +related to the specific exception that has been raised. + +--- +#### Exception handling +Because we raise different exceptions for each error, you can catch an error +if you expect it to be raised. + +```php +getResponseCode() . PHP_EOL; + echo $error->getMessage() . PHP_EOL; +} + + +``` + +This will ensure that you are ready for anything that might go wrong! From c4acc09e064ecc1e8d4f082d9166096dd4fc8dc0 Mon Sep 17 00:00:00 2001 From: Kevin Hellemun Date: Mon, 25 Sep 2017 10:47:04 +0200 Subject: [PATCH 12/18] Added readme reference. --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index 84612d68..035defe0 100644 --- a/README.md +++ b/README.md @@ -233,3 +233,7 @@ Please do not forget to set the `_API_KEY` constant in Information regarding the test cases can be found in the [README.md](./tests/README.md) located in [test](/tests). + +## Exceptions +The SDK can raise multiple exceptions. For an overview of these exceptions please +take a look at [EXCEPTIONS.md](./src/Exception/EXCEPTION.md) From 96996a31a9283095a60b8d866c71da410af119d8 Mon Sep 17 00:00:00 2001 From: Kevin Hellemun Date: Thu, 5 Oct 2017 17:51:18 +0200 Subject: [PATCH 13/18] Copywrtiting :pencil:. --- README.md | 2 +- src/Exception/EXCEPTION.md | 16 ++++++---------- 2 files changed, 7 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 035defe0..a68bff19 100644 --- a/README.md +++ b/README.md @@ -235,5 +235,5 @@ Information regarding the test cases can be found in the [README.md](./tests/REA located in [test](/tests). ## Exceptions -The SDK can raise multiple exceptions. For an overview of these exceptions please +The SDK can throw multiple exceptions. For an overview of these exceptions please take a look at [EXCEPTIONS.md](./src/Exception/EXCEPTION.md) diff --git a/src/Exception/EXCEPTION.md b/src/Exception/EXCEPTION.md index 19e72cd5..09537f93 100644 --- a/src/Exception/EXCEPTION.md +++ b/src/Exception/EXCEPTION.md @@ -1,5 +1,4 @@ ## Exceptions - When you make a request via the SDK, there is a chance of request failing due to various reasons. When such a failure happens, an exception corresponding to the error occurred is raised. @@ -7,7 +6,6 @@ corresponding to the error occurred is raised. ---- #### Possible Exceptions - * `BadRequestException` If the request returns with status code `400` * `UnauthorizedException` If the request returns with status code `401` * `ForbiddenException` If the request returns with status code `403` @@ -16,8 +14,8 @@ corresponding to the error occurred is raised. * `TooManyRequestsException` If the request returns with status code `429` * `PleaseContactBunqException` If the request returns with status code `500`. If you get this exception, please contact us preferably via the support chat in the bunq app. -* `UnknownApiErrorException` If none of the above mentioned exceptions are raised, -this exception will be raised instead. +* `UnknownApiErrorException` If none of the above mentioned exceptions are thrown, +this exception will be thrown instead. For more information regarding these errors, please take a look on the documentation page here: https://doc.bunq.com/api/1/page/errors @@ -53,13 +51,13 @@ class ApiException extends Exception The `Exception` class which is being extended has an `getMessage()` method which is `final` and therefore cannot be overwritten. -This means that each exception will have the response code and the error message -related to the specific exception that has been raised. +This means that each exception will have a response code and an error message +related to the specific error returned by API. --- #### Exception handling -Because we raise different exceptions for each error, you can catch an error -if you expect it to be raised. +Since each API error has a distinct SDK exception type corresponding to it, +you can catch the exact exceptions you expect 👏. ```php Date: Fri, 6 Oct 2017 12:50:59 +0200 Subject: [PATCH 14/18] Magic hidden code. --- src/Exception/EXCEPTION.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Exception/EXCEPTION.md b/src/Exception/EXCEPTION.md index 09537f93..081d98ca 100644 --- a/src/Exception/EXCEPTION.md +++ b/src/Exception/EXCEPTION.md @@ -37,7 +37,9 @@ class ApiException extends Exception * @param int $responseCode */ public function __construct(string $message, int $responseCode) - {} + { + //Some hidden code + } /** * @return int From 33fe8a5365a0b1df1e5b19e1eb9fac87abf22548 Mon Sep 17 00:00:00 2001 From: Kevin Hellemun Date: Sat, 7 Oct 2017 12:12:55 +0200 Subject: [PATCH 15/18] All tests passing :star:. --- tests/Model/Generated/Endpoint/SessionTest.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/tests/Model/Generated/Endpoint/SessionTest.php b/tests/Model/Generated/Endpoint/SessionTest.php index 7fb36fd5..681fdac5 100644 --- a/tests/Model/Generated/Endpoint/SessionTest.php +++ b/tests/Model/Generated/Endpoint/SessionTest.php @@ -15,6 +15,11 @@ class SessionTest extends BunqSdkTestBase */ const SESSION_ID_DUMMY = 0; + /** + * Full name of context config file to use for testing. + */ + const FILENAME_CONTEXT_CONFIG = __DIR__ . '/../bunq-test.conf'; + /** * Delete's the current session. * @@ -31,6 +36,6 @@ public function testDeleteSession() */ public static function tearDownAfterClass() { - static::ensureApiContextValid(); + unlink(static::FILENAME_CONTEXT_CONFIG); } } From ed61b14f5e9a4dccb7be67e96492a42edffd15e8 Mon Sep 17 00:00:00 2001 From: Kevin Hellemun Date: Mon, 9 Oct 2017 20:09:30 +0200 Subject: [PATCH 16/18] Some cleaning :fire: --- src/Exception/EXCEPTION.md | 6 +++--- tests/Model/Generated/Endpoint/SessionTest.php | 11 ++++++++++- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/Exception/EXCEPTION.md b/src/Exception/EXCEPTION.md index 081d98ca..4124882b 100644 --- a/src/Exception/EXCEPTION.md +++ b/src/Exception/EXCEPTION.md @@ -1,7 +1,7 @@ ## Exceptions When you make a request via the SDK, there is a chance of request failing due to various reasons. When such a failure happens, an exception -corresponding to the error occurred is raised. +corresponding to the error occurred is thrown. ---- @@ -51,7 +51,7 @@ class ApiException extends Exception } ``` The `Exception` class which is being extended has an `getMessage()` method which is `final` and therefore cannot be -overwritten. +overridden. This means that each exception will have a response code and an error message related to the specific error returned by API. @@ -75,7 +75,7 @@ try{ //Make a call that might fail $apiContext = ApiContext::create(BunqEnumApiEnvironmentType::SANDBOX(), API_KEY, DEVICE_DESCRIPTION); } catch (BadRequestException $error){ - //Do something if exception is thrown. + // Do something if exception is thrown. echo $error->getResponseCode() . PHP_EOL; echo $error->getMessage() . PHP_EOL; } diff --git a/tests/Model/Generated/Endpoint/SessionTest.php b/tests/Model/Generated/Endpoint/SessionTest.php index 681fdac5..7a166504 100644 --- a/tests/Model/Generated/Endpoint/SessionTest.php +++ b/tests/Model/Generated/Endpoint/SessionTest.php @@ -20,6 +20,11 @@ class SessionTest extends BunqSdkTestBase */ const FILENAME_CONTEXT_CONFIG = __DIR__ . '/../bunq-test.conf'; + /** + * The amount of secconds to sleep to prevent the api from returning a 429. + */ + const SECONDS_TO_SLEEP = 2; + /** * Delete's the current session. * @@ -36,6 +41,10 @@ public function testDeleteSession() */ public static function tearDownAfterClass() { - unlink(static::FILENAME_CONTEXT_CONFIG); + $apiContext = static::getApiContext(); + + sleep(self::SECONDS_TO_SLEEP); + $apiContext->resetSession(); + $apiContext->save(self::FILENAME_CONTEXT_CONFIG); } } From 7b91a040acb07d548a79cb269052dff19ddeaa1b Mon Sep 17 00:00:00 2001 From: Kevin Hellemun Date: Mon, 9 Oct 2017 20:10:47 +0200 Subject: [PATCH 17/18] Added a little doc. --- tests/Model/Generated/Endpoint/SessionTest.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/Model/Generated/Endpoint/SessionTest.php b/tests/Model/Generated/Endpoint/SessionTest.php index 7a166504..7486a32d 100644 --- a/tests/Model/Generated/Endpoint/SessionTest.php +++ b/tests/Model/Generated/Endpoint/SessionTest.php @@ -38,6 +38,7 @@ public function testDeleteSession() } /** + * Resets the session context after this test has ran. */ public static function tearDownAfterClass() { From 4f25c4a858e190cd6437a047f834ec96a7860318 Mon Sep 17 00:00:00 2001 From: Kevin Hellemun Date: Tue, 10 Oct 2017 18:21:38 +0200 Subject: [PATCH 18/18] Changes based on review. --- src/Exception/EXCEPTION.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Exception/EXCEPTION.md b/src/Exception/EXCEPTION.md index 4124882b..919f936a 100644 --- a/src/Exception/EXCEPTION.md +++ b/src/Exception/EXCEPTION.md @@ -38,7 +38,7 @@ class ApiException extends Exception */ public function __construct(string $message, int $responseCode) { - //Some hidden code + // Some hidden code } /** @@ -72,7 +72,7 @@ const API_KEY = 'Some invalid API key '; const DEVICE_DESCRIPTION = 'This will cause BadRequestException to be thrown.'; try{ - //Make a call that might fail + // Make a call that might fail $apiContext = ApiContext::create(BunqEnumApiEnvironmentType::SANDBOX(), API_KEY, DEVICE_DESCRIPTION); } catch (BadRequestException $error){ // Do something if exception is thrown.