From 90091c39cbda79b8bd45d985923bd64a0320243e Mon Sep 17 00:00:00 2001 From: Derek Guenther Date: Mon, 16 Dec 2024 12:33:03 -0500 Subject: [PATCH] Pass through Chainport errors (#1798) --- src/bridges/chainport.service.ts | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/bridges/chainport.service.ts b/src/bridges/chainport.service.ts index 8b0493ac..47d696e4 100644 --- a/src/bridges/chainport.service.ts +++ b/src/bridges/chainport.service.ts @@ -6,6 +6,7 @@ import { BadGatewayException, Injectable, NotFoundException, + UnprocessableEntityException, } from '@nestjs/common'; import { AxiosError, AxiosResponse } from 'axios'; import Joi from 'joi'; @@ -163,6 +164,22 @@ export type ChainportPort = port_in_ack: boolean | null; }; +export type ChainportError = { + error: { + code: string; + description: string; + }; + status: string; +}; + +const chainportErrorSchema = Joi.object({ + error: Joi.object({ + code: Joi.string().required(), + description: Joi.string().required(), + }).required(), + status: Joi.string().required(), +}); + @Injectable() export class ChainportService { constructor( @@ -185,6 +202,16 @@ export class ChainportService { } - ${JSON.stringify(e.response?.data)}`, e.stack ?? '', ); + + const validationResult = chainportErrorSchema.validate( + e.response?.data, + ); + if (!validationResult.error) { + throw new UnprocessableEntityException( + validationResult.value.error.description, + ); + } + throw new BadGatewayException(e); }), ),