From 281ab1b2c9359cadf76b681aa185b83ac14f159f Mon Sep 17 00:00:00 2001 From: Kevin Hellemun Date: Mon, 28 May 2018 16:18:11 +0200 Subject: [PATCH 1/5] Proper check for curl error zero. (bunq/sdk_php#7) --- src/Http/ApiClient.php | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/src/Http/ApiClient.php b/src/Http/ApiClient.php index 76a1891e..3c1637f4 100644 --- a/src/Http/ApiClient.php +++ b/src/Http/ApiClient.php @@ -40,7 +40,7 @@ class ApiClient */ const ERROR_ENVIRONMENT_TYPE_UNKNOWN = 'Unknown environmentType "%s"'; const ERROR_MAC_OS_CURL_VERSION = 'Your PHP seems to be linked to the MacOS provided curl binary. ' . - 'This is incompatible with our SDK, please reinstall by running: "brew reinstall %s --with-homebrew-curl".%s'; + 'This is incompatible with our SDK, please reinstall by running: "brew reinstall %s --with-homebrew-curl".%s'; /** * Public key locations. @@ -130,6 +130,9 @@ class ApiClient */ const COMMAND_DETERMINE_BREW_PHP_VERSION = 'brew list | egrep -e "^php[0-9]{2}$"'; + const REGEX_CURL_ERROR_CODE = '/(cURL error )(?P\d+)/'; + const REGEX_NAMED_GOUP_ERROR_CODE = 'errorCode'; + /** * @var Client */ @@ -209,7 +212,7 @@ private function request( $this->determineRequestOptions($body, $customHeaders) ); } catch (RequestException $exception) { - if ($exception->getCode() === self::ERROR_CODE_MAC_OS_CURL_BUG && $this->isMacOs()) { + if ($this->isCurlErrorCodeZero($exception) && $this->isMacOs()) { die(vsprintf(self::ERROR_MAC_OS_CURL_VERSION, [$this->determineVersionPhpMacOs(), PHP_EOL])); } else { throw $exception; @@ -375,6 +378,21 @@ protected function determineBodyString($body): string return $bodyString; } + /** + * @param RequestException $exception + * + * @return bool + */ + private function isCurlErrorCodeZero(RequestException $exception): bool + { + $allMatch = []; + + preg_match(self::REGEX_CURL_ERROR_CODE, $exception->getMessage(), $allMatch); + + return isset($allMatch[self::REGEX_NAMED_GOUP_ERROR_CODE]) && + $allMatch[self::REGEX_NAMED_GOUP_ERROR_CODE] === self::ERROR_CODE_MAC_OS_CURL_BUG; + } + /** * @return bool */ From 325d390ae6c951bee60756868bc248483b6c1c29 Mon Sep 17 00:00:00 2001 From: Kevin Hellemun Date: Mon, 28 May 2018 16:49:01 +0200 Subject: [PATCH 2/5] Minor forammting fixes. (bunq/sdk_php#7) --- src/Http/ApiClient.php | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/Http/ApiClient.php b/src/Http/ApiClient.php index 3c1637f4..90bc2d4f 100644 --- a/src/Http/ApiClient.php +++ b/src/Http/ApiClient.php @@ -39,8 +39,8 @@ class ApiClient * Error constants. */ const ERROR_ENVIRONMENT_TYPE_UNKNOWN = 'Unknown environmentType "%s"'; - const ERROR_MAC_OS_CURL_VERSION = 'Your PHP seems to be linked to the MacOS provided curl binary. ' . - 'This is incompatible with our SDK, please reinstall by running: "brew reinstall %s --with-homebrew-curl".%s'; + const ERROR_MAC_OS_CURL_VERSION = 'Your PHP seems to be linked to the MacOS provided curl binary. + This is incompatible with our SDK, please reinstall by running: "brew reinstall %s --with-homebrew-curl".%s'; /** * Public key locations. @@ -130,6 +130,9 @@ class ApiClient */ const COMMAND_DETERMINE_BREW_PHP_VERSION = 'brew list | egrep -e "^php[0-9]{2}$"'; + /** + * Curl error regex constants. + */ const REGEX_CURL_ERROR_CODE = '/(cURL error )(?P\d+)/'; const REGEX_NAMED_GOUP_ERROR_CODE = 'errorCode'; @@ -389,8 +392,8 @@ private function isCurlErrorCodeZero(RequestException $exception): bool preg_match(self::REGEX_CURL_ERROR_CODE, $exception->getMessage(), $allMatch); - return isset($allMatch[self::REGEX_NAMED_GOUP_ERROR_CODE]) && - $allMatch[self::REGEX_NAMED_GOUP_ERROR_CODE] === self::ERROR_CODE_MAC_OS_CURL_BUG; + return isset($allMatch[self::REGEX_NAMED_GOUP_ERROR_CODE]) + && $allMatch[self::REGEX_NAMED_GOUP_ERROR_CODE] === self::ERROR_CODE_MAC_OS_CURL_BUG; } /** From d5641ddae596ad543b96b1e1ffff6f67cbcbacf7 Mon Sep 17 00:00:00 2001 From: Kevin Hellemun Date: Tue, 29 May 2018 09:42:12 +0200 Subject: [PATCH 3/5] Formatted error message accordingly. (bunq/sdk_php#7) --- src/Http/ApiClient.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Http/ApiClient.php b/src/Http/ApiClient.php index 90bc2d4f..fb3d7cae 100644 --- a/src/Http/ApiClient.php +++ b/src/Http/ApiClient.php @@ -39,8 +39,8 @@ class ApiClient * Error constants. */ const ERROR_ENVIRONMENT_TYPE_UNKNOWN = 'Unknown environmentType "%s"'; - const ERROR_MAC_OS_CURL_VERSION = 'Your PHP seems to be linked to the MacOS provided curl binary. - This is incompatible with our SDK, please reinstall by running: "brew reinstall %s --with-homebrew-curl".%s'; + const ERROR_MAC_OS_CURL_VERSION = "Your PHP seems to be linked to the MacOS provided curl binary. \n + This is incompatible with our SDK, please reinstall by running: \"brew reinstall %s --with-homebrew-curl\".%s"; /** * Public key locations. From 6474a8ccac73e8e60180a3fd9106dd30a3502348 Mon Sep 17 00:00:00 2001 From: Kevin Hellemun Date: Tue, 29 May 2018 09:57:59 +0200 Subject: [PATCH 4/5] Better error formatting. (bunq/sdk_php#7) --- src/Http/ApiClient.php | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/src/Http/ApiClient.php b/src/Http/ApiClient.php index fb3d7cae..bd0b43e3 100644 --- a/src/Http/ApiClient.php +++ b/src/Http/ApiClient.php @@ -39,8 +39,7 @@ class ApiClient * Error constants. */ const ERROR_ENVIRONMENT_TYPE_UNKNOWN = 'Unknown environmentType "%s"'; - const ERROR_MAC_OS_CURL_VERSION = "Your PHP seems to be linked to the MacOS provided curl binary. \n - This is incompatible with our SDK, please reinstall by running: \"brew reinstall %s --with-homebrew-curl\".%s"; + const ERROR_MAC_OS_CURL_VERSION = 'Your PHP seems to be linked to the MacOS provided curl binary.'; /** * Public key locations. @@ -48,6 +47,13 @@ class ApiClient const FILE_PUBLIC_KEY_ENVIRONMENT_SANDBOX = '/Certificate/sandbox.public.api.bunq.com.pubkey.pem'; const FILE_PUBLIC_KEY_ENVIRONMENT_PRODUCTION = '/Certificate/api.bunq.com.pubkey.pem'; + /** + * String format constants. + */ + const FORMAT_CURL_INSTALLATION_INSTRUCTIONS = + 'This is incompatible with our SDK, please reinstall by running: "brew reinstall %s --with-homebrew-curl".%s'; + const FORMAT_ERROR_MESSAGE_MAC_CURL = '%s %s %s'; + /** * Body constants. */ @@ -198,6 +204,7 @@ public function get(string $uri, array $params, array $customHeaders): BunqRespo * @param string[] $customHeaders * * @return BunqResponseRaw + * @throws BunqException */ private function request( string $method, @@ -216,7 +223,7 @@ private function request( ); } catch (RequestException $exception) { if ($this->isCurlErrorCodeZero($exception) && $this->isMacOs()) { - die(vsprintf(self::ERROR_MAC_OS_CURL_VERSION, [$this->determineVersionPhpMacOs(), PHP_EOL])); + throw new BunqException($this->determineErrorMessageCurlZero()); } else { throw $exception; } @@ -404,6 +411,20 @@ private function isMacOs(): bool return posix_uname()[self::INDEX_UNAME_SYSNAME] === self::SYSNAME_MAC_OS; } + /** + * @return string + */ + private function determineErrorMessageCurlZero(): string + { + return vsprintf( + vsprintf( + self::FORMAT_ERROR_MESSAGE_MAC_CURL, + [self::ERROR_MAC_OS_CURL_VERSION, PHP_EOL, self::FORMAT_CURL_INSTALLATION_INSTRUCTIONS] + ), + [$this->determineVersionPhpMacOs(), PHP_EOL] + ); + } + /** * @return string */ From b3fcc160ca4b1cfb91bcfdd4e0e384dea0b23c7c Mon Sep 17 00:00:00 2001 From: Kevin Hellemun Date: Tue, 29 May 2018 12:11:49 +0200 Subject: [PATCH 5/5] Removed spacing from error format. (bunq/sdk_php#7) --- src/Http/ApiClient.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Http/ApiClient.php b/src/Http/ApiClient.php index bd0b43e3..5f6b7327 100644 --- a/src/Http/ApiClient.php +++ b/src/Http/ApiClient.php @@ -52,7 +52,7 @@ class ApiClient */ const FORMAT_CURL_INSTALLATION_INSTRUCTIONS = 'This is incompatible with our SDK, please reinstall by running: "brew reinstall %s --with-homebrew-curl".%s'; - const FORMAT_ERROR_MESSAGE_MAC_CURL = '%s %s %s'; + const FORMAT_ERROR_MESSAGE_MAC_CURL = '%s%s%s'; /** * Body constants.