no-framework-tutorial/03-error-handler.md

80 lines
3.1 KiB
Markdown
Raw Normal View History

2022-03-29 18:35:06 +00:00
[<< previous](02-composer.md) | [next >>](04-development-helpers.md)
2014-09-12 16:33:31 +00:00
2014-09-12 21:21:50 +00:00
### Error Handler
2014-09-12 18:27:47 +00:00
2014-09-14 18:10:37 +00:00
An error handler allows you to customize what happens if your code results in an error.
2022-03-29 18:35:06 +00:00
A nice error page with a lot of information for debugging goes a long way during development. So the first package
for your application will take care of that.
2014-09-12 18:27:47 +00:00
2022-03-29 18:35:06 +00:00
I like [filp/whoops](https://github.com/filp/whoops), so I will show how you can install that package for your project.
If you prefer another package, feel free to install that one. This is the beauty of programming without a framework,
you have total control over your project.
2014-09-12 18:27:47 +00:00
An alternative package would be: [PHP-Error](https://github.com/JosephLenton/PHP-Error)
2022-03-29 18:35:06 +00:00
To install a new package, open up your `composer.json` and add the package to the require part. It should now look
like this:
2014-09-12 18:27:47 +00:00
2014-09-17 20:07:26 +00:00
```php
2014-09-12 18:27:47 +00:00
"require": {
2022-03-29 18:35:06 +00:00
"php": ">=8.1.0",
"filp/whoops": "^2.14"
2014-09-12 18:27:47 +00:00
},
```
2022-05-20 23:19:14 +00:00
Now run `composer update` in your console, and it will be installed.
2014-09-12 18:27:47 +00:00
2022-03-29 18:35:06 +00:00
Another way to install packages is to simply type "composer require filp/whoops" into your terminal at the project root,
i that case composer automatically installs the package and updates your composer.json-file.
2014-09-12 18:27:47 +00:00
2022-03-29 18:35:06 +00:00
But you can't use it yet. PHP won't know where to find the files for the classes. For this you will need an autoloader,
ideally a [PSR-4](http://www.php-fig.org/psr/psr-4/) autoloader. Composer already takes care of this for you, so you
only have to add a `require __DIR__ . '/../vendor/autoload.php';` to your `Bootstrap.php`.
2014-09-12 18:27:47 +00:00
2022-03-29 18:35:06 +00:00
**Important:** Never show any errors in your production environment. A stack trace or even just a simple error message
2022-05-20 23:19:14 +00:00
can help someone to gain access to your system. Always show a user-friendly error page instead and send an email to
2022-03-29 18:35:06 +00:00
yourself, write to a log or something similar. So only you can see the errors in the production environment.
2014-09-12 18:27:47 +00:00
2022-03-29 18:35:06 +00:00
For development that does not make sense though -- you want a nice error page. The solution is to have an environment
switch in your code. We use the getenv() function here to check the environment and define the 'dev' env as standard in
case no environment has been set.
Then after the error handler registration, throw an `Exception` to test if everything is working correctly.
Your `Bootstrap.php` should now look similar to this:
2014-09-12 18:27:47 +00:00
2014-09-17 20:07:26 +00:00
```php
2022-03-29 18:35:06 +00:00
<?php declare(strict_types=1);
namespace Lubian\NoFramework;
2014-09-12 18:27:47 +00:00
2022-03-29 18:35:06 +00:00
use Whoops\Handler\PrettyPageHandler;
use Whoops\Run;
2014-09-12 18:27:47 +00:00
require __DIR__ . '/../vendor/autoload.php';
2014-09-12 18:27:47 +00:00
2022-03-29 18:35:06 +00:00
$environment = getenv('ENVIRONMENT') ?: 'dev';
2014-09-12 18:27:47 +00:00
2022-03-29 18:35:06 +00:00
error_reporting(E_ALL);
2014-09-12 18:27:47 +00:00
2022-03-29 18:35:06 +00:00
$whoops = new Run;
2022-04-30 19:24:25 +00:00
if ($environment === 'dev') {
2022-03-29 18:35:06 +00:00
$whoops->pushHandler(new PrettyPageHandler);
2014-09-12 18:27:47 +00:00
} else {
2022-03-29 18:35:06 +00:00
$whoops->pushHandler(function (\Throwable $e) {
error_log("Error: " . $e->getMessage(), $e->getCode());
echo 'An Error happened';
2014-09-12 18:27:47 +00:00
});
}
2014-12-08 10:42:21 +00:00
$whoops->register();
2014-09-12 18:27:47 +00:00
2022-03-29 18:35:06 +00:00
throw new \Exception("Ooooopsie");
2014-09-12 18:27:47 +00:00
2014-09-12 18:48:04 +00:00
```
2022-03-29 18:35:06 +00:00
You should now see a error page with the line highlighted where you throw the exception. If not, go back and debug until
you get it working. Now would also be a good time for another commit.
2014-09-12 18:48:04 +00:00
2022-03-29 18:35:06 +00:00
[<< previous](02-composer.md) | [next >>](04-development-helpers.md)