Skip to content

Commit a8a72cb

Browse files
authored
Error handling (#23)
* fixup * wip * fixu * fixup * fixup * fixup * fixup-specs
1 parent 2eb5d71 commit a8a72cb

3 files changed

Lines changed: 24 additions & 64 deletions

File tree

src/ShipEngine.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public function createLabelFromRate($rateId, $params, $config = null): array
110110
$config = $this->config->merge($config);
111111
$client = new ShipEngineClient();
112112
$apiResponse = $client->post(
113-
"v1/labels/rates/{$rateId}",
113+
"v1/labels/rates/$rateId",
114114
$config,
115115
$params
116116
);
@@ -154,7 +154,7 @@ public function voidLabelWithLabelId($labelId, $config = null): array
154154
$config = $this->config->merge($config);
155155
$client = new ShipEngineClient();
156156
$apiResponse = $client->put(
157-
"v1/labels/${labelId}/void",
157+
"v1/labels/$labelId/void",
158158
$config
159159
);
160160

@@ -196,8 +196,8 @@ public function trackUsingLabelId($labelId, $config = null): array
196196
{
197197
$config = $this->config->merge($config);
198198
$client = new ShipEngineClient();
199-
$apiResponse = $client->post(
200-
"v1/labels/${labelId}/track",
199+
$apiResponse = $client->get(
200+
"v1/labels/$labelId/track",
201201
$config
202202
);
203203

@@ -218,10 +218,9 @@ public function trackUsingCarrierCodeAndTrackingNumber($carrierCode, $trackingNu
218218
{
219219
$config = $this->config->merge($config);
220220
$client = new ShipEngineClient();
221-
$apiResponse = $client->post(
222-
"v1/tracking",
223-
$config,
224-
array('carrier_code' => $carrierCode, 'tracking_number' => $trackingNumber)
221+
$apiResponse = $client->get(
222+
"v1/tracking?carrier_code=$carrierCode&tracking_number=$trackingNumber",
223+
$config
225224
);
226225

227226
return $apiResponse;

src/ShipEngineClient.php

Lines changed: 16 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,14 @@ private function sendRequestWithRetries(
9292
string $method,
9393
string $path,
9494
?array $params,
95-
ShipEngineConfig $config): array
95+
ShipEngineConfig $config
96+
): array
9697
{
9798
$apiResponse = null;
9899
for ($retry = 0; $retry <= $config->retries; $retry++) {
99100
try {
100101
$apiResponse = $this->sendRequest($method, $path, $params, $retry, $config);
102+
break;
101103
} catch (\RuntimeException $err) {
102104
if (($retry < $config->retries) &&
103105
$err instanceof RateLimitExceededException &&
@@ -143,6 +145,7 @@ private function sendRequest(
143145
$client = new Client(
144146
[
145147
'base_uri' => $config->baseUrl,
148+
'timeout' => $config->timeout,
146149
'max_retry_attempts' => $config->retries
147150
]
148151
);
@@ -175,7 +178,7 @@ private function sendRequest(
175178
// $assert->isResponse404($statusCode, $parsedResponse);
176179
// $assert->isResponse429($statusCode, $parsedResponse, $config);
177180
// $assert->isResponse500($statusCode, $parsedResponse);
178-
var_dump($parsedResponse);
181+
179182
return $this->handleResponse($parsedResponse);
180183
}
181184

@@ -188,61 +191,19 @@ private function sendRequest(
188191
*/
189192
private function handleResponse(array $response): array
190193
{
191-
return $response;
192-
194+
if (!isset($response['errors']) || (count($response['errors']) == 0)) {
195+
return $response;
196+
}
193197

194-
// $error = $response['error'];
198+
$error = $response['errors'][0];
195199

196-
// switch ($error['data']['type']) {
197-
// case ErrorType::ACCOUNT_STATUS:
198-
// throw new AccountStatusException(
199-
// $error['message'],
200-
// $response['id'],
201-
// $error['data']['source'],
202-
// $error['data']['type'],
203-
// $error['data']['code']
204-
// );
205-
// case ErrorType::SECURITY:
206-
// throw new SecurityException(
207-
// $error['message'],
208-
// $response['id'],
209-
// $error['data']['source'],
210-
// $error['data']['type'],
211-
// $error['data']['code']
212-
// );
213-
// case ErrorType::VALIDATION:
214-
// throw new ValidationException(
215-
// $error['message'],
216-
// $response['id'],
217-
// $error['data']['source'],
218-
// $error['data']['type'],
219-
// $error['data']['code']
220-
// );
221-
// case ErrorType::BUSINESS_RULES:
222-
// throw new BusinessRuleException(
223-
// $error['message'],
224-
// $response['id'],
225-
// $error['data']['source'],
226-
// $error['data']['type'],
227-
// $error['data']['code']
228-
// );
229-
// case ErrorType::SYSTEM:
230-
// throw new SystemException(
231-
// $error['message'],
232-
// $response['id'],
233-
// $error['data']['source'],
234-
// $error['data']['type'],
235-
// $error['data']['code']
236-
// );
237-
// default:
238-
// throw new ShipEngineException(
239-
// $error['message'],
240-
// $response['id'],
241-
// $error['data']['source'],
242-
// $error['data']['type'],
243-
// $error['data']['code']
244-
// );
245-
// }
200+
throw new ShipEngineException(
201+
$error['message'],
202+
$response['request_id'],
203+
$error['error_source'],
204+
$error['error_type'],
205+
$error['error_code']
206+
);
246207
}
247208

248209
/**

src/ShipEngineConfig.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ final class ShipEngineConfig implements \JsonSerializable
3333
/**
3434
* Default timeout for the ShipEngineClient in seconds as a **DateInterval**.
3535
*/
36-
public const DEFAULT_TIMEOUT = 'PT5S';
36+
public const DEFAULT_TIMEOUT = 'PT10S';
3737

3838

3939
/**

0 commit comments

Comments
 (0)