mirror of
https://github.com/flarum/framework.git
synced 2025-04-26 14:44:03 +08:00

This finally adopts the new standardized interfaces instead of the work-in-progress ones with the `Interop\` prefix. Since we have now updated to PHP 7.1, we can also use Stratigility 3.0 as the middleware dispatcher.
48 lines
1.1 KiB
PHP
48 lines
1.1 KiB
PHP
<?php
|
|
|
|
/*
|
|
* This file is part of Flarum.
|
|
*
|
|
* (c) Toby Zerner <toby.zerner@gmail.com>
|
|
*
|
|
* For the full copyright and license information, please view the LICENSE
|
|
* file that was distributed with this source code.
|
|
*/
|
|
|
|
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;
|
|
|
|
class HandleErrors implements Middleware
|
|
{
|
|
/**
|
|
* @var ErrorHandler
|
|
*/
|
|
protected $errorHandler;
|
|
|
|
/**
|
|
* @param ErrorHandler $errorHandler
|
|
*/
|
|
public function __construct(ErrorHandler $errorHandler)
|
|
{
|
|
$this->errorHandler = $errorHandler;
|
|
}
|
|
|
|
/**
|
|
* Catch all errors that happen during further middleware execution.
|
|
*/
|
|
public function process(Request $request, Handler $handler): Response
|
|
{
|
|
try {
|
|
return $handler->handle($request);
|
|
} catch (Exception $e) {
|
|
return $this->errorHandler->handle($e);
|
|
}
|
|
}
|
|
}
|