2017 © Pedro Peláez
 

library errorhandler

PHP library for handling exceptions and errors.

image

josantonius/errorhandler

PHP library for handling exceptions and errors.

  • PHP
  • 2 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 11 Versions
  • 5 % Grown

The README.md

PHP ErrorHandler library

Latest Stable Version License Total Downloads CI CodeCov PSR1 PSR4 PSR12, (*1)

Translations: Español, (*2)

PHP library for handling exceptions., (*3)

To handle exceptions you can use the exception-handler library., (*4)



Requirements

  • Operating System: Linux | Windows., (*5)

  • PHP versions: 8.1., (*6)

Installation

The preferred way to install this extension is through Composer., (*7)

To install PHP ErrorHandler library, simply:, (*8)

composer require josantonius/error-handler

The previous command will only install the necessary files, if you prefer to download the entire source code you can use:, (*9)

composer require josantonius/error-handler --prefer-source

You can also clone the complete repository with Git:, (*10)

git clone https://github.com/josantonius/php-error-handler.git

Available Classes

ErrorException Class

Josantonius\ErrorHandler\ErrorException Extends ErrorException, (*11)

Gets error file:, (*12)

public function getFile(): string;

Gets error level:, (*13)

public function getLevel(): int;

Gets error file line:, (*14)

public function getLine(): int;

Gets error message:, (*15)

public function getMessage(): string;

Gets error name:, (*16)

public function getName(): string;

ErrorHandled Class

Josantonius\ErrorHandler\ErrorHandled, (*17)

Gets error file:, (*18)

public function getFile(): string;

Gets error level:, (*19)

public function getLevel(): int;

Gets error file line:, (*20)

public function getLine(): int;

Gets error message:, (*21)

public function getMessage(): string;

Gets error name:, (*22)

public function getName(): string;

ErrorHandler Class

Josantonius\ErrorHandler\ErrorHandler, (*23)

Convert errors to exceptions:, (*24)

/**
 * The errors will be thrown from the ErrorException instance.
 * 
 * @param int[] $errorLevel Define the specific error levels that will become exceptions.
 * 
 * @throws WrongErrorLevelException if error level is not valid.
 * 
 * @see https://www.php.net/manual/en/errorfunc.constants.php to view available error levels.
 */
public function convertToExceptions(int ...$errorLevel): ErrorHandler;

Convert errors to exceptions except for some of them:, (*25)

/**
 * The errors will be thrown from the ErrorException instance.
 * 
 * @param int[] $errorLevel Define the specific error levels that will become exceptions.
 * 
 * @throws WrongErrorLevelException if error level is not valid.
 * 
 * @see https://www.php.net/manual/en/errorfunc.constants.php to view available error levels.
 */
public function convertToExceptionsExcept(int ...$errorLevel): ErrorHandler;

Register error handler function:, (*26)

/**
 * The error handler will receive the ErrorHandled object.
 * 
 * @see https://www.php.net/manual/en/functions.first_class_callable_syntax.php
 */
public function register(callable $callback): ErrorHandler;

Use error reporting to determine which errors are handled:, (*27)

/**
 * If the setting value in error_reporting() is used to determine which errors are handled.
 *
 * If this method is not used, all errors will be sent to the handler.
 *
 * @see https://www.php.net/manual/en/function.error-reporting.php
 */
public function useErrorReportingLevel(): ErrorHandler;

Exceptions Used

use Josantonius\ErrorHandler\Exceptions\WrongErrorLevelException;

Usage

Examples of use for this library:, (*28)

Convert all errors to exceptions

use Josantonius\ErrorHandler\ErrorHandler;

$errorHandler = new ErrorHandler();

$errorHandler->convertToExceptions();

// All errors will be converted to exceptions.

Convert certain errors to exceptions

use Josantonius\ErrorHandler\ErrorHandler;

$errorHandler = new ErrorHandler();

$errorHandler->convertToExceptions(E_USER_ERROR, E_USER_WARNING);

// Only E_USER_ERROR and E_USER_WARNING will be converted to exceptions.

Convert all errors to exceptions except for some of them

use Josantonius\ErrorHandler\ErrorHandler;

$errorHandler = new ErrorHandler();

$errorHandler->convertToExceptionsExcept(E_USER_DEPRECATED, E_USER_NOTICE);

// All errors except E_USER_DEPRECATED and E_USER_NOTICE will be converted to exceptions.

Convert to exceptions using error reporting level

use Josantonius\ErrorHandler\ErrorHandler;

error_reporting(E_USER_ERROR);

$errorHandler = new ErrorHandler();

$errorHandler->convertToExceptions()->useErrorReportingLevel();

// Only E_USER_ERROR will be converted to exception.

Convert to exceptions and use an exception handler

use ErrorException;
use Josantonius\ErrorHandler\ErrorHandler;

set_exception_handler(function (ErrorException $exception) {
    var_dump([
        'level'   => $exception->getLevel(),
        'message' => $exception->getMessage(),
        'file'    => $exception->getFile(),
        'line'    => $exception->getLine(),
        'name'    => $exception->getName(),
    ]);
});

$errorHandler = new ErrorHandler();

$errorHandler->convertToExceptions();

