vimpublishing / psr15-middleware-error-handler
无依赖中间件,用于捕获和处理器任何可抛出的异常,并使用专用错误处理器
1.0.0
2019-06-18 12:58 UTC
Requires
- php: >=7.2
- fig/http-message-util: *@stable
- psr/http-server-middleware: *@stable
Requires (Dev)
- phpunit/phpunit: *@stable
- vmpublishing/psr15-middleware-test-helpers: *@stable
This package is auto-updated.
Last update: 2024-09-19 01:19:17 UTC
README
WHAT
尽可能低依赖的 PSR-15 错误处理中间件。
为了生产效率,我们喜欢一个可以捕获和将错误处理成更友好格式的中间件(如果可能的话)
这包括两个处理器实现。一个只是重新抛出错误(用于开发目的),另一个响应预定义文件的内容和正确的状态码(用于生产使用)
安装
要安装,请使用 composer require vmpublishing/psr15-middleware-error-handler:*@stable
使用
这很简单:定义你的处理器并添加中间件;
use VM\ErrorHandler\Services\RethrowHandler;
$handler = new RethrowHandler();
$middleware = new ErrorProcessor($handler);
// and for slim, given $app
$app->add($middleware);
// or just add it on the routes you want it on
对于 StaticFileHandler,你需要至少传递一个文件和一个空的 Response 对象;
use VM\ErrorHandler\Services\StaticFileHandler;
$handler = new StaticFileHandler('/path/to/500.html', $emptyResponse);
$middleware = new ErrorProcessor($handler);
// and for slim, given $app
$app->add($middleware);
// ...
要使用特定的 ErrorCodes,你需要使用这个库中的 Http 异常,例如:NotFound