Skip to content

Commit df50dfe

Browse files
authored
Merge pull request #17 from route4me/error_handling
Changed error handling in Route4Me->makeRequst()
2 parents c0405e1 + c2da2cc commit df50dfe

File tree

3 files changed

+56
-27
lines changed

3 files changed

+56
-27
lines changed

composer.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
"name": "route4me/route4me-php",
33
"description": "Access Route4Me's logistics-as-a-service API using our PHP SDK",
44
"minimum-stability": "stable",
5-
"version": "1.2.3",
6-
5+
"version": "1.2.4",
76
"authors": [
87
{
98
"name": "Igor Route4Me",

src/Route4Me/Exception/ApiError.php

+12
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,16 @@
44

55
class ApiError extends \Exception
66
{
7+
protected string $source = '';
8+
9+
public function __construct(string $message = '', int $code = 0, string $source = '', \Throwable $previous = null)
10+
{
11+
parent::__construct($message, $code, $previous);
12+
$this->source = $source;
13+
}
14+
15+
final public function getSource() : string
16+
{
17+
return $this->source;
18+
}
719
}

src/Route4Me/Route4Me.php

+43-25
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,10 @@ public static function getBaseUrl()
3333
public static function makeRequst($options)
3434
{
3535
$method = isset($options['method']) ? $options['method'] : 'GET';
36-
$query = isset($options['query']) ? array_filter($options['query'], function ($x) { return !is_null($x); }) : [];
36+
$query = isset($options['query'])
37+
? array_filter($options['query'], function ($x) {
38+
return !is_null($x);
39+
}) : [];
3740

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

5457
$ch = curl_init();
5558

56-
$url = isset($options['url']) ? $options['url'].'?'.http_build_query(array_merge(
57-
$query, ['api_key' => self::getApiKey()]
58-
)) : '';
59+
$url = isset($options['url'])
60+
? $options['url'] . '?' . http_build_query(array_merge(
61+
$query,
62+
['api_key' => self::getApiKey()]
63+
)) : '';
5964

6065
$baseUrl = self::getBaseUrl();
6166

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

7479
if (null != $file) {
75-
$cfile = new \CURLFile($file,'','');
80+
$cfile = new \CURLFile($file, '', '');
7681
$body['strFilename']=$cfile;
7782
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
78-
curl_setopt($ch, CURLOPT_POST,true);
83+
curl_setopt($ch, CURLOPT_POST, true);
7984
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
8085
} else {
8186
switch ($method) {
@@ -102,7 +107,8 @@ public static function makeRequst($options)
102107
}
103108
break;
104109
case 'ADD':
105-
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($query)); break;
110+
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($query));
111+
break;
106112
}
107113

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

115121
$result = curl_exec($ch);
122+
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
123+
curl_close($ch);
116124

117-
$isxml = false;
118-
$jxml = '';
125+
$json = null;
119126
if (strpos($result, '<?xml') > -1) {
120127
$xml = simplexml_load_string($result);
121-
//$jxml = json_encode($xml);
122-
$jxml = self::object2array($xml);
123-
$isxml = true;
128+
$json = self::object2array($xml);
129+
} else {
130+
$json = json_decode($result, true);
124131
}
125132

126-
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
127-
curl_close($ch);
128-
129133
if (200 == $code) {
130-
if ($isxml) {
131-
$json = $jxml;
132-
} else {
133-
$json = json_decode($result, true);
134-
}
135-
136134
if (isset($json['errors'])) {
137-
throw new ApiError(implode(', ', $json['errors']));
135+
throw new ApiError(implode(', ', $json['errors']), $code, $result);
138136
} else {
139137
return $json;
140138
}
141-
} elseif (409 == $code) {
142-
throw new ApiError('Wrong API key');
139+
} elseif (isset($code) && (!isset($result) || !$result)) {
140+
throw new ApiError('', $code, $result);
143141
} else {
144-
throw new ApiError('Something wrong');
142+
if (isset($json['messages'])) {
143+
$msg = '';
144+
foreach ($json['messages'] as $key => $value) {
145+
if ($msg !== '') {
146+
$msg .= PHP_EOL;
147+
}
148+
$msg .= $key . ': ' . implode(', ', $value);
149+
}
150+
throw new ApiError($msg, $code, $result);
151+
} elseif (isset($json['errors'])) {
152+
$msg = '';
153+
foreach ($json['errors'] as $key => $value) {
154+
if ($msg !== '') {
155+
$msg .= PHP_EOL;
156+
}
157+
$msg .= $value;
158+
}
159+
throw new ApiError($msg, $code, $result);
160+
} else {
161+
throw new ApiError($result, $code, $result);
162+
}
145163
}
146164
}
147165

0 commit comments

Comments
 (0)