From d1e5892a0daf0f613c144c4715aaf65f128b7e07 Mon Sep 17 00:00:00 2001 From: Alvar San Martin Date: Wed, 13 May 2026 17:08:19 +0200 Subject: [PATCH] Mejorado el control de errores para ALAI --- .yarnrc.yml | 4 +- docs/sim-objenious/Alerts.bru | 38 +++++++++++++++++++ package.json | 2 +- .../aplication/SimAlai.controller.ts | 13 ++++++- .../aplication/SimAlai.usecases.ts | 31 ++++++++------- .../certificates/.debugToken | 2 +- .../config/httpClient.config.ts | 4 +- .../infrastructure/AlaiRepository.ts | 31 +++++++++++---- 8 files changed, 97 insertions(+), 28 deletions(-) create mode 100644 docs/sim-objenious/Alerts.bru diff --git a/.yarnrc.yml b/.yarnrc.yml index a36c533..ff0d611 100644 --- a/.yarnrc.yml +++ b/.yarnrc.yml @@ -4,8 +4,8 @@ enableGlobalCache: false nodeLinker: node-modules +npmRegistryServer: "https://registry.npmjs.org/" + npmScopes: sf-alvar: npmRegistryServer: "https://git.savefamilygps.net/api/packages/SaveFamily/npm/" - -npmRegistryServer: "https://registry.npmjs.org/" diff --git a/docs/sim-objenious/Alerts.bru b/docs/sim-objenious/Alerts.bru new file mode 100644 index 0000000..32a5177 --- /dev/null +++ b/docs/sim-objenious/Alerts.bru @@ -0,0 +1,38 @@ +meta { + name: Alerts + type: http + seq: 23 +} + +get { + url: https://api-getway.objenious.com/ws/alarms + body: formUrlEncoded + auth: bearer +} + +auth:bearer { + token: {{ws-access-token-partenaire}} +} + +body:json { + { + "identifier": { + "identifiers": ["8933201124059175967"], + "identifierType": "ICCID" + } + } +} + +body:form-urlencoded { + ~identifier.identifierType: "ICCID" + ~identifier.identifiers: ["8933201124059175967"] +} + +vars:pre-request { + ~id: 5187320 +} + +settings { + encodeUrl: true + timeout: 0 +} diff --git a/package.json b/package.json index 6195332..9588af0 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "sim-eventos", "version": "1.0.0", - "packageManager": "yarn@4.12.0", + "packageManager": "yarn@4.14.1", "workspaces": [ "packages/*" ], diff --git a/packages/sim-consumidor-alai/aplication/SimAlai.controller.ts b/packages/sim-consumidor-alai/aplication/SimAlai.controller.ts index 4f54c80..304778c 100644 --- a/packages/sim-consumidor-alai/aplication/SimAlai.controller.ts +++ b/packages/sim-consumidor-alai/aplication/SimAlai.controller.ts @@ -7,6 +7,11 @@ import { SimEvents } from "sim-shared/domain/SimEvents.js"; import { iccidValidator } from "./httpValidators.js"; import { alaiSimToCommonSim } from "#domain/transformers.js"; +type ErrorUsecase = { + msg: string, + stackTrace?: string +} + export class SimAlaiController { constructor( @@ -41,12 +46,13 @@ export class SimAlaiController { } } + /** * Metodo duplicado se puede generalizar la a una clase sharedController con las funciones basicas * TODO: meter un check de 429 */ private async tryUseCase - (msg: ConsumeMessage, usecase: () => Promise>): Promise> { + (msg: ConsumeMessage, usecase: () => Promise>): Promise> { try { const result = await usecase() if (result.error == undefined) { @@ -61,7 +67,10 @@ export class SimAlaiController { console.error("Error general procesando el caso de uso (Alai)") this.eventBus.nack(msg) return { - error: String(e) + error: { + msg: String(e), + stackTrace: String(e) + } } } } diff --git a/packages/sim-consumidor-alai/aplication/SimAlai.usecases.ts b/packages/sim-consumidor-alai/aplication/SimAlai.usecases.ts index 5aaa000..ce523ea 100644 --- a/packages/sim-consumidor-alai/aplication/SimAlai.usecases.ts +++ b/packages/sim-consumidor-alai/aplication/SimAlai.usecases.ts @@ -10,13 +10,10 @@ */ import { AlaiAPI } from "#domain/AlaiAPI.js"; import { AlaiRepository } from "#infrastructure/AlaiRepository.js"; -import { ConsumeMessage } from "amqplib"; -import { error } from "node:console"; import { ErrorOrderDTO, FinishOrderDTO, UpdateOrderDTO } from "sim-shared/domain/Order.js"; import { Result } from "sim-shared/domain/Result.js"; import { HttpClient } from "sim-shared/infrastructure/HTTPClient.js"; import { OrderRepository } from "sim-shared/infrastructure/OrderRepository.js"; -import { isOmittedExpression } from "typescript"; export class SimAlaiUsecases { constructor( @@ -45,7 +42,7 @@ export class SimAlaiUsecases { return order } - private async setFailed(correlation_id: string, reason: string, detail?: string) { + private async setFailed(correlation_id: string, reason: string, stackTrace?: string) { // En NOS el updateOrder se hace con el correlation_id que viene en la cabecera del // mensaje consumido const updateData: ErrorOrderDTO = { @@ -53,7 +50,7 @@ export class SimAlaiUsecases { correlation_id: correlation_id, reason: reason, error: reason, - stackTrace: detail + stackTrace: stackTrace } console.log("SET FAILED DATA:", updateData) @@ -67,11 +64,11 @@ export class SimAlaiUsecases { * a peticiones de lectura (no pasan por la cola y no generan un order) */ public usecaseTemplate( - func: (_: T) => Promise>, + func: (_: T) => Promise>, args: T, correlation_id?: string | undefined ) { - return async (): Promise> => { + return async (): Promise> => { // Operacion pending -> running if (correlation_id != undefined) this.setRunning(correlation_id) @@ -86,7 +83,7 @@ export class SimAlaiUsecases { if (res.error != undefined) { console.log("Error peticion: ", res, correlation_id) if (correlation_id != undefined) - this.setFailed(correlation_id, res.error) + this.setFailed(correlation_id, res.error.msg, res.error.stackTrace) .then(e => console.log("failed", e)) .catch(e => console.error(e)) return res; @@ -100,7 +97,9 @@ export class SimAlaiUsecases { if (correlation_id != undefined) this.setFailed(correlation_id, "Error general de operacion de SIM (NOS) ", String(e)).then() return { - error: "Error general de operacion de SIM (NOS) " + String(e) + error: { + msg: "Error general de operacion de SIM (NOS) " + String(e) + } } } @@ -119,7 +118,9 @@ export class SimAlaiUsecases { if (sim.data == undefined) { return { - error: `La sim ${iccid} no se ha encontrado` + error: { + msg: `La sim ${iccid} no se ha encontrado` + } } } @@ -127,7 +128,9 @@ export class SimAlaiUsecases { if (subscriptionId == undefined) { return { - error: `La sim ${iccid} no tiene un id de subscripción` + error: { + msg: `La sim ${iccid} no tiene un id de subscripción` + } } } @@ -244,7 +247,7 @@ export class SimAlaiUsecases { * Para sacar los datos de una liena hay que sacar sim -> subscripcion -> imei * son 3 llamadas distintas. */ - public async selectCompleteSim(iccid: string): Promise(promiseReq: Promise>): Promise> { + private async manageRequest(promiseReq: Promise>): Promise> { try { const res = await promiseReq return { @@ -19,19 +24,26 @@ export class AlaiRepository { } } catch (e) { if (axios.isAxiosError(e)) { + console.log("ERROR REQUEST ", e.response) const error = e as AxiosError return { - error: error.code + " : " + String(error.response?.statusText) + error: { + msg: error.code + " : " + String(error.response?.statusText), + stackTrace: JSON.stringify(error.response?.data) + } } } else { return { - error: String(e) + error: { + msg: String(e), + stackTrace: String(e) + } } } } } - public static async login(httpsAgent: https.Agent): Promise> { + public static async login(httpsAgent: https.Agent): Promise> { const alaiUrl = env.ALAI_API_URL const endpoint = "/v1/auth/login" const fullUrl = alaiUrl + endpoint @@ -50,11 +62,16 @@ export class AlaiRepository { if (axios.isAxiosError(e)) { const error = e as AxiosError return { - error: error.code + " : " + String(error.response?.statusText) + error: { + msg: error.code + " : " + String(error.response?.statusText), + stackTrace: String(error) + } } } else { return { - error: String(e) + error: { + msg: String(e) + } } } } @@ -101,7 +118,7 @@ export class AlaiRepository { /** * Antes se usaba PATCH /v1/sim/{iccid}/{orderId} pero en la docu ha pasado a POST */ - public async createReserve(orderId: string, iccid: string): Promise> { + public async createReserve(orderId: string, iccid: string): Promise> { const endpoint = `/v1/sim/${iccid}/order/${orderId}` // Crear la reserva no usa datos en el body const promReq = this.httpClient.post(endpoint, undefined)