-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexception_handler.class.php
More file actions
35 lines (31 loc) · 1.12 KB
/
exception_handler.class.php
File metadata and controls
35 lines (31 loc) · 1.12 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
<?php
set_exception_handler(function ($e) {
if ($e instanceof ResponseException) {
http_response_code($e->getCode());
echo json_encode($e->payload, JSON_NUMERIC_CHECK);
} else {
http_response_code(Response::INTERNAL_SERVER_ERROR["code"]);
$line = $e->getLine();
$path = $e->getFile();
$file = basename($path); // $file is set to "index.php"
//$file = basename($path, ".php"); // $file is set to "index"
$payload = [
"status" => Response::INTERNAL_SERVER_ERROR["status"],
"message" => "Internal Server Error (line $line in $file): ".$e->getMessage(),
"code" => Response::INTERNAL_SERVER_ERROR["code"],
"version" => "v5",
"timestamp" => time(),
"request" => $_SERVER["REQUEST_URI"] ?? "",
"method" => $_SERVER["REQUEST_METHOD"] ?? "",
];
echo json_encode($payload, JSON_NUMERIC_CHECK);
}
} );
class ResponseException extends Exception
{
public $payload = [];
public function __construct($payload, int $code) {
parent::__construct($payload["message"], $code);
$this->payload = $payload;
}
}