Skip to content

Commit

Permalink
Merge pull request #12 from one-piece-team1/feat-wrapping-exception
Browse files Browse the repository at this point in the history
Feat wrapping exception
  • Loading branch information
libterty authored Mar 14, 2021
2 parents d7dc80d + 4f2ecbb commit 41f2653
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 29 deletions.
33 changes: 5 additions & 28 deletions src/gateways/gateway.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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);
}
}
}
6 changes: 6 additions & 0 deletions src/gateways/interfaces/gateway.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,9 @@ export interface IServerConf {
host: string;
port: number;
}

export interface IErrorStruct {
statusCode: number;
message?: any;
error?: any;
}
32 changes: 31 additions & 1 deletion src/libs/utils.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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,
);
}

0 comments on commit 41f2653

Please sign in to comment.