/
HandleErrors.php
50 lines (44 loc) · 1.12 KB
/
HandleErrors.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<?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;
class HandleErrors
{
/**
* @var ErrorHandler
*/
protected $errorHandler;
/**
* @param ErrorHandler $errorHandler
*/
public function __construct(ErrorHandler $errorHandler)
{
$this->errorHandler = $errorHandler;
}
/**
* Catch all errors that happen during further middleware execution.
*
* @param Request $request
* @param Response $response
* @param callable $out
* @return Response
*/
public function __invoke(Request $request, Response $response, callable $out = null)
{
try {
return $out($request, $response);
} catch (Exception $e) {
return $this->errorHandler->handle($e);
}
}
}