1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace BEAR\Package\Provide\Error; |
6
|
|
|
|
7
|
|
|
use BEAR\Resource\ResourceObject; |
8
|
|
|
use BEAR\Sunday\Extension\Router\RouterMatch; |
9
|
|
|
use Throwable; |
10
|
|
|
|
11
|
|
|
use function json_encode; |
12
|
1 |
|
use function sprintf; |
13
|
|
|
|
14
|
1 |
|
use const JSON_PRETTY_PRINT; |
15
|
1 |
|
use const JSON_UNESCAPED_SLASHES; |
16
|
1 |
|
use const PHP_EOL; |
17
|
1 |
|
|
18
|
1 |
|
final class DevVndErrorPage extends ResourceObject |
19
|
|
|
{ |
20
|
1 |
|
public function __construct(Throwable $e, RouterMatch $request) |
21
|
|
|
{ |
22
|
1 |
|
$status = new Status($e); |
23
|
|
|
$this->code = $status->code; |
24
|
1 |
|
$this->headers = $this->getHeader(); |
25
|
|
|
$this->body = $this->getResponseBody($e, $request, $status); |
26
|
|
|
} |
27
|
1 |
|
|
28
|
|
|
public function toString(): string |
29
|
1 |
|
{ |
30
|
|
|
$this->view = json_encode($this->body, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) . PHP_EOL; |
31
|
|
|
|
32
|
1 |
|
return $this->view; |
33
|
|
|
} |
34
|
|
|
|
35
|
1 |
|
/** @return array<string, string> */ |
36
|
1 |
|
private function getHeader(): array |
37
|
1 |
|
{ |
38
|
1 |
|
return ['content-type' => 'application/vnd.error+json']; |
39
|
1 |
|
} |
40
|
|
|
|
41
|
|
|
/** @return array<string, string> */ |
42
|
|
|
private function getResponseBody(Throwable $e, RouterMatch $request, Status $status): array |
43
|
|
|
{ |
44
|
|
|
return [ |
45
|
|
|
'message' => $status->text, |
46
|
|
|
'logref' => (string) new LogRef($e), |
47
|
|
|
'request' => (string) $request, |
48
|
|
|
'exceptions' => sprintf('%s(%s)', $e::class, $e->getMessage()), |
49
|
|
|
'file' => sprintf('%s(%s)', $e->getFile(), $e->getLine()), |
50
|
|
|
]; |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|