// All errors will be converted to exceptions.

Register an error handler function

use Josantonius\ErrorHandler\ErrorHandled;
use Josantonius\ErrorHandler\ErrorHandler;

function handler(Errorhandled $errorHandled): void {
    var_dump([
        'level'   => $errorHandled->getLevel(),
        'message' => $errorHandled->getMessage(),
        'file'    => $errorHandled->getFile(),
        'line'    => $errorHandled->getLine(),
        'name'    => $errorHandled->getName(),
    ]);
 }

$errorHandler = new ErrorHandler();

$errorHandler->register(
    callback: handler(...)
);

// All errors will be converted to exceptions.

Register error handler function and convert to exceptions

use Josantonius\ErrorHandler\ErrorHandled;
use Josantonius\ErrorHandler\ErrorHandler;

class Handler {
    public static function errors(Errorhandled $exception): void { /* do something */ }
}

$errorHandler = new ErrorHandler();

$errorHandler->register(
    callback: Handler::errors(...)
)->convertToExceptions();

// The error will be sent to the error handler and then throw the exception.

Register error handler function, convert to exceptions and use error reporting level

error_reporting(E_USER_ERROR);

class Handler {
    public function errors(Errorhandled $exception): void { /* do something */ }
}

$handler = new Handler();

$errorHandled->register(
    callback: $handler->errors(...),
)->convertToExceptions()->useErrorReportingLevel();

// Only E_USER_ERROR will be passed to the handler and converted to exception.

Tests

To run tests you just need composer and to execute the following:, (*29)

git clone https://github.com/josantonius/php-error-handler.git
cd php-error-handler
composer install

Run unit tests with PHPUnit:, (*30)

composer phpunit

Run code standard tests with PHPCS:, (*31)

composer phpcs

Run PHP Mess Detector tests to detect inconsistencies in code style:, (*32)

composer phpmd

Run all previous tests:, (*33)

composer tests

TODO

  • [ ] Add new feature
  • [ ] Improve tests
  • [ ] Improve documentation
  • [ ] Improve English translation in the README file
  • [ ] Refactor code for disabled code style rules (see phpmd.xml and phpcs.xml)

Changelog

Detailed changes for each release are documented in the release notes., (*34)

Contribution

Please make sure to read the Contributing Guide, before making a pull request, start a discussion or report a issue., (*35)

Thanks to all contributors! :heart:, (*36)

If this project helps you to reduce your development time, you can sponsor me to support my open source work :blush:, (*37)

License

This repository is licensed under the MIT License., (*38)

Copyright © 2016-present, Josantonius, (*39)

The Versions

16/01 2018

dev-master

9999999-dev

PHP library for handling exceptions and errors.

  Sources   Download

MIT

The Requires

  • php ^5.6 || ^7.0

 

The Development Requires

php exception hhvm error handler error throwable exception handler throw

16/01 2018

1.1.8

1.1.8.0

PHP library for handling exceptions and errors.

  Sources   Download

MIT

The Requires

  • php ^5.6 || ^7.0

 

The Development Requires

php exception hhvm error handler error throwable exception handler throw

12/11 2017

1.1.7

1.1.7.0

PHP library for handling exceptions and errors.

  Sources   Download

MIT

The Requires

  • php ^5.6 || ^7.0

 

The Development Requires

php exception hhvm error handler error throwable exception handler throw

30/10 2017

1.1.6

1.1.6.0

PHP library for handling exceptions and errors.

  Sources   Download

MIT

The Requires

  • php ^5.6 || ^7.0

 

The Development Requires

php exception hhvm error handler error throwable exception handler throw

17/09 2017

1.1.5

1.1.5.0

PHP library for handling exceptions and errors.

  Sources   Download

MIT

The Requires

  • php ^5.6 || ^7.0

 

The Development Requires

php exception hhvm error handler error throwable exception handler throw

18/07 2017

1.1.4

1.1.4.0

PHP library for handling exceptions and errors.

  Sources   Download

MIT

The Requires

  • php ^5.6 || ^7.0

 

php exception hhvm error handler error throwable exception handler throw

15/05 2017

1.1.3

1.1.3.0

PHP library for handling exceptions and errors.

  Sources   Download

MIT

The Requires

  • php ^5.6 || ^7.0

 

php exception hhvm error handler error throwable exception handler throw

20/03 2017

1.1.2

1.1.2.0

PHP library for handling exceptions and errors.

  Sources   Download

MIT

The Requires

  • php ^5.6 || ^7.0

 

php exception hhvm error handler error throwable exception handler throw

04/03 2017

1.1.1

1.1.1.0

PHP library for handling exceptions and errors.

  Sources   Download

MIT

The Requires

  • php >=5.6

 

php exception hhvm error handler error throwable exception handler throw

30/01 2017

1.1.0

1.1.0.0

PHP library for handling exceptions and errors.

  Sources   Download

MIT

The Requires

  • php >=5.6

 

php exception hhvm error handler error throwable exception handler throw

14/12 2016

1.0.0

1.0.0.0

PHP library for handling exceptions and errors.

  Sources   Download

MIT

The Requires

  • php >=7.0

 

php exception hhvm error handler error throwable exception handler throw