From 053b3fd96b7018f9f4e674d482b147c9aa343aed Mon Sep 17 00:00:00 2001 From: Alexander Skvortsov Date: Tue, 28 Jul 2020 18:06:38 -0400 Subject: [PATCH] Add errorClasses parameter to HandleErrors so that HandleErrors middleware can be used on only some exceptions if wanted --- src/Http/Middleware/HandleErrors.php | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/src/Http/Middleware/HandleErrors.php b/src/Http/Middleware/HandleErrors.php index a2d741884..bd1e27100 100644 --- a/src/Http/Middleware/HandleErrors.php +++ b/src/Http/Middleware/HandleErrors.php @@ -41,11 +41,17 @@ class HandleErrors implements Middleware */ protected $reporters; - public function __construct(Registry $registry, HttpFormatter $formatter, iterable $reporters) + /** + * @var array + */ + protected $errorClasses; + + public function __construct(Registry $registry, HttpFormatter $formatter, iterable $reporters, $errorClasses = null) { $this->registry = $registry; $this->formatter = $formatter; $this->reporters = $reporters; + $this->errorClasses = $errorClasses; } /** @@ -56,6 +62,25 @@ class HandleErrors implements Middleware try { return $handler->handle($request); } catch (Throwable $e) { + // If an array of allowlisted exception classes has been provided + // (such as when we only want to handle frontend errors), we check + // that the error inherits one of these classes. If not, we throw it + // to let other handlers up in the middleware pipe handle it. + if (is_array($this->errorClasses)) { + $handled = false; + + foreach ($this->errorClasses as $errorClass) { + if ($e instanceof $errorClass) { + $handled = true; + break; + } + } + + if (!$handled) { + throw $e; + } + + } $error = $this->registry->handle($e); if ($error->shouldBeReported()) {