diff --git a/src/gateways/gateway.service.ts b/src/gateways/gateway.service.ts index 830a907..2b0c296 100644 --- a/src/gateways/gateway.service.ts +++ b/src/gateways/gateway.service.ts @@ -2,6 +2,7 @@ import { HttpException, HttpStatus, Injectable } from '@nestjs/common'; import { Request } from 'express'; import { config } from '../../config'; import { APIRequestFactory } from '../libs/request-factory'; +import { ExceptionHandler } from '../libs/utils'; import * as IGateway from './interfaces'; @Injectable() @@ -102,13 +103,7 @@ export class GatewayService { json: true, }); } catch (error) { - throw new HttpException( - { - status: HttpStatus.INTERNAL_SERVER_ERROR, - error: error.message, - }, - HttpStatus.INTERNAL_SERVER_ERROR, - ); + throw new ExceptionHandler(error); } } @@ -185,13 +180,7 @@ export class GatewayService { json: true, }); } catch (error) { - throw new HttpException( - { - status: HttpStatus.INTERNAL_SERVER_ERROR, - error: error.message, - }, - HttpStatus.INTERNAL_SERVER_ERROR, - ); + throw new ExceptionHandler(error); } } @@ -260,13 +249,7 @@ export class GatewayService { json: true, }); } catch (error) { - throw new HttpException( - { - status: HttpStatus.INTERNAL_SERVER_ERROR, - error: error.message, - }, - HttpStatus.INTERNAL_SERVER_ERROR, - ); + throw new ExceptionHandler(error); } } @@ -334,13 +317,7 @@ export class GatewayService { json: true, }); } catch (error) { - throw new HttpException( - { - status: HttpStatus.INTERNAL_SERVER_ERROR, - error: error.message, - }, - HttpStatus.INTERNAL_SERVER_ERROR, - ); + throw new ExceptionHandler(error); } } } diff --git a/src/gateways/interfaces/gateway.interface.ts b/src/gateways/interfaces/gateway.interface.ts index 9b26658..92c3a2e 100644 --- a/src/gateways/interfaces/gateway.interface.ts +++ b/src/gateways/interfaces/gateway.interface.ts @@ -3,3 +3,9 @@ export interface IServerConf { host: string; port: number; } + +export interface IErrorStruct { + statusCode: number; + message?: any; + error?: any; +} diff --git a/src/libs/utils.ts b/src/libs/utils.ts index 49dad55..41c004d 100644 --- a/src/libs/utils.ts +++ b/src/libs/utils.ts @@ -1,5 +1,6 @@ -import { Logger } from '@nestjs/common'; +import { HttpException, HttpStatus, Logger } from '@nestjs/common'; import { extname } from 'path'; +import * as IGateway from '../gateways/interfaces'; /** * @description Check Memory Info @@ -77,3 +78,32 @@ export function editFileName(req, file, cb) { .join(''); cb(null, `${name}-${randomName}${fileExtName}`); } + +export function formatErrorMessage( + errorMsg: string, +): IGateway.IErrorStruct | null { + const errorMsgStr: string = errorMsg.split('-')[1]; + if (!errorMsgStr) return null; + errorMsgStr.replace(/\/\n/gi, ''); + return isJsonString(errorMsgStr) ? JSON.parse(errorMsgStr) : null; +} + +export function ExceptionHandler(error: Error): void { + const errorStruct: IGateway.IErrorStruct = formatErrorMessage(error.message); + if (!errorStruct) { + throw new HttpException( + { + status: HttpStatus.INTERNAL_SERVER_ERROR, + error: error.message, + }, + HttpStatus.INTERNAL_SERVER_ERROR, + ); + } + throw new HttpException( + { + status: errorStruct.statusCode, + error: errorStruct.message || errorStruct.error, + }, + errorStruct.statusCode, + ); +}