Add errorClasses parameter to HandleErrors so that HandleErrors middleware can be used on only some exceptions if wanted

This commit is contained in:
Alexander Skvortsov 2020-07-28 18:06:38 -04:00
parent 9e3699ea47
commit 053b3fd96b

View File

@ -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()) {