Download the PHP package hgh/exception-handler without Composer
On this page you can find all versions of the php package hgh/exception-handler. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download hgh/exception-handler
More information about hgh/exception-handler
Files in hgh/exception-handler
Package exception-handler
Short Description This package centralizes exceptions behaviour of projects. The duty of this package is to prevent to show unwanted exceptions to user.
License MIT
Informations about the package exception-handler
Exception Handler
This package centralizes exceptions behaviour of projects. The duty of this package is to prevent to show unwanted exceptions to user. Actually sometimes you want some exceptions to be shown in output too. This package will handle both sides. All exceptions can use the predefined interfaces.
Table of contents
- Installation
- Interfaces
- How to use
- Exception handling
- Exception logging
Installation
To install this package, require it via the composer.
Interfaces
There are 11 interfaces that exceptions can be implemented. The exceptions that are implemented from these interfaces will be log in the files by their type.
Interface | Description |
---|---|
AlertInterface | Mark exception as an alert exception |
CriticalInterface | Mark exception as a critical exception |
DebugInterface | Mark exception as a debug exception |
EmergencyInterface | Mark exception as a emergency exception |
InfoInterface | Mark exception as a info exception |
NoticeInterface | Mark exception as a notice exception |
WarningInterface | Mark exception as a warning exception |
NotLog | The exception will not be log |
ShouldPublish | The exception will publish in response handler, otherwise will replace with UnexpectedException |
WithDescription | The exception has extra description addition to exception message |
How to use
Exception handling
First as a sample we define an exception class.
You can use exception handler directly.
Or use facade:
As you can see, the result of exception handling is the same exception, because it implements from ShouldPublish
interface.
Let's define another exception to see another type of exception handling:
See, the result of exception handling is not the same. It is UnexpectedException
because it doesn't implement the ShouldPublish
interface.
Exception Logging
Another class exists here to log the exceptions. First before ExceptionHandler
change the type of exception, log the exception, then handle it by ExceptionHanlder
.
Let's define an exception.
Now it's the turn of the logger. You can use the direct service as below:
or use the facade:
then handle it and pass it to output. Basically it's better to extend the facade of package and define your own fileWriter to prevent defining the fileWriter each time.
By this way, before changing the exception by exception handler, you log every exception, and you will never miss them and on the other side, after handling the exceptions, you will never show to the user unwanted exceptions.