start rework for 2025

This commit is contained in:
lubiana 2025-07-08 22:30:04 +02:00
parent 81c47d94ca
commit b2fb8bee39
Signed by: lubiana
SSH key fingerprint: SHA256:vW1EA0fRR3Fw+dD/sM0K+x3Il2gSry6YRYHqOeQwrfk
14 changed files with 214 additions and 1589 deletions

View file

@ -4,29 +4,68 @@
An error handler allows you to customize what happens if your code results in an error.
A nice error page with a lot of information for debugging goes a long way during development. So the first package
It is important to clearly define how your project should behave when it encounters an error, and especially where it
should output the information about an error.
We want to mainly focus on Exceptions, which are an encapsulation of errors in php. We also have "normal" errors, but
most of them are currently being changed currently into Exceptions.
```php
<?php
declare(strict_types=1);
namespace Lubiana\NoFramework;
use ErrorException;
use Throwable;
set_exception_handler(function (Throwable $t) {
$errorType = match($t->getCode()) {
E_ERROR, E_USER_ERROR => 'Fatal Error',
E_WARNING, E_USER_WARNING => 'Warning',
E_NOTICE, E_USER_NOTICE => 'Notice',
default => 'Unknown Error'
};
echo <<<HTML
<h1>{$errorType}</h1>
<p>{$t->getMessage()}</p>
<pre>{$t->getTraceAsString()}</pre>
HTML;
});
set_error_handler(
function (int $errno, string $errstr, string $errfile, int $errline) {
throw new ErrorException(
message: $errstr,
code: $errno,
severity: $errno,
filename: $errfile,
line: $errline
);
}
);
echo 'Hello world!';
```
You can then replace `echo 'Hello world!';` with `trigger_error('This is a test error');`
or `throw new Exception('This is a test exception');` and open it in your browser to see if the error handling works.
During development there are some other nice features to add. For example a quick link to open your Editor on the file the Error occured. So the first package
for your application will take care of that.
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.
An alternative package would be: [PHP-Error](https://github.com/JosephLenton/PHP-Error)
Some alternatives would be: [PHP-Error](https://github.com/JosephLenton/PHP-Error) or [Tracy](https://tracy.nette.org/en/)
To install a new package, open up your `composer.json` and add the package to the require part. It should now look
like this:
```php
"require": {
"php": ">=8.1.0",
"filp/whoops": "^2.14"
},
```
Now run `composer update` in your console, and it will be installed.
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.
To install that package into your project simply type `composer require filp/whoops` into your terminal at the project root,
now composer automatically looks for a version of that package compatible with the rest of your project and your php
version.
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
@ -36,7 +75,7 @@ only have to add a `require __DIR__ . '/../vendor/autoload.php';` to your `Boots
can help someone to gain access to your system. Always show a user-friendly error page instead and send an email to
yourself, write to a log or something similar. So only you can see the errors in the production environment.
For development that does not make sense though -- you want a nice error page. The solution is to have an environment
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.
@ -44,36 +83,51 @@ Then after the error handler registration, throw an `Exception` to test if every
Your `Bootstrap.php` should now look similar to this:
```php
<?php declare(strict_types=1);
<?php
namespace Lubian\NoFramework;
declare(strict_types=1);
namespace Lubiana\NoFramework;
use Throwable;
use Whoops\Handler\CallbackHandler;
use Whoops\Handler\PrettyPageHandler;
use Whoops\Run;
require __DIR__ . '/../vendor/autoload.php';
$environment = getenv('ENVIRONMENT') ?: 'dev';
error_reporting(E_ALL);
$environment = getenv('APP_ENV') ?: 'dev';
$whoops = new Run;
$whoops->pushHandler(
new CallbackHandler(
function (Throwable $e) use ($environment) {
if ($environment !== 'dev') {
http_response_code(500);
echo 'Whoops';
}
error_log(<<<TXT
Error: {$e->getMessage()}
{$e->getTraceAsString()}
TXT
);
}
)
);
if ($environment === 'dev') {
$whoops->pushHandler(new PrettyPageHandler);
} else {
$whoops->pushHandler(function (\Throwable $e) {
error_log("Error: " . $e->getMessage(), $e->getCode());
echo 'An Error happened';
});
}
$whoops->register();
throw new \Exception("Ooooopsie");
throw new \Exception('Hello world');
```
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.
**Side-note:** Here we use `getenv()` to read a Variable from the Environment and fallback to using 'dev' if it is not set. That is a bad default, and in a production app you should always default to the strictest mode.
There are also good libraries that help with managing that in a better fashion. You can take a look at [phpdotenv](https://github.com/vlucas/phpdotenv) or [symfony/dotenv](https://github.com/symfony/dotenv) and maybe implement them. This tutorial however skips this step.
[<< previous](02-composer.md) | [next >>](04-development-helpers.md)