| 1 | <?php |
| 2 | |
| 3 | require_once __DIR__ . "/lib/manejaErrores.php"; |
| 4 | require_once __DIR__ . "/lib/devuelveJson.php"; |
| 5 | |
| 6 | devuelveJson([ |
| 7 | "nombre" => ["value" => "pp"], |
| 8 | "apellido" => ["value" => "tkt"], |
| 9 | "genero" => ["value" => "pop"], |
| 10 | "generacion" => ["value" => ""], |
| 11 | "edad" => ["valueAsNumber" => 18], |
| 12 | "numero" => ["value" => 5], |
| 13 | "avance" => ["value" => 70], |
| 14 | "capacidad" => ["value" => 60], |
| 15 | "temperatura" => ["valueAsNumber" => 40], |
| 16 | "aprobado" => ["checked" => true], |
| 17 | "gracioso" => ["value" => false], |
| 18 | "emplacado" => ["value" => false], |
| 19 | "direccion" => ["textContent" => "Girasoles 23\ncolonia Rosales"], |
| 20 | "encabezado" => ["innerHTML" => "<em>Hola, soy <strong>pp</strong>"], |
| 21 | "nacimiento" => ["value" => "2000-07-04"], |
| 22 | "imagen1" => [ |
| 23 | "src" => "https://gilpgawoas.github.io/img/icono/maskable_icon_x48.png" |
| 24 | ], |
| 25 | "imagen2" => ["src" => "", "hidden" => true], |
| 26 | "pasatiempos[]" => ["fut", "basket"], |
| 27 | "madrugador" => ["no"], |
| 28 | "patos[]" => ["paco", "luis"], |
| 29 | ]); |
| 30 |
| 1 | <?php |
| 2 | |
| 3 | require_once __DIR__ . "/devuelveResultadoNoJson.php"; |
| 4 | |
| 5 | function devuelveJson($resultado) |
| 6 | { |
| 7 | $json = json_encode($resultado); |
| 8 | if ($json === false) { |
| 9 | devuelveResultadoNoJson(); |
| 10 | } else { |
| 11 | header("Content-Type: application/json; charset=utf-8"); |
| 12 | echo $json; |
| 13 | } |
| 14 | exit(); |
| 15 | } |
| 16 |
| 1 | <?php |
| 2 | |
| 3 | require_once __DIR__ . "/INTERNAL_SERVER_ERROR.php"; |
| 4 | |
| 5 | function devuelveResultadoNoJson() |
| 6 | { |
| 7 | http_response_code(INTERNAL_SERVER_ERROR); |
| 8 | header("Content-Type: application/problem+json; charset=utf-8"); |
| 9 | |
| 10 | echo '{' . |
| 11 | "status: " . INTERNAL_SERVER_ERROR . |
| 12 | '"title": "El resultado no puede representarse como JSON."' . |
| 13 | '"type": "/errors/resultadonojson.html"' . |
| 14 | '}'; |
| 15 | } |
| 16 |
| 1 | <?php |
| 2 | |
| 3 | const INTERNAL_SERVER_ERROR = 500; |
| 1 | <?php |
| 2 | |
| 3 | require_once __DIR__ . "/INTERNAL_SERVER_ERROR.php"; |
| 4 | require_once __DIR__ . "/ProblemDetailsException.php"; |
| 5 | |
| 6 | |
| 7 | |
| 8 | // Hace que se lance una excepción automáticamente cuando se genere un error. |
| 9 | set_error_handler(function ($severity, $message, $file, $line) { |
| 10 | throw new ErrorException($message, 0, $severity, $file, $line); |
| 11 | }); |
| 12 | |
| 13 | // Código cuando una excepción no es atrapada. |
| 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 |
| 1 | <?php |
| 2 | |
| 3 | require_once __DIR__ . "/INTERNAL_SERVER_ERROR.php"; |
| 4 | |
| 5 | /** |
| 6 | * Detalle de los errores devueltos por un servicio. |
| 7 | */ |
| 8 | class ProblemDetailsException extends Exception |
| 9 | { |
| 10 | |
| 11 | public array $problemDetails; |
| 12 | |
| 13 | public function __construct( |
| 14 | array $problemDetails, |
| 15 | ) { |
| 16 | |
| 17 | parent::__construct( |
| 18 | isset($problemDetails["detail"]) |
| 19 | ? $problemDetails["detail"] |
| 20 | : (isset($problemDetails["title"]) |
| 21 | ? $problemDetails["title"] |
| 22 | : "Error"), |
| 23 | $problemDetails["status"] |
| 24 | ? $problemDetails["status"] |
| 25 | : INTERNAL_SERVER_ERROR |
| 26 | ); |
| 27 | |
| 28 | $this->problemDetails = $problemDetails; |
| 29 | } |
| 30 | } |
| 31 |