Skip to content

Commit

Permalink
Start Issue documentation.
Browse files Browse the repository at this point in the history
  • Loading branch information
jlambe committed Nov 18, 2024
1 parent 33f4a0e commit 137091c
Showing 1 changed file with 39 additions and 7 deletions.
46 changes: 39 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Features
- Configurable error reporter
- Backtrace with filterable and identifiable frames
- File preview
- Supports nested exceptions
- HTML error report template with light and dark themes
- Configurable PHP error handler
- Configurable PHP exception handler
Expand All @@ -38,7 +39,8 @@ The package provides 2 components to help you interact with PHP errors and excep
- [Reporters](#reporters)
- [PHP Error Handler](#php-error-handler)
- [PHP Exception Handler](#php-exception-handler)
2. [Backtrace](#backtrace)
2. [Issue](#issue)
3. [Backtrace](#backtrace)

Report Handler
--------------
Expand Down Expand Up @@ -222,13 +224,13 @@ $reportHandler = new ReportHandler(
set_error_handler(new ErrorHandler($reportHandler));
```

The `ErrorHandler` captures all triggered PHP errors. The current implementation is only reporting the **deprecated** errors (E_DEPRECATED, E_USER_DEPRECATED) with the given `ReportHandler` instance. All other errors are converted to an `ErrorException` and are thrown so the default PHP exception handler can capture them.
The `ErrorHandler` class captures all triggered PHP errors. The current implementation is only reporting the **deprecated** errors (E_DEPRECATED, E_USER_DEPRECATED) with the given `ReportHandler` instance. All other errors are converted to an `ErrorException` and are thrown back again so the default PHP exception handler can capture them.

### PHP Exception Handler

You can register a `ReportHandler` instance as the default PHP exception handler. See the [set_exception_handler()](https://www.php.net/manual/en/function.set-exception-handler.php) function in the PHP documentation for details.

Just like for the PHP errors, the package provides a pre-configured class can be register a `ReportHandler` instance and hook it up as the default PHP exception handler.
Just like for the PHP errors, the package provides a pre-configured class that requires a `ReportHandler` instance and hook it up as the default PHP exception handler.
Simply pass an `ExceptionHandler` class instance to the `set_exception_handler` PHP function like so:

```php
Expand All @@ -244,7 +246,7 @@ set_exception_handler(new ExceptionHandler($reportHandler));

#### HTML Error Report Template

On a web context, you might want to render thrown exceptions in the browser during development to easily debug your application. The package also provides a HTML template with a light and a dark theme. Here is an example on how to use the `ExceptionHandlerHttpResponse` class that is responsible to prepare the data for usage in the `resources/views/exception.php` file from this package:
On a web context, you might want to render thrown exceptions in the browser, during development, to easily debug your application. The package provides a HTML template with a light and a dark theme. Here is an example on how to use the `ExceptionHandlerHtmlResponse` class, responsible to render a given `Issue` as HTML to the `stdout`:

```php
<?php
Expand All @@ -256,9 +258,16 @@ $reporters->add(
$backtrace = new Backtrace(new InMemoryFrameIdentifiers());
$backtrace->captureException($issue->exception());

(new ExceptionHandlerHttpResponse(
$information = new InMemoryInformation();
$information->add(
(new InformationGroup('General'))
->add('PHP', phpversion())
->add('OS', php_uname('s'))
);

(new ExceptionHandlerHtmlResponse(
backtrace: $backtrace,
information: new InMemoryInformation(),
information: $information,
))->render($issue);
}),
);
Expand All @@ -271,9 +280,32 @@ $reportHandler = new ReportHandler(
set_exception_handler(new ExceptionHandler($reportHandler));
```

The above code snippet is attaching a `ReportHandler` to the default PHP exception handler. The report handler contains one reporter that is exposing any thrown exceptions to the standard output using HTML.
The above code snippet is attaching a `ReportHandler` to replace the default PHP exception handler. The report handler contains one reporter that is exposing any thrown exceptions to the standard output using HTML.

> The attached condition is to always report the issue to the stdout. On your application, make sure to constraint the condition to avoid rendering the error HTML template on a production environment.
Issue
-----

An `Issue` is the package abstract definition for any problem that could occur inside a PHP application. The `ReportHandler` is never directly dealing with a PHP error nor a PHP exception but with an `Issue` instance.

An issue contains the following properties:

- A message: the problem description text.
- A date: when the problem occurred.
- A level: the severity of the issue, following PSR-3 levels.
- An exception: for PHP compatibity, the issue should be able to be converted/translated to an exception.
- Information: any additional information about the issue.

In most PHP applications, developers have to deal with exceptions. The package provides the `ExceptionalIssue` class that can be used to translate an exception into an issue like so:

```php
<?php

$issue = ExceptionalIssue::create(new RuntimeException('Oops!'));
```

### Information

Backtrace
---------

0 comments on commit 137091c

Please sign in to comment.