| 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 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 | /** |
| 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 | /** |
| 2 | * @param { { errorMessage: string } } res |
| 3 | */ |
| 4 | export function falloEnLaConexionMqtt(res) { |
| 5 | const mensaje = "Fallo en conexión:" + res.errorMessage |
| 6 | console.error(mensaje) |
| 7 | alert(mensaje) |
| 8 | } |
| 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 | /** |
| 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 | } |