Download the PHP package bakame/aide-error without Composer
On this page you can find all versions of the php package bakame/aide-error. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download bakame/aide-error
More information about bakame/aide-error
Files in bakame/aide-error
Package aide-error
Short Description A siple class to handle errors in PHP functions
License MIT
Informations about the package aide-error
Aide for Errors
A Cloak system to help dealing with error reporting in PHP.
[!CAUTION]
Sub-split of Aide for Error.
⚠️ this is a sub-split, for pull requests and issues, visit: https://github.com/bakame-php/aide
Installation
Composer
composer require bakame-php/aide-error
System Requirements
You need:
- PHP >= 8.1 but the latest stable version of PHP is recommended
Usage
Traditionally to correctly handle errors with PHP's functions you have two (2) options. Either
you use the @
to suppress the error which is not recommended; or you need to add some
boilerplate code around set_error_handler
and restore_error_handler
.
The Bakame\Aide\Error\Cloak
utility class helps you remove that burden by doing the heavy-lifting for you.
`
You can control its behaviour on your global codebase
`
Or you can decide to specifically change its default behaviour for a specific call.
`
Available properties and methods
Accessing the error
To access the errors store in the instance you need to call the Cloak::errors
method
which will return a CloakedErrors
instance. This container gives you can access all
the ErrorException
generated during the last execution of the callback.
If no error occurred during the last execution of the class, the CloakedErrors
instance
will be empty.
Controlling when to throw or not your errors.
The class general behaviour is controlled by two (2) static methods.
In all cases if an error occurred, it is converted into an ErrorException
and will be made accessible via the Cloak::errors
method. The difference
being that with:
Cloak::throwOnError
: every instance will throw on the first error;Cloak::silentOnError
: no exception will be thrown;
[!NOTE] to respect PHP's behaviour,
Cloak
usesCloak::silentOnError
by default
Named constructors
To ease usage the named constructors are added:
They all share the same signature:
the $onError
argument is used to tweak the instance behaviour on error:
Cloak::THROW
will override the general behaviour and force throwing an exception if availableCloak::SILENT
will override the general behaviour and silence the error if it existsCloak::OBEY
will comply with the curring general behaviour.
If you really need other fined grained error level you can still use the constructor as shown below:
ReportingLevel class
Because dealing with PHP error reporting level can be confusing sometimes, the package ships with an friendlier
approach to deal with them. As an example, the previous code example can be rewritten using the
ReportingLevel
class.
The class exposes a friendlier API to ease working with error reporting level:
ReportingLevel::fromValue
allow instantiating the class with any value you want.ReportingLevel::fromName
allow instantiating the class with the string corresponding to one of theE_*
constants.ReportingLevel::fromEnv
instantiates the class to match your current environment settings.ReportingLevel::fromInclusion
instantiates the error level by adding all the submitted values via a bitwiseOR
operation starting at0
meaning that no Error reporting level exists if none is added.ReportingLevel::fromExclusion
does the opposite, each value given will be removed from the maximum value, represented byE_ALL
.
on top of that the class expose a construct for each error reporting level using the following syntax:
You can tell which error reporting is being configured using the contains
method.
The class also provides the excluded
and included
methods which returns the
error reporting level names.
Accessing the Error Reporting Level from a Cloak instance
Once instantiated, you can always access the error reporting level via
the errorLevel
method on a Cloak
instance. For example, if you need to know if a
specific error is included you can do the following: