mirror of
https://github.com/flarum/core.git
synced 2025-08-23 00:23:25 +02:00
Add errorClasses parameter to HandleErrors so that HandleErrors middleware can be used on only some exceptions if wanted
This commit is contained in:
@@ -41,11 +41,17 @@ class HandleErrors implements Middleware
|
|||||||
*/
|
*/
|
||||||
protected $reporters;
|
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->registry = $registry;
|
||||||
$this->formatter = $formatter;
|
$this->formatter = $formatter;
|
||||||
$this->reporters = $reporters;
|
$this->reporters = $reporters;
|
||||||
|
$this->errorClasses = $errorClasses;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -56,6 +62,25 @@ class HandleErrors implements Middleware
|
|||||||
try {
|
try {
|
||||||
return $handler->handle($request);
|
return $handler->handle($request);
|
||||||
} catch (Throwable $e) {
|
} 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);
|
$error = $this->registry->handle($e);
|
||||||
|
|
||||||
if ($error->shouldBeReported()) {
|
if ($error->shouldBeReported()) {
|
||||||
|
Reference in New Issue
Block a user