fyre/error

An error handling library.

v5.0 2024-11-12 14:11 UTC

This package is auto-updated.

Last update: 2024-11-12 14:11:55 UTC


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

$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

$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;