fyre/error

An error handling library.

v4.0.4 2024-06-25 06:58 UTC

This package is auto-updated.

Last update: 2024-06-25 06:59:05 UTC


README

FyreError

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;

Methods

Get Exception

Get the current Exception.

$exception = ErrorHandler::getException();

Get Renderer

Get the error renderer.

$renderer = ErrorHandler::getRenderer();

Handle

Handle an Exception.

$response = ErrorHandler::handle($exception);

Register

Register the error handler.

  • $options is an array containing configuration options.
    • log is a boolean indicating whether to log errors, and will default to false.
    • level is an integer representing the error_reporting level.
ErrorHandler::register($options);

Render

Render an Exception.

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;
$middleware = new ErrorHandlerMiddleware();

Process

$response = $middleware->process($request, $handler);

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;