|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the Madapaja.TwigModule package. |
|
4
|
|
|
* |
|
5
|
|
|
* @license http://opensource.org/licenses/MIT MIT |
|
6
|
|
|
*/ |
|
7
|
|
|
namespace Madapaja\TwigModule; |
|
8
|
|
|
|
|
9
|
|
|
use BEAR\Resource\Code; |
|
10
|
|
|
use BEAR\Resource\Exception\BadRequestException as BadRequest; |
|
11
|
|
|
use BEAR\Resource\Exception\ResourceNotFoundException as NotFound; |
|
12
|
|
|
use BEAR\Resource\Exception\ServerErrorException as ServerError; |
|
13
|
|
|
use BEAR\Resource\TransferInterface; |
|
14
|
|
|
use BEAR\Sunday\Extension\Error\ErrorInterface; |
|
15
|
|
|
use BEAR\Sunday\Extension\Router\RouterMatch as Request; |
|
16
|
|
|
use Psr\Log\LoggerInterface; |
|
17
|
|
|
|
|
18
|
|
|
final class TwigErrorPageHandler implements ErrorInterface |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @var TransferInterface |
|
22
|
|
|
*/ |
|
23
|
|
|
private $transfer; |
|
24
|
|
|
/** |
|
25
|
|
|
* @var TwigErrorPage |
|
26
|
|
|
*/ |
|
27
|
|
|
private $errorPage; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var LoggerInterface |
|
31
|
|
|
*/ |
|
32
|
|
|
private $logger; |
|
33
|
|
|
|
|
34
|
|
|
public function __construct(TwigErrorPage $errorPage, TransferInterface $transfer, LoggerInterface $logger) |
|
35
|
|
|
{ |
|
36
|
|
|
$this->transfer = $transfer; |
|
37
|
|
|
$this->errorPage = $errorPage; |
|
38
|
|
|
$this->logger = $logger; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* {@inheritdoc} |
|
43
|
|
|
*/ |
|
44
|
|
|
public function handle(\Exception $e, Request $request) |
|
45
|
|
|
{ |
|
46
|
|
|
unset($request); |
|
47
|
|
|
$code = $this->isCodeExists($e) ? $e->getCode() : 503; |
|
48
|
|
|
if ($code >= 500) { |
|
49
|
|
|
$eStr = (string) $e; |
|
50
|
|
|
$this->logger->error(\sprintf('logref:%s %s', \crc32($eStr), $eStr)); |
|
51
|
|
|
} |
|
52
|
|
|
$this->errorPage->code = $code; |
|
53
|
|
|
$this->errorPage->body = [ |
|
54
|
|
|
'status' => [ |
|
55
|
|
|
'code' => $code, |
|
56
|
|
|
'message' => (new Code)->statusText[$code] |
|
57
|
|
|
], |
|
58
|
|
|
'e' => [ |
|
59
|
|
|
'code' => $e->getCode(), |
|
60
|
|
|
'message' => $e->getMessage() |
|
61
|
|
|
] |
|
62
|
|
|
]; |
|
63
|
|
|
|
|
64
|
|
|
return $this; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* {@inheritdoc} |
|
69
|
|
|
*/ |
|
70
|
|
|
public function transfer() |
|
71
|
|
|
{ |
|
72
|
|
|
($this->transfer)($this->errorPage, []); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
private function isCodeExists(\Exception $e) : bool |
|
76
|
|
|
{ |
|
77
|
|
|
if (! ($e instanceof NotFound) && ! ($e instanceof BadRequest) && ! ($e instanceof ServerError)) { |
|
78
|
|
|
return false; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
return \array_key_exists($e->getCode(), (new Code)->statusText); |
|
|
|
|
|
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: