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 | } |
14 | |
15 | // Permite que los eventos de html usen la función. |
16 | window["conexionMqttPerdida"] = conexionMqttPerdida |
1 | /** |
2 | * Añade caracteres al azar a una raíz, para obtener un clientId único por cada |
3 | * instancia que se conecte al servidor de mqtt. |
4 | * @param {string} raiz |
5 | */ |
6 | export function creaClientIdMqtt(raiz) { |
7 | const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" |
8 | for (var i = 0; i < 15; i++) { |
9 | raiz += chars.charAt(Math.floor(Math.random() * chars.length)) |
10 | } |
11 | return raiz |
12 | } |
13 | |
14 | // Permite que los eventos de html usen la función. |
15 | window["creaClientIdMqtt"] = creaClientIdMqtt |
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 | } |
9 | |
10 | // Permite que los eventos de html usen la función. |
11 | window["falloEnLaConexionMqtt"] = falloEnLaConexionMqtt |
1 | import { ProblemDetails } from "./ProblemDetails.js" |
2 | |
3 | /** |
4 | * Muestra un error en la consola y en un cuadro de |
5 | * alerta el mensaje de una excepción. |
6 | * @param { ProblemDetails | Error | null } error descripción del error. |
7 | */ |
8 | export function muestraError(error) { |
9 | if (error === null) { |
10 | console.log("Error") |
11 | alert("Error") |
12 | } else if (error instanceof ProblemDetails) { |
13 | let mensaje = error.title |
14 | if (error.detail) { |
15 | mensaje += `\n\n${error.detail}` |
16 | } |
17 | mensaje += `\n\nCódigo: ${error.status}` |
18 | if (error.type) { |
19 | mensaje += ` ${error.type}` |
20 | } |
21 | console.error(mensaje) |
22 | console.error(error) |
23 | alert(mensaje) |
24 | } else { |
25 | console.error(error) |
26 | alert(error.message) |
27 | } |
28 | } |
29 | |
30 | // Permite que los eventos de html usen la función. |
31 | window["muestraError"] = muestraError |
1 | export const ProblemDetails_BadRequest = 400 |
2 | export const ProblemDetails_NotFound = 404 |
3 | export const ProblemDetails_InternalServerError = 500 |
4 | |
5 | export class ProblemDetails extends Error { |
6 | |
7 | /** |
8 | * @param {number} status |
9 | * @param {string} title |
10 | * @param {string} [detail] |
11 | * @param {string} [type] |
12 | */ |
13 | constructor(status, title, detail, type) { |
14 | super(title) |
15 | /** @readonly */ |
16 | this.status = status |
17 | /** @readonly */ |
18 | this.type = type |
19 | /** @readonly */ |
20 | this.title = title |
21 | /** @readonly */ |
22 | this.detail = detail |
23 | } |
24 | |
25 | } |