fyre / error
An error handling library.
Requires
- fyre/console: ^4.0
- fyre/container: ^1.0
- fyre/log: ^5.0
- fyre/middleware: ^5.0
- fyre/server: ^3.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.59
- fyre/php-cs-fixer-config: ^1.0
- phpunit/phpunit: ^11
README
FyreError is a free, open-source error handling library for PHP.
Table Of Contents
Installation
Using Composer
composer require fyre/error
In PHP:
use Fyre\Error\ErrorHandler;
Basic Usage
$container
is a Container.$io
is a Console.$logManager
is a Logmanager.$config
is a Config.
$errorHandler = new ErrorHandler($container, $io, $logManager, $config);
Default configuration options will be resolved from the "Error" key in the Config.
Autoloading
It is recommended to bind the ErrorHandler to the Container as a singleton.
$container->singleton(ErrorHandler::class);
Any dependencies will be injected automatically when loading from the Container.
$errorHandler = $container->use(ErrorHandler::class);
Methods
Get Exception
Get the current Exception.
$exception = $errorHandler->getException();
Get Renderer
Get the error renderer.
$renderer = $errorHandler->getRenderer();
Register
Register the error handler.
$errorHandler->register();
Render
Render an Exception.
$response = $errorHandler->render($exception);
Set Renderer
Set the error renderer.
$renderer
is a Closure that accepts an Exception as the first argument.
$errorHandler->setRenderer($renderer);
The renderer should return a ClientResponse or a string.
Middleware
use Fyre\Error\Middleware\ErrorHandlerMiddleware;
$errorHandler
is an ErrorHandler.
$middleware = new ErrorHandlerMiddleware($errorHandler);
Any dependencies will be injected automatically when loading from the Container.
$middleware = $container->use(ErrorHandlerMiddleware::class);
Handle
$request
is a ServerRequest.$next
is a Closure.
$response = $middleware->handle($request, $next);
This method will return a ClientResponse.
Exceptions
Custom exceptions can be created by extending the Fyre\Error\Exceptions\Exception
class.
$message
is a string representing the error message.$code
is a number representing the error code, and will default to 500.$previous
is an Exception representing the previous exception, and will default to null.
new Exception($message, $code, $previous);
Http Exceptions
Bad Request
400 Bad Request error.
use Fyre\Error\Exceptions\BadRequestException;
Unauthorized
401 Unauthorized error.
use Fyre\Error\Exceptions\UnauthorizedException;
Forbidden
403 Forbidden error.
use Fyre\Error\Exceptions\Forbidden;
Not Found
404 Not Found error.
use Fyre\Error\Exceptions\NotFoundException;
Method Not Allowed
405 Method Not Allowed error.
use Fyre\Error\Exceptions\MethodNotAllowedException;
Not Acceptable
406 Not Acceptable error.
use Fyre\Error\Exceptions\NotAcceptableException;
Conflict
409 Conflict error.
use Fyre\Error\Exceptions\ConflictException;
Gone
410 Gone error.
use Fyre\Error\Exceptions\GoneException;
Internal Server
500 Internal Server error.
use Fyre\Error\Exceptions\InternalServerException;
Not Implemented
501 Not Implemented error.
use Fyre\Error\Exceptions\NotImplementedException;
Service Unavailable
503 Service Unavailable error.
use Fyre\Error\Exceptions\ServiceUnavailableException;