Skip to content

Commit

Permalink
Merge pull request #17 from route4me/error_handling
Browse files Browse the repository at this point in the history
Changed error handling in Route4Me->makeRequst()
  • Loading branch information
h2rd authored Feb 16, 2023
2 parents c0405e1 + c2da2cc commit df50dfe
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 27 deletions.
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
"name": "route4me/route4me-php",
"description": "Access Route4Me's logistics-as-a-service API using our PHP SDK",
"minimum-stability": "stable",
"version": "1.2.3",

"version": "1.2.4",
"authors": [
{
"name": "Igor Route4Me",
Expand Down
12 changes: 12 additions & 0 deletions src/Route4Me/Exception/ApiError.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,16 @@

class ApiError extends \Exception
{
protected string $source = '';

public function __construct(string $message = '', int $code = 0, string $source = '', \Throwable $previous = null)
{
parent::__construct($message, $code, $previous);
$this->source = $source;
}

final public function getSource() : string
{
return $this->source;
}
}
68 changes: 43 additions & 25 deletions src/Route4Me/Route4Me.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ public static function getBaseUrl()
public static function makeRequst($options)
{
$method = isset($options['method']) ? $options['method'] : 'GET';
$query = isset($options['query']) ? array_filter($options['query'], function ($x) { return !is_null($x); }) : [];
$query = isset($options['query'])
? array_filter($options['query'], function ($x) {
return !is_null($x);
}) : [];

$body = isset($options['body']) ? $options['body'] : null;
$file = isset($options['FILE']) ? $options['FILE'] : null;
Expand All @@ -53,9 +56,11 @@ public static function makeRequst($options)

$ch = curl_init();

$url = isset($options['url']) ? $options['url'].'?'.http_build_query(array_merge(
$query, ['api_key' => self::getApiKey()]
)) : '';
$url = isset($options['url'])
? $options['url'] . '?' . http_build_query(array_merge(
$query,
['api_key' => self::getApiKey()]
)) : '';

$baseUrl = self::getBaseUrl();

Expand All @@ -72,10 +77,10 @@ public static function makeRequst($options)
curl_setopt_array($ch, $curlOpts);

if (null != $file) {
$cfile = new \CURLFile($file,'','');
$cfile = new \CURLFile($file, '', '');
$body['strFilename']=$cfile;
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_POST,true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
} else {
switch ($method) {
Expand All @@ -102,7 +107,8 @@ public static function makeRequst($options)
}
break;
case 'ADD':
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($query)); break;
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($query));
break;
}

if (is_numeric(array_search($method, ['DELETE', 'PUT']))) {
Expand All @@ -113,35 +119,47 @@ public static function makeRequst($options)
}

$result = curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

$isxml = false;
$jxml = '';
$json = null;
if (strpos($result, '<?xml') > -1) {
$xml = simplexml_load_string($result);
//$jxml = json_encode($xml);
$jxml = self::object2array($xml);
$isxml = true;
$json = self::object2array($xml);
} else {
$json = json_decode($result, true);
}

$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

if (200 == $code) {
if ($isxml) {
$json = $jxml;
} else {
$json = json_decode($result, true);
}

if (isset($json['errors'])) {
throw new ApiError(implode(', ', $json['errors']));
throw new ApiError(implode(', ', $json['errors']), $code, $result);
} else {
return $json;
}
} elseif (409 == $code) {
throw new ApiError('Wrong API key');
} elseif (isset($code) && (!isset($result) || !$result)) {
throw new ApiError('', $code, $result);
} else {
throw new ApiError('Something wrong');
if (isset($json['messages'])) {
$msg = '';
foreach ($json['messages'] as $key => $value) {
if ($msg !== '') {
$msg .= PHP_EOL;
}
$msg .= $key . ': ' . implode(', ', $value);
}
throw new ApiError($msg, $code, $result);
} elseif (isset($json['errors'])) {
$msg = '';
foreach ($json['errors'] as $key => $value) {
if ($msg !== '') {
$msg .= PHP_EOL;
}
$msg .= $value;
}
throw new ApiError($msg, $code, $result);
} else {
throw new ApiError($result, $code, $result);
}
}
}

Expand Down

0 comments on commit df50dfe

Please sign in to comment.