PHP异常的HTTP状态代码库推荐:HTTP Exceptions
HTTP状态代码不仅仅是404 Not Found和500 Server Internal Error,还有很多。
在你的API开发过程中,你迟早会面临这样的情况,你将会使用更多的HTTP状态码。
你可以在你的项目中逐一实现它们,或者你可以使用HTTP Exceptions库,它最近发布了包含所有HTTP状态代码的2.0主要版本。
composer安装
只需将其作为依赖项添加到你的项目中:
composer require pavelsterba/http-exceptions
使用方法
所有的异常都可以被抛出,不需要任何额外的信息--消息和代码都是预定义的,你只需要把它们发送到终端。
try {
throw new HttpException\ServerError\InternalServerErrorException();
} catch (HttpException\HttpException $e) {
echo $e->getMessage(); // 500 Internal Server Error
echo $e->getCode(); // 500
}
如果你想自定义错误信息或附加以前的异常,
你当然可以把它们作为参数传递给构造函数(但你必须同时传递状态代码),
或者你可以使用静态get方法来自定义:
use HttpException\ServerError\InternalServerErrorException;
try {
// ...
} catch (Exception $ex) {
throw InternalServerErrorException::get("Server down, sorry.", $ex);
}
异常的层次结构
从2.0版本开始,你可以让你的API完全由异常驱动,因为信息、成功和重定向代码的异常已经实现。
完整的层次结构或异常是:
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
考虑到这一点,你可以随心所欲地捕捉它们。
try {
// ...
} catch (HttpException\ClientError\NotFoundException $e) {
// Only NotFoundException catched
} catch (HttpException\ClientErrorException $e) {
// All ClientError exceptions
} catch (HttpException\HttpException $e) {
// All HTTP exceptions from library
} catch (Exception $e) {
// Everything else...
}
相关文章