-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy patherrorHandler.php
More file actions
48 lines (44 loc) · 1.41 KB
/
Copy patherrorHandler.php
File metadata and controls
48 lines (44 loc) · 1.41 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
<?php
ini_set('display_errors', 'Off');
ini_set('error_reporting', E_ALL);
/**
* Log the error message, file, line number, class, method, and stack trace
* @param Exception $e the exception to log
* @param bool $red whether to color the error message red
* @return void
*/
function logError($e, $red = true)
{
$logMessage = sprintf(
($red ? "\e[0;31m" : "") . "Error: %s in %s on line %d\n%s\n%s::%s\nStack trace:\n%s\n\nLink to code: vscode://file/%s:%d" . ($red ? "\033[0m" : ""),
$e->getMessage(),
$e->getFile(),
$e->getLine(),
$e->getMessage(),
$e->getFile(),
$e->getLine(),
$e->getTraceAsString(),
$e->getFile(),
$e->getLine()
);
error_log($logMessage);
}
/**
* Handle the error by logging it, setting the response code, and sending a JSON response
* @param Exception $e the exception to handle
* @return void
*/
function errorHandler($e)
{
if (!$e instanceof \Exceptions\HttpExceptions\HttpException) {
logError($e);
$e = new \Exceptions\HttpExceptions\InternalServerErrorException();
} else {
logError($e, false);
}
header('Content-Type: application/json');
$response = \Utils\ApiResponseBuilder::buildErrorResponse($e->getMessage(), $e->getHttpResponseCode());
http_response_code($e->getHttpResponseCode());
echo json_encode($response);
}
set_exception_handler('errorHandler');