-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathErrorHandler.php
More file actions
55 lines (45 loc) · 1.4 KB
/
ErrorHandler.php
File metadata and controls
55 lines (45 loc) · 1.4 KB
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
51
52
53
54
55
<?php
declare(strict_types=1);
namespace Phenix\Http;
use Amp\Http\HttpStatus;
use Amp\Http\Server\ErrorHandler as ErrorHandlerContract;
use Amp\Http\Server\Request;
use Amp\Http\Server\Response;
use Phenix\Facades\Config;
class ErrorHandler implements ErrorHandlerContract
{
public function handleError(int $status, ?string $reason = null, ?Request $request = null): Response
{
$message = $reason ?? HttpStatus::getReason($status);
$payload = [
'success' => false,
'error' => $message,
'status' => $status,
];
if ($this->shouldExposeDebugDetails()) {
$payload['debug'] = [
'reason' => $message,
'path' => $request?->getUri()->getPath(),
];
}
return $this->json($payload, $status, $reason);
}
/**
* @param array<string, mixed> $payload
*/
public function json(array $payload, int $status, ?string $reason = null): Response
{
$response = new Response(
headers: [
'content-type' => 'application/json',
],
body: json_encode($payload, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE)
);
$response->setStatus($status, $reason);
return $response;
}
public function shouldExposeDebugDetails(): bool
{
return Config::get('app.debug') === true;
}
}