Download the PHP package phrity/util-errorhandler without Composer
On this page you can find all versions of the php package phrity/util-errorhandler. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download phrity/util-errorhandler
More information about phrity/util-errorhandler
Files in phrity/util-errorhandler
Package util-errorhandler
Short Description Inline error handler; catch and resolve errors for code block.
License MIT
Homepage https://phrity.sirn.se/util-errorhandler
Informations about the package util-errorhandler
Error Handler utility
The PHP error handling can be somewhat of a headache.
Typically an application uses a system level error handler and/or suppressing errors using the @
prefix.
But those cases when your code need to act on triggered errors are more tricky.
This library provides two convenience methods to handle errors on code blocks, either by throwing exceptions or running callback code when an error occurs.
Current version supports PHP ^7.2|^8.0
.
Installation
Install with Composer;
The Error Handler
The class provides two main methods; with()
and withAll()
.
The difference is that with()
will act immediately on an error and abort further code execution, while withAll()
will attempt to execute the entire code block before acting on errors that occurred.
Throwing ErrorException
The examples above will run the callback code, but if an error occurs it will throw an ErrorException. Error message and severity will be that of the triggering error.
with()
will throw immediately when occuredwithAll()
will throw when code is complete; if more than one error occurred, the first will be thrown
Throwing specified Throwable
The examples above will run the callback code, but if an error occurs it will throw provided Throwable.
The thrown Throwable will have an ErrorException attached as $previous
.
with()
will throw immediately when occuredwithAll()
will throw when code is complete; if more than one error occurred, the first will be thrown
Using callback
The examples above will run the callback code, but if an error occurs it will call the error callback as well.
with()
will run the error callback immediately when error occured; error callback expects an ErrorException instancewithAll()
will run the error callback when code is complete; error callback expects an array of ErrorException and the returned result of code callback
Filtering error types
Both with()
and withAll()
accepts error level(s) as last parameter.
Any value or combination of values accepted by set_error_handler is usable.
Default is E_ALL
. List of constants.
The global error handler
The class also has global set()
and restore()
methods.
Class synopsis
Versions
Version | PHP | |
---|---|---|
1.1 |
^7.4\|^8.0 |
Some improvements |
1.0 |
^7.2\|^8.0 |
Initial version |