Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/WebFiori/err
Browse files Browse the repository at this point in the history
  • Loading branch information
usernane committed May 14, 2022
2 parents a73d0d0 + 85339ca commit 63e9d88
Showing 1 changed file with 60 additions and 3 deletions.
63 changes: 60 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ A library for handling PHP errors and exceptions in a better way.
<a target="_blank" href="https://github.com/WebFiori/err/actions/workflows/php81.yml">
<img src="https://github.com/WebFiori/err/workflows/Build%20PHP%208.1/badge.svg?branch=main">
</a>
<a href="https://codecov.io/gh/WebFiori/err">
<img src="https://codecov.io/gh/WebFiori/err/branch/main/graph/badge.svg" />
</a>
<a href="https://sonarcloud.io/dashboard?id=WebFiori_err">
<img src="https://sonarcloud.io/api/project_badges/measure?project=WebFiori_err&metric=alert_status" />
</a>
Expand All @@ -28,3 +25,63 @@ A library for handling PHP errors and exceptions in a better way.
|<a target="_blank" href="https://github.com/WebFiori/err/actions/workflows/php80.yml"><img src="https://github.com/WebFiori/err/workflows/Build%20PHP%208.0/badge.svg?branch=main"></a>|
|<a target="_blank" href="https://github.com/WebFiori/err/actions/workflows/php81.yml"><img src="https://github.com/WebFiori/err/workflows/Build%20PHP%208.1/badge.svg?branch=main"></a>|
|<a target="_blank" href="https://github.com/WebFiori/err/actions/workflows/php82.yml"><img src="https://github.com/WebFiori/err/workflows/Build%20PHP%208.2/badge.svg?branch=main"></a><br>(dev)|

## Installation
The library can be included in your project by including following entry in your `require` section of your `composer.json`: `"webfiori/err":"1.0.0"`.

## Features
* Conversion of all PHP errors to exceptions.
* Ability to create a custom exceptions handler.
* Provides OOP abstraction for the function `set_exception_handler()`

## Usage

The library has two main classes that the developer will work with. The first one is the class `Handler` and the second class is `AbstractExceptionHandler`. The first class is the core of the library. It is used to set custom exception handler. The second class is used to implement custom exceptions handler. Since the library will convert all PHP errors to exceptions, no need for the developer to have a custom errors handler.

### Implementing a Custom Exceptions Handler

First step in setting a custom exceptions handler is to implement one. Implementing a custom handler is strait forward procedure. Simply, the developer have to extends the class `AbstractExceptionHandler` and implement one abstract method of the class. The method `AbstractExceptionHandler::handle()` is used to handle the exception. The developer can have access to the properties of the thrown exception in order to handle it properly. The library comes with one default exceptions handler which can act as good example in how to implement a custom handler.

``` php
<?php
namespace webfiori\error;

class DefaultExceptionsHandler extends AbstractExceptionHandler {
public function __construct() {
parent::__construct();
}
/**
* Handles the exception.
*/
public function handle() {
echo '<pre>';
echo 'An exception was thrown at '.$this->getClass().' line '.$this->getLine().'.<br>';
echo 'Exception message: '.$this->getMessage().'.<br>';
echo 'Stack trace:<br>';
$trace = $this->getTrace();

if (count($trace) == 0) {
echo '&lt;Empty&gt;';
} else {
$index = '0';

foreach ($trace as $entry) {
echo '#'.$index.' '.$entry.'<br>';
$index++;
}
}
echo '</pre>';
}
}

```

### Setting a Custom Exceptions Handler

After implementing the handler, the developer must set it as exceptions handler. To do that, simply the developer have to use the method `Handler::setHandler()` in any place in his source code.

``` php
Handler::setHandler(new DefaultExceptionsHandler());
```


0 comments on commit 63e9d88

Please sign in to comment.