From 295a007cd547ae8259db0f5c9ce0f1ad6c64e7ba Mon Sep 17 00:00:00 2001 From: Toby Zerner Date: Sun, 11 Nov 2018 17:54:19 +1030 Subject: [PATCH] Catch Throwables so that we handle internal PHP errors too --- src/Api/ErrorHandler.php | 7 ++++++- src/Api/Middleware/HandleErrors.php | 4 ++-- src/Http/Middleware/HandleErrorsWithView.php | 6 +++--- src/Http/Middleware/HandleErrorsWithWhoops.php | 4 ++-- 4 files changed, 13 insertions(+), 8 deletions(-) diff --git a/src/Api/ErrorHandler.php b/src/Api/ErrorHandler.php index 3f6125adc..21ea56e3b 100644 --- a/src/Api/ErrorHandler.php +++ b/src/Api/ErrorHandler.php @@ -12,6 +12,7 @@ namespace Flarum\Api; use Exception; +use Throwable; use Tobscure\JsonApi\Document; use Tobscure\JsonApi\ErrorHandler as JsonApiErrorHandler; @@ -34,8 +35,12 @@ class ErrorHandler * @param Exception $e * @return JsonApiResponse */ - public function handle(Exception $e) + public function handle(Throwable $e) { + if (! $e instanceof Exception) { + $e = new Exception($e->getMessage(), $e->getCode(), $e); + } + $response = $this->errorHandler->handle($e); $document = new Document; diff --git a/src/Api/Middleware/HandleErrors.php b/src/Api/Middleware/HandleErrors.php index bba861ac2..cfcc54068 100644 --- a/src/Api/Middleware/HandleErrors.php +++ b/src/Api/Middleware/HandleErrors.php @@ -11,12 +11,12 @@ namespace Flarum\Api\Middleware; -use Exception; use Flarum\Api\ErrorHandler; use Psr\Http\Message\ResponseInterface as Response; use Psr\Http\Message\ServerRequestInterface as Request; use Psr\Http\Server\MiddlewareInterface as Middleware; use Psr\Http\Server\RequestHandlerInterface as Handler; +use Throwable; class HandleErrors implements Middleware { @@ -40,7 +40,7 @@ class HandleErrors implements Middleware { try { return $handler->handle($request); - } catch (Exception $e) { + } catch (Throwable $e) { return $this->errorHandler->handle($e); } } diff --git a/src/Http/Middleware/HandleErrorsWithView.php b/src/Http/Middleware/HandleErrorsWithView.php index 0ef7f5139..27e2118ac 100644 --- a/src/Http/Middleware/HandleErrorsWithView.php +++ b/src/Http/Middleware/HandleErrorsWithView.php @@ -11,7 +11,6 @@ namespace Flarum\Http\Middleware; -use Exception; use Flarum\Settings\SettingsRepositoryInterface; use Illuminate\Contracts\View\Factory as ViewFactory; use Psr\Http\Message\ResponseInterface as Response; @@ -20,6 +19,7 @@ use Psr\Http\Server\MiddlewareInterface as Middleware; use Psr\Http\Server\RequestHandlerInterface as Handler; use Psr\Log\LoggerInterface; use Symfony\Component\Translation\TranslatorInterface; +use Throwable; use Zend\Diactoros\Response\HtmlResponse; class HandleErrorsWithView implements Middleware @@ -65,12 +65,12 @@ class HandleErrorsWithView implements Middleware { try { return $handler->handle($request); - } catch (Exception $e) { + } catch (Throwable $e) { return $this->formatException($e); } } - protected function formatException(Exception $error) + protected function formatException(Throwable $error) { $status = 500; $errorCode = $error->getCode(); diff --git a/src/Http/Middleware/HandleErrorsWithWhoops.php b/src/Http/Middleware/HandleErrorsWithWhoops.php index 57d56b2ad..44e3f2881 100644 --- a/src/Http/Middleware/HandleErrorsWithWhoops.php +++ b/src/Http/Middleware/HandleErrorsWithWhoops.php @@ -11,12 +11,12 @@ namespace Flarum\Http\Middleware; -use Exception; use Franzl\Middleware\Whoops\WhoopsRunner; use Psr\Http\Message\ResponseInterface as Response; use Psr\Http\Message\ServerRequestInterface as Request; use Psr\Http\Server\MiddlewareInterface as Middleware; use Psr\Http\Server\RequestHandlerInterface as Handler; +use Throwable; class HandleErrorsWithWhoops implements Middleware { @@ -27,7 +27,7 @@ class HandleErrorsWithWhoops implements Middleware { try { return $handler->handle($request); - } catch (Exception $e) { + } catch (Throwable $e) { return WhoopsRunner::handle($e, $request); } }