| 1 | <?php |
| 2 | |
| 3 | require_once __DIR__ . "/INTERNAL_SERVER_ERROR.php"; |
| 4 | require_once __DIR__ . "/ProblemDetailsException.php"; |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | set_error_handler(function ($severity, $message, $file, $line) { |
| 10 | throw new ErrorException($message, 0, $severity, $file, $line); |
| 11 | }); |
| 12 | |
| 13 | |
| 14 | set_exception_handler(function (Throwable $excepcion) { |
| 15 | if ($excepcion instanceof ProblemDetailsException) { |
| 16 | devuelveProblemDetails($excepcion->problemDetails); |
| 17 | } else { |
| 18 | devuelveProblemDetails([ |
| 19 | "status" => INTERNAL_SERVER_ERROR, |
| 20 | "title" => "Error interno del servidor", |
| 21 | "detail" => $excepcion->getMessage(), |
| 22 | "type" => "/errors/errorinterno.html", |
| 23 | ]); |
| 24 | } |
| 25 | exit(); |
| 26 | }); |
| 27 | |
| 28 | function devuelveProblemDetails(array $array) |
| 29 | { |
| 30 | $json = json_encode($array); |
| 31 | if ($json === false) { |
| 32 | devuelveResultadoNoJson(); |
| 33 | } else { |
| 34 | http_response_code(isset($array["status"]) ? $array["status"] : 500); |
| 35 | header("Content-Type: application/problem+json; charset=utf-8"); |
| 36 | echo $json; |
| 37 | } |
| 38 | } |
| 39 | |