|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace BEAR\Sunday\Provide\Error; |
|
6
|
|
|
|
|
7
|
|
|
use BEAR\Resource\Code; |
|
8
|
|
|
use BEAR\Resource\Exception\BadRequestException as BadRequest; |
|
9
|
|
|
use BEAR\Resource\Exception\ResourceNotFoundException as NotFound; |
|
10
|
|
|
use BEAR\Resource\Exception\ServerErrorException as ServerError; |
|
11
|
|
|
use BEAR\Sunday\Extension\Error\ErrorInterface; |
|
12
|
|
|
use BEAR\Sunday\Extension\Router\RouterMatch as Request; |
|
13
|
|
|
use BEAR\Sunday\Extension\Transfer\TransferInterface; |
|
14
|
|
|
use Exception; |
|
15
|
|
|
use Override; |
|
16
|
|
|
|
|
17
|
|
|
use function array_key_exists; |
|
18
|
|
|
use function error_log; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Vnd.Error media type error |
|
22
|
|
|
* |
|
23
|
|
|
* @see https://github.com/blongden/vnd.error |
|
24
|
|
|
*/ |
|
25
|
|
|
final class VndError implements ErrorInterface |
|
26
|
|
|
{ |
|
27
|
|
|
private const CONTENT_TYPE = 'application/vnd.error+json'; |
|
28
|
|
|
|
|
29
|
|
|
/** @var array{Content-Type: string} */ |
|
30
|
|
|
public $headers = ['Content-Type' => '']; |
|
31
|
|
|
|
|
32
|
|
|
/** @var array{message: string} */ |
|
33
|
|
|
public $body = ['message' => '']; |
|
34
|
|
|
private ErrorPage $errorPage; |
|
35
|
|
|
|
|
36
|
|
|
public function __construct( |
|
37
|
6 |
|
private TransferInterface $transfer, |
|
38
|
|
|
) { |
|
39
|
6 |
|
$this->errorPage = new ErrorPage(); |
|
40
|
6 |
|
} |
|
41
|
6 |
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* {@inheritDoc} |
|
44
|
|
|
* |
|
45
|
|
|
* @noinspection ForgottenDebugOutputInspection |
|
46
|
5 |
|
*/ |
|
47
|
|
|
#[Override] |
|
48
|
5 |
|
public function handle(Exception $e, Request $request) // phpcs:disable SlevomatCodingStandard.Exceptions.ReferenceThrowableOnly.ReferencedGeneralException |
|
49
|
3 |
|
{ |
|
50
|
3 |
|
if ($this->isCodeExists($e)) { |
|
51
|
|
|
$this->errorPage->code = (int) $e->getCode(); |
|
52
|
3 |
|
$this->errorPage->body = ['message' => (new Code())->statusText[$this->errorPage->code]]; |
|
53
|
|
|
|
|
54
|
2 |
|
return $this; |
|
55
|
2 |
|
} |
|
56
|
2 |
|
|
|
57
|
2 |
|
$this->errorPage->code = 500; |
|
58
|
|
|
$this->errorPage->body = ['message' => '500 Server Error']; |
|
59
|
2 |
|
error_log((string) $request); |
|
60
|
|
|
error_log((string) $e); |
|
61
|
|
|
|
|
62
|
|
|
return $this; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
5 |
|
#[Override] |
|
66
|
|
|
public function transfer(): void |
|
67
|
5 |
|
{ |
|
68
|
5 |
|
$this->errorPage->headers['Content-Type'] = self::CONTENT_TYPE; |
|
69
|
5 |
|
$this->transfer->__invoke($this->errorPage, []); |
|
70
|
|
|
} |
|
71
|
5 |
|
|
|
72
|
|
|
private function isCodeExists(Exception $e): bool |
|
73
|
5 |
|
{ |
|
74
|
1 |
|
if (! ($e instanceof NotFound) && ! ($e instanceof BadRequest) && ! ($e instanceof ServerError)) { |
|
75
|
|
|
return false; |
|
76
|
|
|
} |
|
77
|
4 |
|
|
|
78
|
|
|
return array_key_exists($e->getCode(), (new Code())->statusText); |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|