From 4d9a946db167e1b0ad6aec5bcb5dabc37be97044 Mon Sep 17 00:00:00 2001 From: lib Date: Sun, 14 Mar 2021 19:28:18 +0800 Subject: [PATCH 1/2] feat: wrapping up exception handling --- src/gateways/gateway.service.ts | 33 +++----------------- src/gateways/interfaces/gateway.interface.ts | 6 ++++ src/libs/utils.ts | 30 +++++++++++++++++- 3 files changed, 40 insertions(+), 29 deletions(-) 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..9d52db7 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; +} \ No newline at end of file diff --git a/src/libs/utils.ts b/src/libs/utils.ts index 49dad55..9e4617c 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,30 @@ export function editFileName(req, file, cb) { .join(''); cb(null, `${name}-${randomName}${fileExtName}`); } + +export function formatErrorMessage(errorMsg: string): IGateway.IErrorStruct | null { + let 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, + ); +} From 4f2ecbb17df29751ada6db15d223943f04bcaeb2 Mon Sep 17 00:00:00 2001 From: lib Date: Sun, 14 Mar 2021 19:28:35 +0800 Subject: [PATCH 2/2] chore: code formatting --- src/gateways/interfaces/gateway.interface.ts | 2 +- src/libs/utils.ts | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/gateways/interfaces/gateway.interface.ts b/src/gateways/interfaces/gateway.interface.ts index 9d52db7..92c3a2e 100644 --- a/src/gateways/interfaces/gateway.interface.ts +++ b/src/gateways/interfaces/gateway.interface.ts @@ -8,4 +8,4 @@ export interface IErrorStruct { statusCode: number; message?: any; error?: any; -} \ No newline at end of file +} diff --git a/src/libs/utils.ts b/src/libs/utils.ts index 9e4617c..41c004d 100644 --- a/src/libs/utils.ts +++ b/src/libs/utils.ts @@ -79,8 +79,10 @@ export function editFileName(req, file, cb) { cb(null, `${name}-${randomName}${fileExtName}`); } -export function formatErrorMessage(errorMsg: string): IGateway.IErrorStruct | null { - let errorMsgStr: string = errorMsg.split('-')[1]; +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;