Skip to content

Commit

Permalink
Improve error handling (#7)
Browse files Browse the repository at this point in the history
* Improve documentation - add navigation link

* Improve error handling

* Version bump to 1.0.1

* Optimize the code for the bug fix

* Improve code
  • Loading branch information
lyubomir-sumup authored Feb 18, 2019
1 parent fe9d6ad commit 0d68ae2
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "SumUp eCom SDK for PHP",
"type": "library",
"license": "proprietary",
"version": "1.0.0",
"version": "1.0.1",
"keywords": ["sumup", "sdk", "payment processing", "ecommerce", "payment", "checkout"],
"homepage": "https://developer.sumup.com",
"authors": [
Expand Down
28 changes: 26 additions & 2 deletions src/SumUp/HttpClients/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,36 @@ protected function parseResponseForErrors()
throw new SumUpValidationException($invalidFields, $this->httpResponseCode);
}
if ($this->httpResponseCode >= 500) {
$message = (!is_null($this->body) && !is_null($this->body->message)) ? $this->body->message : $this->body;
$message = $this->parseErrorMessage('Server error');
throw new SumUpServerException($message, $this->httpResponseCode);
}
if ($this->httpResponseCode >= 400) {
$message = (!is_null($this->body) && !is_null($this->body->message)) ? $this->body->message : $this->body;
$message = $this->parseErrorMessage('Client error');
throw new SumUpResponseException($message, $this->httpResponseCode);
}
}

/**
* Return error message.
*
* @param string $defaultMessage
*
* @return string
*/
protected function parseErrorMessage($defaultMessage = '')
{
if (is_null($this->body)) {
return $defaultMessage;
}

if (isset($this->body->message)) {
return $this->body->message;
}

if (isset($this->body->error_message)) {
return $this->body->error_message;
}

return $defaultMessage;
}
}

0 comments on commit 0d68ae2

Please sign in to comment.