| 1 | import { exportaAHtml } from "./exportaAHtml.js" | 
| 2 | import { ProblemDetails } from "./ProblemDetails.js" | 
| 3 | |
| 4 | /** | 
| 5 | * Espera a que la promesa de un fetch termine. Si | 
| 6 | * hay error, lanza una excepción. Si no hay error, | 
| 7 | * interpreta la respuesta del servidor como JSON y | 
| 8 | * la convierte en una literal de objeto. | 
| 9 | * | 
| 10 | * @param { string | Promise<Response> } servicio | 
| 11 | */ | 
| 12 | export async function consumeJson(servicio) { | 
| 13 | |
| 14 | if (typeof servicio === "string") { | 
| 15 | servicio = fetch(servicio, { | 
| 16 | headers: { "Accept": "application/json, application/problem+json" } | 
| 17 | }) | 
| 18 | } else if (!(servicio instanceof Promise)) { | 
| 19 | throw new Error("Servicio de tipo incorrecto.") | 
| 20 | } | 
| 21 | |
| 22 | const respuesta = await servicio | 
| 23 | |
| 24 | const headers = respuesta.headers | 
| 25 | |
| 26 | if (respuesta.ok) { | 
| 27 | // Aparentemente el servidor tuvo éxito. | 
| 28 | |
| 29 | if (respuesta.status === 204) { | 
| 30 | // No contiene texto de respuesta. | 
| 31 | |
| 32 | return { headers, body: {} } | 
| 33 | |
| 34 | } else { | 
| 35 | |
| 36 | const texto = await respuesta.text() | 
| 37 | |
| 38 | try { | 
| 39 | |
| 40 | return { headers, body: JSON.parse(texto) } | 
| 41 | |
| 42 | } catch (error) { | 
| 43 | |
| 44 | // El contenido no es JSON. Probablemente sea texto de un error. | 
| 45 | throw new ProblemDetails(respuesta.status, headers, texto, | 
| 46 | "/error/errorinterno.html") | 
| 47 | |
| 48 | } | 
| 49 | |
| 50 | } | 
| 51 | |
| 52 | } else { | 
| 53 | // Hay un error. | 
| 54 | |
| 55 | const texto = await respuesta.text() | 
| 56 | |
| 57 | if (texto === "") { | 
| 58 | |
| 59 | // No hay texto. Se usa el texto predeterminado. | 
| 60 | throw new ProblemDetails(respuesta.status, headers, respuesta.statusText) | 
| 61 | |
| 62 | } else { | 
| 63 | // Debiera se un ProblemDetails en JSON. | 
| 64 | |
| 65 | try { | 
| 66 | |
| 67 | const { title, type, detail } = JSON.parse(texto) | 
| 68 | |
| 69 | throw new ProblemDetails(respuesta.status, headers, | 
| 70 | typeof title === "string" ? title : respuesta.statusText, | 
| 71 | typeof type === "string" ? type : undefined, | 
| 72 | typeof detail === "string" ? detail : undefined) | 
| 73 | |
| 74 | } catch (error) { | 
| 75 | |
| 76 | if (error instanceof ProblemDetails) { | 
| 77 | // El error si era un ProblemDetails | 
| 78 | |
| 79 | throw error | 
| 80 | |
| 81 | } else { | 
| 82 | |
| 83 | throw new ProblemDetails(respuesta.status, headers, respuesta.statusText, | 
| 84 | undefined, texto) | 
| 85 | |
| 86 | } | 
| 87 | |
| 88 | } | 
| 89 | |
| 90 | } | 
| 91 | |
| 92 | } | 
| 93 | |
| 94 | } | 
| 95 | |
| 96 | exportaAHtml(consumeJson) | 
| 1 | /** | 
| 2 | * Permite que los eventos de html usen la función. | 
| 3 | * @param {function} functionInstance | 
| 4 | */ | 
| 5 | export function exportaAHtml(functionInstance) { | 
| 6 | window[nombreDeFuncionParaHtml(functionInstance)] = functionInstance | 
| 7 | } | 
| 8 | |
| 9 | /** | 
| 10 | * @param {function} valor | 
| 11 | */ | 
| 12 | export function nombreDeFuncionParaHtml(valor) { | 
| 13 | const names = valor.name.split(/\s+/g) | 
| 14 | return names[names.length - 1] | 
| 15 | } | 
| 1 | import { exportaAHtml } from "./exportaAHtml.js" | 
| 2 | import { ProblemDetails } from "./ProblemDetails.js" | 
| 3 | |
| 4 | /** | 
| 5 | * Muestra un error en la consola y en un cuadro de | 
| 6 | * alerta el mensaje de una excepción. | 
| 7 | * @param { ProblemDetails | Error | null } error descripción del error. | 
| 8 | */ | 
| 9 | export function muestraError(error) { | 
| 10 | |
| 11 | if (error === null) { | 
| 12 | |
| 13 | console.error("Error") | 
| 14 | alert("Error") | 
| 15 | |
| 16 | } else if (error instanceof ProblemDetails) { | 
| 17 | |
| 18 | let mensaje = error.title | 
| 19 | if (error.detail) { | 
| 20 | mensaje += `\n\n${error.detail}` | 
| 21 | } | 
| 22 | mensaje += `\n\nCódigo: ${error.status}` | 
| 23 | if (error.type) { | 
| 24 | mensaje += ` ${error.type}` | 
| 25 | } | 
| 26 | |
| 27 | console.error(mensaje) | 
| 28 | console.error(error) | 
| 29 | console.error("Headers:") | 
| 30 | error.headers.forEach((valor, llave) => console.error(llave, "=", valor)) | 
| 31 | alert(mensaje) | 
| 32 | |
| 33 | } else { | 
| 34 | |
| 35 | console.error(error) | 
| 36 | alert(error.message) | 
| 37 | |
| 38 | } | 
| 39 | |
| 40 | } | 
| 41 | |
| 42 | exportaAHtml(muestraError) | 
| 1 | import { exportaAHtml } from "./exportaAHtml.js" | 
| 2 | |
| 3 | /** | 
| 4 | * @param { Document | HTMLElement } raizHtml | 
| 5 | * @param { any } objeto | 
| 6 | */ | 
| 7 | export function muestraObjeto(raizHtml, objeto) { | 
| 8 | for (const [nombre, definiciones] of Object.entries(objeto)) { | 
| 9 | |
| 10 | if (Array.isArray(definiciones)) { | 
| 11 | |
| 12 | muestraArray(raizHtml, nombre, definiciones) | 
| 13 | |
| 14 | } else if (definiciones !== undefined && definiciones !== null) { | 
| 15 | |
| 16 | const elementoHtml = buscaElementoHtml(raizHtml, nombre) | 
| 17 | |
| 18 | if (elementoHtml instanceof HTMLInputElement) { | 
| 19 | |
| 20 | muestraInput(raizHtml, elementoHtml, definiciones) | 
| 21 | |
| 22 | } else if (elementoHtml !== null) { | 
| 23 | |
| 24 | for (const [propiedad, valor] of Object.entries(definiciones)) { | 
| 25 | if (propiedad in elementoHtml) { | 
| 26 | elementoHtml[propiedad] = valor | 
| 27 | } | 
| 28 | } | 
| 29 | |
| 30 | } | 
| 31 | |
| 32 | } | 
| 33 | |
| 34 | } | 
| 35 | } | 
| 36 | exportaAHtml(muestraObjeto) | 
| 37 | |
| 38 | /** | 
| 39 | * @param { Document | HTMLElement } raizHtml | 
| 40 | * @param { string } nombre | 
| 41 | */ | 
| 42 | export function buscaElementoHtml(raizHtml, nombre) { | 
| 43 | return raizHtml.querySelector( | 
| 44 | `[id="${nombre}"],[name="${nombre}"],[data-name="${nombre}"]`) | 
| 45 | } | 
| 46 | |
| 47 | /** | 
| 48 | * @param { Document | HTMLElement } raizHtml | 
| 49 | * @param { string } propiedad | 
| 50 | * @param {any[]} valores | 
| 51 | */ | 
| 52 | function muestraArray(raizHtml, propiedad, valores) { | 
| 53 | |
| 54 | const conjunto = new Set(valores) | 
| 55 | const elementos = | 
| 56 | raizHtml.querySelectorAll(`[name="${propiedad}"],[data-name="${propiedad}"]`) | 
| 57 | |
| 58 | if (elementos.length === 1) { | 
| 59 | |
| 60 | const elemento = elementos[0] | 
| 61 | if (elemento instanceof HTMLSelectElement) { | 
| 62 | for (let i = 0, options = elemento.options, len = options.length; i < len | 
| 63 | ; i++) { | 
| 64 | const option = options[i] | 
| 65 | option.selected = conjunto.has(option.value) | 
| 66 | } | 
| 67 | return | 
| 68 | } | 
| 69 | |
| 70 | } | 
| 71 | |
| 72 | for (let i = 0, len = elementos.length; i < len; i++) { | 
| 73 | const elemento = elementos[i] | 
| 74 | if (elemento instanceof HTMLInputElement) { | 
| 75 | elemento.checked = conjunto.has(elemento.value) | 
| 76 | } | 
| 77 | } | 
| 78 | |
| 79 | } | 
| 80 | |
| 81 | /** | 
| 82 | * @param { Document | HTMLElement } raizHtml | 
| 83 | * @param { HTMLInputElement } input | 
| 84 | * @param { any } definiciones | 
| 85 | */ | 
| 86 | function muestraInput(raizHtml, input, definiciones) { | 
| 87 | |
| 88 | for (const [propiedad, valor] of Object.entries(definiciones)) { | 
| 89 | |
| 90 | if (propiedad == "data-file") { | 
| 91 | |
| 92 | const img = getImgParaElementoHtml(raizHtml, input) | 
| 93 | if (img !== null) { | 
| 94 | input.dataset.file = valor | 
| 95 | input.value = "" | 
| 96 | if (valor === "") { | 
| 97 | img.src = "" | 
| 98 | img.hidden = true | 
| 99 | } else { | 
| 100 | img.src = valor | 
| 101 | img.hidden = false | 
| 102 | } | 
| 103 | } | 
| 104 | |
| 105 | } else if (propiedad in input) { | 
| 106 | |
| 107 | input[propiedad] = valor | 
| 108 | |
| 109 | } | 
| 110 | } | 
| 111 | |
| 112 | } | 
| 113 | |
| 114 | /** | 
| 115 | * @param { Document | HTMLElement } raizHtml | 
| 116 | * @param { HTMLElement } elementoHtml | 
| 117 | */ | 
| 118 | export function getImgParaElementoHtml(raizHtml, elementoHtml) { | 
| 119 | const imgId = elementoHtml.getAttribute("data-img") | 
| 120 | if (imgId === null) { | 
| 121 | return null | 
| 122 | } else { | 
| 123 | const input = buscaElementoHtml(raizHtml, imgId) | 
| 124 | if (input instanceof HTMLImageElement) { | 
| 125 | return input | 
| 126 | } else { | 
| 127 | return null | 
| 128 | } | 
| 129 | } | 
| 130 | } | 
| 1 | /** | 
| 2 | * Detalle de los errores devueltos por un servicio. | 
| 3 | */ | 
| 4 | export class ProblemDetails extends Error { | 
| 5 | |
| 6 | /** | 
| 7 | * @param {number} status | 
| 8 | * @param {Headers} headers | 
| 9 | * @param {string} title | 
| 10 | * @param {string} [type] | 
| 11 | * @param {string} [detail] | 
| 12 | */ | 
| 13 | constructor(status, headers, title, type, detail) { | 
| 14 | super(title) | 
| 15 | /** | 
| 16 | * @readonly | 
| 17 | */ | 
| 18 | this.status = status | 
| 19 | /** | 
| 20 | * @readonly | 
| 21 | */ | 
| 22 | this.headers = headers | 
| 23 | /** | 
| 24 | * @readonly | 
| 25 | */ | 
| 26 | this.type = type | 
| 27 | /** | 
| 28 | * @readonly | 
| 29 | */ | 
| 30 | this.detail = detail | 
| 31 | /** | 
| 32 | * @readonly | 
| 33 | */ | 
| 34 | this.title = title | 
| 35 | } | 
| 36 | |
| 37 | } | 
| 1 | <?php | 
| 2 | |
| 3 | require_once __DIR__ . "/INTERNAL_SERVER_ERROR.php"; | 
| 4 | require_once __DIR__ . "/devuelveProblemDetails.php"; | 
| 5 | require_once __DIR__ . "/devuelveProblemDetails.php"; | 
| 6 | |
| 7 | function devuelveErrorInterno(Throwable $error) | 
| 8 | { | 
| 9 | devuelveProblemDetails(new ProblemDetails( | 
| 10 | status: INTERNAL_SERVER_ERROR, | 
| 11 | title: $error->getMessage(), | 
| 12 | type: "/error/errorinterno.html" | 
| 13 | )); | 
| 14 | } | 
| 15 | 
| 1 | <?php | 
| 2 | |
| 3 | require_once __DIR__ . "/devuelveResultadoNoJson.php"; | 
| 4 | |
| 5 | function devuelveJson($resultado) | 
| 6 | { | 
| 7 | |
| 8 | $json = json_encode($resultado); | 
| 9 | |
| 10 | if ($json === false) { | 
| 11 | |
| 12 | devuelveResultadoNoJson(); | 
| 13 | } else { | 
| 14 | |
| 15 | http_response_code(200); | 
| 16 | header("Content-Type: application/json"); | 
| 17 | echo $json; | 
| 18 | } | 
| 19 | } | 
| 20 | 
| 1 | <?php | 
| 2 | |
| 3 | require_once __DIR__ . "/devuelveResultadoNoJson.php"; | 
| 4 | require_once __DIR__ . "/ProblemDetails.php"; | 
| 5 | |
| 6 | function devuelveProblemDetails(ProblemDetails $details) | 
| 7 | { | 
| 8 | |
| 9 | $body = ["title" => $details->title]; | 
| 10 | if ($details->type !== null) { | 
| 11 | $body["type"] = $details->type; | 
| 12 | } | 
| 13 | if ($details->detail !== null) { | 
| 14 | $body["detail"] = $details->detail; | 
| 15 | } | 
| 16 | |
| 17 | $json = json_encode($body); | 
| 18 | |
| 19 | if ($json === false) { | 
| 20 | |
| 21 | devuelveResultadoNoJson(); | 
| 22 | } else { | 
| 23 | |
| 24 | http_response_code($details->status); | 
| 25 | header("Content-Type: application/problem+json"); | 
| 26 | echo $json; | 
| 27 | } | 
| 28 | } | 
| 29 | 
| 1 | <?php | 
| 2 | |
| 3 | require_once __DIR__ . "/INTERNAL_SERVER_ERROR.php"; | 
| 4 | |
| 5 | function devuelveResultadoNoJson() | 
| 6 | { | 
| 7 | |
| 8 | http_response_code(INTERNAL_SERVER_ERROR); | 
| 9 | header("Content-Type: application/problem+json"); | 
| 10 | echo '{' . | 
| 11 | '"title": "El resultado no puede representarse como JSON."' . | 
| 12 | '"type": "/error/resultadonojson.html"' . | 
| 13 | '}'; | 
| 14 | } | 
| 15 | 
| 1 | <?php | 
| 2 | |
| 3 | const INTERNAL_SERVER_ERROR = 500; | 
| 1 | <?php | 
| 2 | |
| 3 | /** Detalle de los errores devueltos por un servicio. */ | 
| 4 | class ProblemDetails extends Exception | 
| 5 | { | 
| 6 | |
| 7 | public int $status; | 
| 8 | public string $title; | 
| 9 | public ?string $type; | 
| 10 | public ?string $detail; | 
| 11 | |
| 12 | public function __construct( | 
| 13 | int $status, | 
| 14 | string $title, | 
| 15 | ?string $type = null, | 
| 16 | ?string $detail = null, | 
| 17 | Throwable $previous = null | 
| 18 | ) { | 
| 19 | parent::__construct($title, $status, $previous); | 
| 20 | $this->status = $status; | 
| 21 | $this->type = $type; | 
| 22 | $this->title = $title; | 
| 23 | $this->detail = $detail; | 
| 24 | } | 
| 25 | } | 
| 26 |