Skip to content

Instantly share code, notes, and snippets.

@pavarov
Created June 22, 2022 05:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pavarov/f7a74eea5aef528f44e4f1483aa68726 to your computer and use it in GitHub Desktop.
Save pavarov/f7a74eea5aef528f44e4f1483aa68726 to your computer and use it in GitHub Desktop.
handlers for uncaught errors and exceptions
<?php
declare(strict_types=1);
use App\Response;
set_exception_handler(static function (Throwable $ex) {
$msg = json_encode([
'error message' => $ex->getMessage(),
'error file' => str_replace($_SERVER['DOCUMENT_ROOT'], '', str_replace('\\', '/', $ex->getFile())),
'error line' => $ex->getLine(),
]);
Response::jsonFail($msg);
});
set_error_handler(static function (int $errno, string $errStr, string $errFile, string | int $errLine) {
$msg = json_encode([
'error code' => $errno,
'error message' => $errStr,
'error file' => str_replace($_SERVER['DOCUMENT_ROOT'], '', str_replace('\\', '/', $errFile)),
'error line' => $errLine,
]);
Response::jsonFail($msg);
}, E_ALL);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment