Skip to content

Commit

Permalink
improved error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
ivodvb committed Jun 17, 2016
1 parent 2c4e4f2 commit f7481ca
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 13 deletions.
31 changes: 19 additions & 12 deletions src/Api/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

namespace Paynl\Api;

use Curl\Curl;
use Paynl\Config;
use Paynl\Error;
use Paynl\Helper;
Expand All @@ -32,12 +31,13 @@ class Api
{
protected $version = 1;

protected $data = array();
protected $data = [];

/**
* @var bool Is the ApiToken required for this API
*/
protected $apiTokenRequired = false;

/**
* @var bool Is the serviceId required for this API
*/
Expand All @@ -55,59 +55,66 @@ public function isServiceIdRequired()

protected function getData()
{
if($this->isApiTokenRequired()) {
if ($this->isApiTokenRequired()) {
Helper::requireApiToken();

$this->data['token'] = Config::getApiToken();
}
if($this->isServiceIdRequired()){
if ($this->isServiceIdRequired()) {
Helper::requireServiceId();

$this->data['serviceId'] = Config::getServiceId();
}

return $this->data;
}

protected function processResult($result)
{
$output = Helper::objectToArray($result);

if(!is_array($output)){
if (!is_array($output)) {
throw new Error\Api($output);
}

if ($output['request']['result'] != 1 && $output['request']['result'] != 'TRUE') {
throw new Error\Api($output['request']['errorId'] . ' - ' . $output['request']['errorMessage']);
throw new Error\Api($output['request']['errorId'].' - '.$output['request']['errorMessage']);
}

return $output;
}

public function doRequest($endpoint, $version = null)
{
if(is_null($version)){
if (is_null($version)) {
$version = $this->version;
}

$data = $this->getData();


$uri = Config::getApiUrl($endpoint, $version);

$curl = Config::getCurl();

if(Config::getCAInfoLocation()){
if (Config::getCAInfoLocation()) {
// set a custom CAInfo file
$curl->setOpt(CURLOPT_CAINFO, Config::getCAInfoLocation());
}

$result = $curl->post($uri, $data);

if($curl->error){
throw new Error\Error($curl->errorMessage);
if ($curl->error) {
// todo handle 400 errors properly, the SDK user needs to know what
// went wrong

$error = new Error\Error($curl->errorMessage);
$error->setAdditionalData($result);

throw $error;
}

$output = static::processResult($result);

return $output;
}
}
}
37 changes: 36 additions & 1 deletion src/Error/Error.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,40 @@
*/
class Error extends \Exception
{
//put your code here
/**
* @var mixed
*/
protected $additionalData;

/**
* Error constructor.
*
* @param null $message
* @param int $code
* @param \Exception|null $previous
*/
public function __construct($message = null, $code = 0, \Exception $previous = null)
{
parent::__construct($message, $code, $previous);
}

/**
* @return mixed
*/
public function getAdditionalData()
{
return $this->additionalData;
}

/**
* @param mixed $additionalData
*
* @return Error
*/
public function setAdditionalData($additionalData)
{
$this->additionalData = $additionalData;

return $this;
}
}

0 comments on commit f7481ca

Please sign in to comment.