2017 © Pedro Pelรกez
 

library http-exceptions

HTTP status code exceptions for PHP.

image

pavelsterba/http-exceptions

HTTP status code exceptions for PHP.

  • Friday, December 18, 2015
  • by pavelsterba
  • Repository
  • 1 Watchers
  • 8 Stars
  • 7,003 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 3 Versions
  • 15 % Grown

The README.md

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

The Versions

18/12 2015

dev-master

9999999-dev https://github.com/pavelsterba/http-exceptions

HTTP status code exceptions for PHP.

  Sources   Download

MIT

by Pavel Sterba

php http exceptions

18/12 2015

v1.1

1.1.0.0 https://github.com/pavelsterba/http-exceptions

HTTP status code exceptions for PHP.

  Sources   Download

MIT

by Pavel Sterba

php http exceptions

30/09 2015

v1.0

1.0.0.0 https://github.com/pavelsterba/http-exceptions

HTTP status code exceptions for PHP.

  Sources   Download

MIT

by Pavel Sterba

php http exceptions