aatis/error-handler

There is no license information available for the latest version (1.0.0) of this package.

Error handler of Aatis

1.0.0 2024-02-06 07:50 UTC

This package is auto-updated.

Last update: 2024-04-06 08:07:57 UTC


README

Installation

composer require aatis/error-handler

Usage

Initialization

To initialize the error handler, you need to pass the following parameters:

  • a logger service that implements the Psr\Log\LoggerInterface
  • an instance of ErrorCodeBag service of the package
  • an instance of ExceptionCodeBag service of the package
ErrorHandler::initialize(
    new Logger(),
    new ErrorCodeBag(),
    new ExceptionCodeBag(),
);

Logger

Each time an error is triggered or an exception is thrown, the ErrorHandler will log a message with the logger service you provided.

ErrorCodeBag

The ErrorCodeBag service store 15 error codes corresponding to the 15 error levels of PHP :

  • 1 => 'E_ERROR'
  • 2 => 'E_WARNING'
  • 4 => 'E_PARSE'
  • 8 => 'E_NOTICE'
  • 16 => 'E_CORE_ERROR'
  • 32 => 'E_CORE_WARNING'
  • 64 => 'E_COMPILE_ERROR'
  • 128 => 'E_COMPILE_WARNING'
  • 256 => 'E_USER_ERROR'
  • 512 => 'E_USER_WARNING'
  • 1024 => 'E_USER_NOTICE'
  • 2048 => 'E_STRICT'
  • 4096 => 'E_RECOVERABLE_ERROR'
  • 8192 => 'E_DEPRECATED'
  • 16384 => 'E_USER_DEPRECATED'

You can not add or override any error codes from this bag.

ExceptionCodeBag

The ExceptionCodeBag service store any sort of exception code you want to use in your application. By default, a list with all the 400 and 500 error codes is provided, but you can extand and/or override it :

  • 0 => 'Basic Error'
  • 400 => 'Bad Request'
  • 401 => 'Unauthorized'
  • 402 => 'Payment Required Experimental'
  • 403 => 'Forbidden'
  • 404 => 'Not Found'
  • 405 => 'Method Not Allowed'
  • 406 => 'Not Acceptable'
  • 407 => 'Proxy Authentication Required'
  • 408 => 'Request Timeout'
  • 409 => 'Conflict'
  • 410 => 'Gone'
  • 411 => 'Length Required'
  • 412 => 'Precondition Failed'
  • 413 => 'Payload Too Large'
  • 414 => 'URI Too Long'
  • 415 => 'Unsupported Media Type'
  • 416 => 'Range Not Satisfiable'
  • 417 => 'Expectation Failed'
  • 418 => 'I'm a teapot'
  • 421 => 'Misdirected Request'
  • 422 => 'Unprocessable Content'
  • 423 => 'Locked'
  • 424 => 'Failed Dependency'
  • 425 => 'Too Early Experimental'
  • 426 => 'Upgrade Required'
  • 428 => 'Precondition Required'
  • 429 => 'Too Many Requests'
  • 431 => 'Request Header Fields Too Large'
  • 451 => 'Unavailable For Legal Reasons'
  • 500 => 'Internal Server Error'
  • 501 => 'Not Implemented'
  • 502 => 'Bad Gateway'
  • 503 => 'Service Unavailable'
  • 504 => 'Gateway Timeout'
  • 505 => 'HTTP Version Not Supported'
  • 506 => 'Variant Also Negotiates'
  • 507 => 'Insufficient Storage'
  • 508 => 'Loop Detected'
  • 510 => 'Not Extended'
  • 511 => 'Network Authentication Required'

You can add or override any error codes from this bag by creating a custom string enum like the following template :

enum ExampleExceptionCodeEnum: string
{
    case _404 = 'Custom Not Found';
    case _30 = 'Custom Error 30';
}

Then, pass it to the ExceptionCodeBag.

You can pass as many enums as you want into it constructor

new ExceptionCodeBag([
    ExampleExceptionCodeEnum::class,
    OtherExampleExceptionCodeEnum::class
]);

To pass a specific code to an exception, you can do the following :

throw new Exception('My custom message', 30);

With Aatis Framework

Requirements

Add the ErrorCodeBag and ExceptionCodeBag services to the Container:

# In config/services.yaml file :

include_services:
    - 'Aatis\ErrorHandler\Service\ErrorCodeBag'
    - 'Aatis\ErrorHandler\Service\ExceptionCodeBag'  

ExceptionCodeBag

If you want to add or override any error codes from the ExceptionCodeBag, you can do the following :

# In config/services.yaml file :

services:
    Aatis\ErrorHandler\Service\ExceptionCodeBag:
        arguments:
            extraExceptionCodeEnums:
                - 'Namespace\To\ExampleExceptionCodeEnum'
                - 'Namespace\To\OtherExampleExceptionCodeEnum'