HTTP Exceptions
If you are creating API, SPL Exceptions are sometimes not the best choice to describe your problem. That is where HTTP Exceptions can be helpful., (*1)
Requested data not found? Unauthorized request? XML instead of JSON received? Throw relevant exception!, (*2)
Installation
Just add it as dependency to your project:, (*3)
composer require pavelsterba/http-exceptions
Usage
All exceptions can be thrown without any additional information - message and code are predefined., (*4)
try {
throw new HttpException\ServerError\InternalServerErrorException();
} catch (HttpException\HttpException $e) {
echo $e->getMessage(); // 500 Internal Server Error
echo $e->getCode(); // 500
}
To get customized instance of exception, you can pass parameters to exception as usual or use static function get(), where you have to specify only message and previous exception:, (*5)
use HttpException\ServerError\InternalServerErrorException;
try {
// ...
} catch (Exception $ex) {
throw InternalServerErrorException::get("Server down, sorry.", $ex);
}
Structure
Your API can be fully exception driven since all HTTP statuses from RFC 9110 are implemented as separated exceptions with following hierarchy:, (*6)
Exception
โโ HttpException\HttpException
โโ HttpException\InformationalException
โ โโ HttpException\Informational\ContinueException
โ โโ HttpException\Informational\SwitchingProtocolsException
โ โโ HttpException\Informational\ProcessingException
โ โโ HttpException\Informational\EarlyHintsException
โโ HttpException\SuccessfulException
โ โโ HttpException\Successful\OKException
โ โโ HttpException\Successful\CreatedException
โ โโ HttpException\Successful\AcceptedException
โ โโ HttpException\Successful\NonAuthoritativeInformationException
โ โโ HttpException\Successful\NoContentException
โ โโ HttpException\Successful\ResetContentException
โ โโ HttpException\Successful\PartialContentException
โ โโ HttpException\Successful\MultiStatusException
โ โโ HttpException\Successful\AlreadyReportedException
โ โโ HttpException\Successful\IMUsedException
โโ HttpException\RedirectionException
โ โโ HttpException\Redirection\MultipleChoicesException
โ โโ HttpException\Redirection\MovedPermanentlyException
โ โโ HttpException\Redirection\FoundException
โ โโ HttpException\Redirection\SeeOtherException
โ โโ HttpException\Redirection\NotModifiedException
โ โโ HttpException\Redirection\UseProxyException
โ โโ HttpException\Redirection\TemporaryRedirectException
โ โโ HttpException\Redirection\PermanentRedirectException
โโ HttpException\ClientErrorException
โ โโ HttpException\ClientError\BadRequestException
โ โโ HttpException\ClientError\UnauthorizedException
โ โโ HttpException\ClientError\PaymentRequiredException
โ โโ HttpException\ClientError\ForbiddenException
โ โโ HttpException\ClientError\NotFoundException
โ โโ HttpException\ClientError\MethodNotAllowedException
โ โโ HttpException\ClientError\NotAcceptableException
โ โโ HttpException\ClientError\ProxyAuthenticationRequiredException
โ โโ HttpException\ClientError\RequestTimeoutException
โ โโ HttpException\ClientError\ConflictException
โ โโ HttpException\ClientError\GoneException
โ โโ HttpException\ClientError\LengthRequiredException
โ โโ HttpException\ClientError\PreconditionFailedException
โ โโ HttpException\ClientError\PayloadTooLargeException
โ โโ HttpException\ClientError\URITooLongException
โ โโ HttpException\ClientError\UnsupportedMediaTypeException
โ โโ HttpException\ClientError\RangeNotSatisfiableException
โ โโ HttpException\ClientError\ExpectationFailedException
โ โโ HttpException\ClientError\IMaTeapotException
โ โโ HttpException\ClientError\MisdirectedRequestException
โ โโ HttpException\ClientError\UnprocessableEntityException
โ โโ HttpException\ClientError\LockedException
โ โโ HttpException\ClientError\FailedDependencyException
โ โโ HttpException\ClientError\TooEarlyException
โ โโ HttpException\ClientError\UpgradeRequiredException
โ โโ HttpException\ClientError\PreconditionRequiredException
โ โโ HttpException\ClientError\TooManyRequestsException
โ โโ HttpException\ClientError\RequestHeaderFieldsTooLargeException
โ โโ HttpException\ClientError\UnavailableForLegalReasonsException
โโ HttpException\ServerErrorException
โโ HttpException\ServerError\InternalServerErrorException
โโ HttpException\ServerError\NotImplementedException
โโ HttpException\ServerError\BadGatewayException
โโ HttpException\ServerError\ServiceUnavailableException
โโ HttpException\ServerError\GatewayTimeoutException
โโ HttpException\ServerError\HTTPVersionNotSupportedException
โโ HttpException\ServerError\VariantAlsoNegotiatesException
โโ HttpException\ServerError\InsufficientStorageException
โโ HttpException\ServerError\LoopDetectedException
โโ HttpException\ServerError\NotExtendedException
โโ HttpException\ServerError\NetworkAuthenticationRequiredException