Name: error-handler
Owner: Middlewares
Description: PSR-15 middleware to handle http errors
Created: 2016-10-03 12:02:54.0
Updated: 2017-12-11 20:02:00.0
Pushed: 2018-01-26 18:40:40.0
Homepage: null
Size: 39
Language: PHP
GitHub Committers
User | Most Recent Commit | # Commits |
---|
Other Committers
User | Most Recent Commit | # Commits |
---|
Middleware to execute a handler if the response returned by the next middlewares has any error (status code 400-599) or throws a Middlewares\HttpErrorException
.
This package provides the Middlewares\HttpErrorException
that you can use to send context-related data to the error handler. The methods setContext(array $data)
and getContext()
methods allows to assign and retrieve the error context data used in the error handler.
This package is installable and autoloadable via Composer as middlewares/error-handler.
oser require middlewares/error-handler
Interop\Http\Server\RequestHandlerInterface;
Psr\Http\Message\ResponseInterface;
Psr\Http\Message\ServerRequestInterface;
s ErrorRequestHandler implements RequestHandlerInterface
public function handle(ServerRequestInterface $request): ResponseInterface
{
//Get the error info as an instance of Middlewares\HttpErrorException
$error = $request->getAttribute('error');
//The error can contains context data that you can use, for example for PSR-3 loggin
Logger::error("There's an error", $error->getContext());
//Any output is captured and added to the response's body
echo $error->getMessage();
return (new Response())->withStatus($error->getCode());
}
patcher = new Dispatcher([
new Middlewares\ErrorHandler(new ErrorRequestHandler()),
function ($request, $next) {
$user = Session::signup($request);
if ($user->isNotAllowed()) {
//Send an exception adding context data
throw Middlewares\HttpErrorException::create(401, [
'user' => $user,
'request' => $request
]);
}
return $next($request);
}
ponse = $dispatcher->dispatch(new ServerRequest());
__construct(Interop\Http\Server\RequestHandlerInterface $handler = null)
The request handler used to generate the response. If it's not provided, use the default that provides different outputs for different formats.
catchExceptions(true)
Used to catch also other exceptions than Middlewares\HttpErrorException
and create 500
error responses. Disabled by default.
statusCode(callable $statusCodeValidator)
By default, all responses with status code between 400-599 are interpreted as error responses. But it's possible to change this behaviour, to handle, for example, only 404 errors providing a validator:
patcher = new Dispatcher([
(new Middlewares\ErrorHandler($handler))
->statusCode(function ($code) {
return $code === 404; //handle only 404 errors
})
attribute(string $attribute)
The attribute name used to store the instance of Middlewares\HttpErrorException
with the error info in the server request. By default is error
.
Please see CHANGELOG for more information about recent changes and CONTRIBUTING for contributing details.
The MIT License (MIT). Please see LICENSE for more information.