aatis / error-handler
此软件包最新版本(1.0.0)没有提供许可证信息。
Aatis错误处理器
1.0.0
2024-02-06 07:50 UTC
Requires
- php: ^8.2
- psr/log: ^3.0
README
安装
composer require aatis/error-handler
用法
初始化
要初始化错误处理器,您需要传递以下参数
- 实现
Psr\Log\LoggerInterface
的日志服务 - 包的
ErrorCodeBag
服务实例 - 包的
ExceptionCodeBag
服务实例
ErrorHandler::initialize( new Logger(), new ErrorCodeBag(), new ExceptionCodeBag(), );
日志记录器
每次触发错误或抛出异常时,ErrorHandler
都会使用您提供的日志服务记录一条消息。
ErrorCodeBag
ErrorCodeBag
服务存储了与PHP 15个错误级别相对应的15个错误代码
- 1 => 'E_ERROR'
- 2 => 'E_WARNING'
- 4 => 'E_PARSE'
- 8 => 'E_NOTICE'
- 16 => 'E_CORE_ERROR'
- 32 => 'E_CORE_WARNING'
- 64 => 'E_COMPILE_ERROR'
- 128 => 'E_COMPILE_WARNING'
- 256 => 'E_USER_ERROR'
- 512 => 'E_USER_WARNING'
- 1024 => 'E_USER_NOTICE'
- 2048 => 'E_STRICT'
- 4096 => 'E_RECOVERABLE_ERROR'
- 8192 => 'E_DEPRECATED'
- 16384 => 'E_USER_DEPRECATED'
您不能从此包中添加或覆盖任何错误代码。
ExceptionCodeBag
ExceptionCodeBag
服务存储了您在应用程序中想要使用的任何类型的异常代码。默认情况下,提供了一个包含所有400和500错误代码的列表,但您可以扩展和/或覆盖它
- 0 => 'Basic Error'
- 400 => 'Bad Request'
- 401 => 'Unauthorized'
- 402 => 'Payment Required Experimental'
- 403 => 'Forbidden'
- 404 => 'Not Found'
- 405 => 'Method Not Allowed'
- 406 => 'Not Acceptable'
- 407 => 'Proxy Authentication Required'
- 408 => 'Request Timeout'
- 409 => 'Conflict'
- 410 => 'Gone'
- 411 => 'Length Required'
- 412 => 'Precondition Failed'
- 413 => 'Payload Too Large'
- 414 => 'URI Too Long'
- 415 => 'Unsupported Media Type'
- 416 => 'Range Not Satisfiable'
- 417 => 'Expectation Failed'
- 418 => 'I'm a teapot'
- 421 => 'Misdirected Request'
- 422 => 'Unprocessable Content'
- 423 => 'Locked'
- 424 => 'Failed Dependency'
- 425 => 'Too Early Experimental'
- 426 => 'Upgrade Required'
- 428 => 'Precondition Required'
- 429 => 'Too Many Requests'
- 431 => 'Request Header Fields Too Large'
- 451 => 'Unavailable For Legal Reasons'
- 500 => 'Internal Server Error'
- 501 => 'Not Implemented'
- 502 => 'Bad Gateway'
- 503 => 'Service Unavailable'
- 504 => 'Gateway Timeout'
- 505 => 'HTTP Version Not Supported'
- 506 => 'Variant Also Negotiates'
- 507 => 'Insufficient Storage'
- 508 => 'Loop Detected'
- 510 => 'Not Extended'
- 511 => 'Network Authentication Required'
您可以通过创建以下模板的定制字符串枚举来添加或覆盖此包中的任何错误代码
enum ExampleExceptionCodeEnum: string { case _404 = 'Custom Not Found'; case _30 = 'Custom Error 30'; }
然后,将其传递给ExceptionCodeBag
。
您可以将任意数量的枚举传递给它构造函数
new ExceptionCodeBag([ ExampleExceptionCodeEnum::class, OtherExampleExceptionCodeEnum::class ]);
要将特定代码传递给异常,您可以这样做
throw new Exception('My custom message', 30);
使用Aatis框架
需求
将ErrorCodeBag
和ExceptionCodeBag
服务添加到Container
# In config/services.yaml file : include_services: - 'Aatis\ErrorHandler\Service\ErrorCodeBag' - 'Aatis\ErrorHandler\Service\ExceptionCodeBag'
ExceptionCodeBag
如果您想添加或覆盖来自 ExceptionCodeBag
的任何错误代码,您可以执行以下操作
# In config/services.yaml file : services: Aatis\ErrorHandler\Service\ExceptionCodeBag: arguments: extraExceptionCodeEnums: - 'Namespace\To\ExampleExceptionCodeEnum' - 'Namespace\To\OtherExampleExceptionCodeEnum'