Skip to content

Latest commit

 

History

History
71 lines (49 loc) · 2.64 KB

Exception.md

File metadata and controls

71 lines (49 loc) · 2.64 KB

Exceptions

Client contains custom exceptions, for easier error handling in your application.

All custom exceptions extend generic MpApiException.

Some methods might throw other native PHP exceptions (i.e., InvalidArgumentException), which do not extend MpApiException. Such methods always have appropriate @throws tags.

  • Generic exception which almost all exceptions thrown in this client extend
  • Thrown when method called expects data of a specific type, but received a different one
  • Thrown when API responded with 4xx or 5xx status code that could not be translated to more specific exceptions
  • Usually indicates an unknown/unexpected error occurred
  • Thrown when API returns 403 status code
  • Usually returned on endpoints your account does not have access to
  • Thrown when API returns 404 status code
  • This exception should never be thrown for endpoints with static url (i.e., enums/lists)
  • Thrown when API returns 401 status code
  • Reasons might include
    • you forgot to provide authenticator middleware
    • you provided invalid credentials to authenticator middleware
    • disabled user account
  • Thrown when API returns 429 status code
  • In that case, you have been rate limited by the API and should slow down your request rate
  • API returns rate limit information as part of response headers, which you can easily check

Example of handling some errors

<?php declare(strict_types=1);

use MpApiClient\Common\Interfaces\ChecksClientInterface;use MpApiClient\Exception\BadResponseException;
use MpApiClient\Exception\IncorrectDataTypeException;
use MpApiClient\Exception\MpApiException;
use MpApiClient\Exception\TooManyRequestsException;

try {
    /** @var ChecksClientInterface $checksClient */
    $brands = $checksClient->getDeliveryErrors();
} catch (TooManyRequestsException $e) {
    var_dump($e->getResponse()->getHeaders());
} catch (BadResponseException $e) {
    var_dump($e->getErrorCodes());
} catch (IncorrectDataTypeException $e) {
    var_dump('Incorrect type: ' . $e->getMessage());
} catch (MpApiException $e) {
    var_dump('Generic exception: ' . $e->getMessage());
}

See more examples here