Skip to content

Commit

Permalink
Add possibility to set custom error handler in Bootstrap. (#320)
Browse files Browse the repository at this point in the history
  • Loading branch information
ksucherek authored Sep 27, 2023
1 parent 9cc81a6 commit e417070
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 16 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ Support for PHP 8.0 is dropped. Minimal PHP version required is 8.2.

Enhancements:
* [ORM] Added support for count query with `DISTINCT` in PostgreSQL dialect.
* [Utilities] `Strings::EMPTY_STRING` is deprecated in favour of `Strings::EMPTY`.
* [Utilities] `Strings::EMPTY_STRING` is deprecated in favour of `Strings::EMPTY`.
* [Core] `Bootstrap::withErrorHandler(ErroraHandler $errorHandler)` set custom error handler which is registered on `Bootstrap::runApplication()`.

Release 2.0.0
--------
Expand Down
19 changes: 15 additions & 4 deletions src/Ouzo/Core/Bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class Bootstrap
/** @var string[] */
private array $interceptors = [];
private bool $overrideMiddleware = false;
private ?ErrorHandler $errorHandler = null;

public function __construct(Environment $environment)
{
Expand Down Expand Up @@ -81,6 +82,12 @@ public function overrideMiddleware(...$interceptors): static
return $this;
}

public function withErrorHandler(ErrorHandler $errorHandler): static
{
$this->errorHandler = $errorHandler;
return $this;
}

public function runApplication(): FrontController
{
if ($this->configRepository) {
Expand All @@ -102,11 +109,15 @@ public function runApplication(): FrontController
private function registerErrorHandlers(): void
{
if (Config::getValue('debug')) {
$handler = new DebugErrorHandler();
} else {
$handler = new ErrorHandler();
(new DebugErrorHandler())->register();
return;
}

if (!is_null($this->errorHandler)) {
$this->errorHandler->register();
return;
}
$handler->register();
(new ErrorHandler())->register();
}

private function includeRoutes(): void
Expand Down
8 changes: 0 additions & 8 deletions src/Ouzo/Core/ExceptionHandling/DebugErrorHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,11 @@

namespace Ouzo\ExceptionHandling;

use Throwable;
use Whoops\Handler\PrettyPageHandler;
use Whoops\Run;

class DebugErrorHandler extends ErrorHandler
{
public function register(): void
{
set_exception_handler(fn(Throwable $exception) => DebugErrorHandler::exceptionHandler($exception));
set_error_handler(fn(...$args) => DebugErrorHandler::errorHandler(...$args));
register_shutdown_function(fn() => DebugErrorHandler::shutdownHandler());
}

protected static function getRun(): Run
{
error_reporting(E_ALL);
Expand Down
6 changes: 3 additions & 3 deletions src/Ouzo/Core/ExceptionHandling/ErrorHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ class ErrorHandler
{
public function register(): void
{
set_exception_handler(fn(Throwable $exception) => ErrorHandler::exceptionHandler($exception));
set_error_handler(fn(...$args) => ErrorHandler::errorHandler(...$args), E_ALL & ~E_DEPRECATED & ~E_STRICT);
register_shutdown_function(fn() => ErrorHandler::shutdownHandler());
set_exception_handler(fn(Throwable $exception) => static::exceptionHandler($exception));
set_error_handler(fn(...$args) => static::errorHandler(...$args), E_ALL & ~E_DEPRECATED & ~E_STRICT);
register_shutdown_function(fn() => static::shutdownHandler());
}

public static function exceptionHandler(Throwable $exception): void
Expand Down
18 changes: 18 additions & 0 deletions test/src/Ouzo/Core/BootstrapTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Ouzo\CookiesSetter;
use Ouzo\DownloadHandler;
use Ouzo\Environment;
use Ouzo\ExceptionHandling\ErrorHandler;
use Ouzo\HeaderSender;
use Ouzo\Injection\InjectorConfig;
use Ouzo\Middleware\Interceptor\SessionStarter;
Expand Down Expand Up @@ -115,4 +116,21 @@ public function shouldThrowExceptionWhenMiddlewareClassNotImplementingIntercepto
//then
CatchException::assertThat()->hasMessage('stdClass class is not implementing Interceptor interface');
}

#[Test]
public function shouldRegisterCustomErrorHandler()
{
//given
Config::overrideProperty('debug')->with(false);
/** @var ErrorHandler|MockInterface $errorHandler */
$errorHandler = Mock::create(ErrorHandler::class);

//when
$this->bootstrap
->withErrorHandler($errorHandler)
->runApplication();

//then
Mock::verify($errorHandler)->register();
}
}

0 comments on commit e417070

Please sign in to comment.