| 1 | /** |
| 2 | * @param {{ |
| 3 | * errorCode: number, |
| 4 | * errorMessage: string |
| 5 | * }} responseObject |
| 6 | */ |
| 7 | export function conexionMqttPerdida(responseObject) { |
| 8 | if (responseObject.errorCode !== 0) { |
| 9 | const mensaje = "Conexión MQTT terminada " + responseObject.errorMessage |
| 10 | console.error(mensaje) |
| 11 | alert(mensaje) |
| 12 | } |
| 13 | } |
| 1 | /** |
| 2 | * Añade caracteres al azar a una raíz, para obtener un clientId único. |
| 3 | * @param {string} raiz |
| 4 | */ |
| 5 | export function creaIdCliente(raiz) { |
| 6 | const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" |
| 7 | for (var i = 0; i < 15; i++) { |
| 8 | raiz += chars.charAt(Math.floor(Math.random() * chars.length)) |
| 9 | } |
| 10 | return raiz |
| 11 | } |
| 1 | // @ts-nocheck |
| 2 | /** |
| 3 | * Envá un valor al servidor de MQTT y es reenviado a todos los dispositivos |
| 4 | * suscritos al tópico indicado |
| 5 | * @param {Paho.MQTT.Client} cliente |
| 6 | * @param {string} mensaje |
| 7 | * @param {string} topico |
| 8 | */ |
| 9 | export function enviaMensajeMqtt(cliente, mensaje, topico) { |
| 10 | const mensajeMqtt = new Paho.MQTT.Message(mensaje) |
| 11 | mensajeMqtt.destinationName = topico |
| 12 | cliente.send(mensajeMqtt) |
| 13 | } |
| 1 | /** |
| 2 | * @param {{errorMessage: string}} res |
| 3 | */ |
| 4 | export function falloEnLaConexionMqtt(res) { |
| 5 | const mensaje = "Fallo en conexión MQTT: " + res.errorMessage |
| 6 | console.error(mensaje) |
| 7 | alert(mensaje) |
| 8 | } |
| 1 | import { muestraError } from "./muestraError.js" |
| 2 | |
| 3 | /** |
| 4 | * Intercepta Response.prototype.json para capturar errores de parseo |
| 5 | * y asegurar que se reporten correctamente en navegadores Chromium. |
| 6 | */ |
| 7 | { |
| 8 | const originalJson = Response.prototype.json |
| 9 | |
| 10 | Response.prototype.json = function () { |
| 11 | // Llamamos al método original usando el contexto (this) de la respuesta |
| 12 | return originalJson.apply(this, arguments) |
| 13 | .catch((/** @type {any} */ error) => { |
| 14 | // Corrige un error de Chrome que evita el manejo correcto de errores. |
| 15 | throw new Error(error) |
| 16 | }) |
| 17 | } |
| 18 | } |
| 19 | |
| 20 | window.onerror = function ( |
| 21 | /** @type {string} */ _message, |
| 22 | /** @type {string} */ _url, |
| 23 | /** @type {number} */ _line, |
| 24 | /** @type {number} */ _column, |
| 25 | /** @type {Error} */ errorObject |
| 26 | ) { |
| 27 | muestraError(errorObject) |
| 28 | return true |
| 29 | } |
| 30 | |
| 31 | window.addEventListener('unhandledrejection', event => { |
| 32 | muestraError(event.reason) |
| 33 | event.preventDefault() |
| 34 | }) |
| 35 |
| 1 | import { ProblemDetailsError } from "./ProblemDetailsError.js" |
| 2 | |
| 3 | /** |
| 4 | * Muestra los datos de una Error en la consola y en un cuadro de alerta. |
| 5 | * @param { ProblemDetailsError | Error | null } error descripción del error. |
| 6 | */ |
| 7 | export function muestraError(error) { |
| 8 | |
| 9 | if (error === null) { |
| 10 | |
| 11 | console.error("Error") |
| 12 | alert("Error") |
| 13 | |
| 14 | } else if (error instanceof ProblemDetailsError) { |
| 15 | |
| 16 | const problemDetails = error.problemDetails |
| 17 | |
| 18 | let mensaje = |
| 19 | typeof problemDetails["title"] === "string" ? problemDetails["title"] : "" |
| 20 | if (typeof problemDetails["detail"] === "string") { |
| 21 | if (mensaje !== "") { |
| 22 | mensaje += "\n\n" |
| 23 | } |
| 24 | mensaje += problemDetails["detail"] |
| 25 | } |
| 26 | if (mensaje === "") { |
| 27 | mensaje = "Error" |
| 28 | } |
| 29 | console.error(error, problemDetails) |
| 30 | alert(mensaje) |
| 31 | |
| 32 | } else { |
| 33 | |
| 34 | console.error(error) |
| 35 | alert(error.message) |
| 36 | |
| 37 | } |
| 38 | |
| 39 | } |
| 1 | export class ProblemDetailsError extends Error { |
| 2 | |
| 3 | /** |
| 4 | * Detalle de los errores devueltos por un servicio. |
| 5 | * Crea una instancia de ProblemDetailsError. |
| 6 | * @param {object} problemDetails Objeto con la descripcipon del error. |
| 7 | */ |
| 8 | constructor(problemDetails) { |
| 9 | |
| 10 | super(typeof problemDetails["detail"] === "string" |
| 11 | ? problemDetails["detail"] |
| 12 | : (typeof problemDetails["title"] === "string" |
| 13 | ? problemDetails["title"] |
| 14 | : "Error")) |
| 15 | |
| 16 | this.problemDetails = problemDetails |
| 17 | |
| 18 | } |
| 19 | |
| 20 | } |