Skip to content

Commit

Permalink
Handle errors in sendMessageNases (#1662)
Browse files Browse the repository at this point in the history
* Handle errors in sendMessageNases

* Rewrite into try-catch

* Eslint autofix
  • Loading branch information
mpinter authored Nov 6, 2024
1 parent 0ffec6b commit 7b88868
Showing 1 changed file with 36 additions and 28 deletions.
64 changes: 36 additions & 28 deletions nest-forms-backend/src/nases/utils-services/tokens.nases.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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'
Expand Down Expand Up @@ -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<NasesSendResponse> {
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<string>(
'SLOVENSKO_SK_CONTAINER_URI',
)}/api/sktalk/receive_and_save_to_outbox`,
Expand All @@ -482,40 +492,38 @@ export default class NasesUtilsService {
},
},
)
.then((response: AxiosResponse<NasesSendFormDataDto>) => {
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: {
message:
'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<boolean> {
Expand Down

0 comments on commit 7b88868

Please sign in to comment.