-
Notifications
You must be signed in to change notification settings - Fork 54
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Proper check for curl error zero. (bunq/sdk_php#7) #148
Merged
OGKevin
merged 5 commits into
develop
from
proper-check-for-curl-error-zero-bunq/sdk_php#7
May 29, 2018
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
281ab1b
Proper check for curl error zero. (bunq/sdk_php#7)
OGKevin 325d390
Minor forammting fixes. (bunq/sdk_php#7)
OGKevin d5641dd
Formatted error message accordingly. (bunq/sdk_php#7)
OGKevin 6474a8c
Better error formatting. (bunq/sdk_php#7)
OGKevin b3fcc16
Removed spacing from error format. (bunq/sdk_php#7)
OGKevin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,15 +39,21 @@ 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.'; | ||
|
||
/** | ||
* Public key locations. | ||
*/ | ||
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. | ||
*/ | ||
|
@@ -130,6 +136,12 @@ 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<errorCode>\d+)/'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. constant comment |
||
const REGEX_NAMED_GOUP_ERROR_CODE = 'errorCode'; | ||
|
||
/** | ||
* @var Client | ||
*/ | ||
|
@@ -192,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, | ||
|
@@ -209,8 +222,8 @@ private function request( | |
$this->determineRequestOptions($body, $customHeaders) | ||
); | ||
} catch (RequestException $exception) { | ||
if ($exception->getCode() === self::ERROR_CODE_MAC_OS_CURL_BUG && $this->isMacOs()) { | ||
die(vsprintf(self::ERROR_MAC_OS_CURL_VERSION, [$this->determineVersionPhpMacOs(), PHP_EOL])); | ||
if ($this->isCurlErrorCodeZero($exception) && $this->isMacOs()) { | ||
throw new BunqException($this->determineErrorMessageCurlZero()); | ||
} else { | ||
throw $exception; | ||
} | ||
|
@@ -375,6 +388,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 | ||
*/ | ||
|
@@ -383,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 | ||
*/ | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove the spaces, otherwise the second line will start with a space, and there will be a useless trailing space after the first line as well.