From 7b88868c0b289d1db6618e568d8d965178af3f38 Mon Sep 17 00:00:00 2001 From: Martin Pinter Date: Wed, 6 Nov 2024 09:28:04 +0100 Subject: [PATCH] Handle errors in sendMessageNases (#1662) * Handle errors in sendMessageNases * Rewrite into try-catch * Eslint autofix --- .../utils-services/tokens.nases.service.ts | 64 +++++++++++-------- 1 file changed, 36 insertions(+), 28 deletions(-) diff --git a/nest-forms-backend/src/nases/utils-services/tokens.nases.service.ts b/nest-forms-backend/src/nases/utils-services/tokens.nases.service.ts index 72573ac74..4b1ef8db0 100644 --- a/nest-forms-backend/src/nases/utils-services/tokens.nases.service.ts +++ b/nest-forms-backend/src/nases/utils-services/tokens.nases.service.ts @@ -6,7 +6,7 @@ import { Stream } from 'node:stream' import { Injectable, Logger } from '@nestjs/common' import { ConfigService } from '@nestjs/config' import { Forms } from '@prisma/client' -import axios, { AxiosResponse } from 'axios' +import axios, { AxiosError, AxiosResponse } from 'axios' import { FormDefinitionSlovenskoSk, isSlovenskoSkFormDefinition, @@ -32,7 +32,6 @@ import alertError from '../../utils/logging' import MinioClientSubservice from '../../utils/subservices/minio-client.subservice' import { NasesIsMessageDeliveredDto, - NasesSendFormDataDto, NasesSendResponse, ResponseGdprDataDto, } from '../dtos/responses.dto' @@ -462,14 +461,25 @@ export default class NasesUtilsService { }` } + // TODO nicer error handling, for now it is assumed this function never throws and a lot of code relies on that async sendMessageNases( jwtToken: string, data: Forms, senderUri?: string, ): Promise { - const message = await this.createEnvelopeSendMessage(data, senderUri) - const result = await axios - .post( + let message + try { + message = await this.createEnvelopeSendMessage(data, senderUri) + } catch (error) { + return { + status: 500, + data: { + message: `Failed to create envelope for nases message: ${(error as Error)?.message || 'Unknown error'}. Details: ${JSON.stringify(error)}`, + }, + } + } + try { + const response = await axios.post( `${this.configService.getOrThrow( 'SLOVENSKO_SK_CONTAINER_URI', )}/api/sktalk/receive_and_save_to_outbox`, @@ -482,18 +492,8 @@ export default class NasesUtilsService { }, }, ) - .then((response: AxiosResponse) => { - if (response.data) { - if (response.data.receive_result !== 0) { - return { - status: 422, - data: { - message: this.getNasesError(response.data.receive_result), - }, - } - } - return { status: 200, data: response.data } - } + + if (!response.data) { return { status: 422, data: { @@ -501,21 +501,29 @@ export default class NasesUtilsService { 'Server error in response data, please contact administrator', }, } - }) - .catch((error) => { - // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access - if (error.response && error.response.data) { - // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-assignment - return { status: error.response.status, data: error.response.data } - } + } + + if (response.data.receive_result !== 0) { return { - status: 400, + status: 422, data: { - message: 'Other server error, please contact administrator', + message: this.getNasesError(response.data.receive_result), }, } - }) - return result + } + + return { status: 200, data: response.data } + } catch (error) { + if (error instanceof AxiosError && error.response?.data) { + return { status: error.response.status, data: error.response.data } + } + return { + status: 400, + data: { + message: 'Other server error, please contact administrator', + }, + } + } } async isNasesMessageDelivered(formId: string): Promise {