-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathException.php
More file actions
56 lines (49 loc) · 2.31 KB
/
Exception.php
File metadata and controls
56 lines (49 loc) · 2.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?php declare(strict_types=1);
use MpApiClient\Common\Authenticators\ClientIdAuthenticator;
use MpApiClient\Exception\BadResponseException;
use MpApiClient\Exception\ForbiddenException;
use MpApiClient\Exception\MpApiException;
use MpApiClient\Exception\NotFoundException;
use MpApiClient\Exception\TooManyRequestsException;
use MpApiClient\Exception\UnauthorizedException;
use MpApiClient\MpApiClient;
use MpApiClient\MpApiClientOptions;
$options = new MpApiClientOptions(
new ClientIdAuthenticator('my-client-id')
);
$client = MpApiClient::createFromOptions('my-app-name', $options);
//
// Basic exception handling example
//
try {
$orderList = $client->orders()->getV2(1234567890); // or use $client->orders()->get(1234567890); for old V1 version
} catch (UnauthorizedException $e) {
// You provided invalid client id, or forgot to provide authenticator middleware altogether
echo 'API authorization failed with error: ' . $e->getMessage();
} catch (TooManyRequestsException $e) {
// Too many requests were sent to API and you got rate limited
// Current max limit of requests for window
echo $e->getResponse()->getHeaderLine('X-RateLimit-Limit') . PHP_EOL;
// Remaining amount of requests in the time window
echo $e->getResponse()->getHeaderLine('X-RateLimit-Remaining') . PHP_EOL;
// Amount of seconds before the rate limit window resets
echo $e->getResponse()->getHeaderLine('X-RateLimit-Reset') . PHP_EOL;
} catch (NotFoundException $e) {
echo 'Order with id 1234567890 does not exist.';
} catch (ForbiddenException $e) {
echo 'Request to API endpoint was forbidden.';
} catch (BadResponseException $e) {
echo 'Request failed with error: ' . $e->getMessage() . PHP_EOL;
foreach ($e->getErrorCodes() as $errorCode) {
echo 'Error message: ' . $errorCode->getMessage() . PHP_EOL;
echo 'Error code: ' . $errorCode->getCode() . PHP_EOL;
echo 'Error attributes: ' . print_r($errorCode->getAttributes(), true) . PHP_EOL;
echo PHP_EOL;
}
// print response returned from API
echo $e->getResponse()->getBody()->getContents();
} catch (MpApiException $e) {
echo 'Unexpected error occurred while loading order list: ' . $e->getMessage();
} catch (Exception $e) {
echo 'Unexpected generic error occurred while loading order list: ' . $e->getMessage();
}