Skip to content

Commit

Permalink
resolve conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
andrechristikan committed Feb 24, 2023
2 parents 6307bc7 + bede6b9 commit 83a0e4b
Show file tree
Hide file tree
Showing 8 changed files with 249 additions and 259 deletions.
11 changes: 5 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ack-nestjs-boilerplate-kafka",
"version": "3.4.3",
"version": "3.5.0",
"description": "Ack NestJs Boilerplate Kafka",
"repository": {
"type": "git",
Expand Down Expand Up @@ -48,7 +48,7 @@
"rollback": "yarn rollback:setting && yarn rollback:apikey"
},
"dependencies": {
"@aws-sdk/client-s3": "^3.276.0",
"@aws-sdk/client-s3": "^3.278.0",
"@faker-js/faker": "^7.6.0",
"@joi/date": "^2.1.0",
"@nestjs/axios": "^2.0.0",
Expand All @@ -63,18 +63,18 @@
"@nestjs/schedule": "^2.2.0",
"@nestjs/swagger": "^6.2.1",
"@nestjs/terminus": "^9.2.1",
"@nestjs/throttler": "^4.0.0",
"@types/response-time": "^2.3.5",
"bcryptjs": "^2.4.3",
"class-transformer": "^0.5.1",
"class-validator": "^0.14.0",
"crypto-js": "^4.1.1",
"express-rate-limit": "^6.7.0",
"geolib": "^3.3.3",
"helmet": "^6.0.1",
"joi": "^17.8.3",
"kafkajs": "^2.2.3",
"moment": "^2.29.4",
"mongoose": "^6.9.2",
"mongoose": "^6.10.0",
"morgan": "^1.10.0",
"nest-winston": "^1.8.0",
"nestjs-command": "^3.1.3",
Expand Down Expand Up @@ -104,13 +104,12 @@
"@types/cron": "^2.0.0",
"@types/crypto-js": "^4.1.1",
"@types/express": "^4.17.17",
"@types/express-rate-limit": "^6.0.0",
"@types/jest": "^29.4.0",
"@types/lodash": "^4.14.191",
"@types/morgan": "^1.9.4",
"@types/ms": "^0.7.31",
"@types/multer": "^1.4.7",
"@types/node": "^18.14.0",
"@types/node": "^18.14.1",
"@types/passport-jwt": "^3.0.8",
"@types/supertest": "^2.0.12",
"@types/ua-parser-js": "^0.7.36",
Expand Down
261 changes: 126 additions & 135 deletions src/common/error/filters/error.http.filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,154 +65,145 @@ export class ErrorHttpFilter implements ExceptionFilter {
request.repoVersion ??
this.configService.get<string>('app.repoVersion');

// Debugger
try {
this.debuggerService.error(
request?.id ? request.id : ErrorHttpFilter.name,
{
description:
exception instanceof Error
? exception.message
: exception.toString(),
class: __class ?? ErrorHttpFilter.name,
function: __function ?? this.catch.name,
path: __path,
},
exception
);
} catch (err: unknown) {}

let statusHttp: HttpStatus = HttpStatus.INTERNAL_SERVER_ERROR;
if (exception instanceof HttpException) {
const statusHttp: number = exception.getStatus();
statusHttp = exception.getStatus();
const responseExpress: Response = ctx.getResponse<Response>();

// Debugger
try {
this.debuggerService.error(
request?.id ? request.id : ErrorHttpFilter.name,
{
description: exception.message,
class: __class,
function: __function,
path: __path,
},
exception
);
} catch (err: unknown) {}

// Restructure
const response = exception.getResponse();

if (!this.isErrorException(response)) {
responseExpress.status(statusHttp).json(response);
if (this.isErrorException(response)) {
const responseException = response as IErrorException;
const {
statusCode,
message,
_errorType,
data,
properties,
_metadata,
} = responseException;

let { errors, _error } = responseException;
if (errors?.length > 0) {
errors =
_errorType === ERROR_TYPE.IMPORT
? await this.messageService.getImportErrorsMessage(
errors as IValidationErrorImport[],
customLang
)
: await this.messageService.getRequestErrorsMessage(
errors as ValidationError[],
customLang
);
}

if (!_error) {
_error =
'message' in exception ? exception.message : undefined;
} else if (typeof _error !== 'string') {
_error = JSON.stringify(_error);
}

const mapMessage: string | IMessage =
await this.messageService.get(message, {
customLanguages: customLang,
properties,
});

const resMetadata: IErrorHttpFilterMetadata = {
languages: customLang,
timestamp: __timestamp,
timezone: __timezone,
requestId: __requestId,
path: __path,
version: __version,
repoVersion: __repoVersion,
..._metadata,
};

const resResponse: IErrorHttpFilter = {
statusCode: statusCode ?? statusHttp,
message: mapMessage,
_error,
errors: errors as IErrors[] | IErrorsImport[],
_metadata: resMetadata,
data,
};

responseExpress
.setHeader('x-custom-lang', customLang)
.setHeader('x-timestamp', __timestamp)
.setHeader('x-timezone', __timezone)
.setHeader('x-request-id', __requestId)
.setHeader('x-version', __version)
.setHeader('x-repo-version', __repoVersion)
.status(statusHttp)
.json(resResponse);

return;
}

const responseException = response as IErrorException;
const {
statusCode,
message,
_errorType,
data,
properties,
_metadata,
} = responseException;

let { errors, _error } = responseException;
if (errors?.length > 0) {
errors =
_errorType === ERROR_TYPE.IMPORT
? await this.messageService.getImportErrorsMessage(
errors as IValidationErrorImport[],
customLang
)
: await this.messageService.getRequestErrorsMessage(
errors as ValidationError[],
customLang
);
}

if (!_error) {
_error = 'message' in exception ? exception.message : undefined;
} else if (typeof _error !== 'string') {
_error = JSON.stringify(_error);
}

const mapMessage: string | IMessage = await this.messageService.get(
message,
{ customLanguages: customLang, properties }
);

const resMetadata: IErrorHttpFilterMetadata = {
languages: customLang,
timestamp: __timestamp,
timezone: __timezone,
requestId: __requestId,
path: __path,
version: __version,
repoVersion: __repoVersion,
..._metadata,
};

const resResponse: IErrorHttpFilter = {
statusCode: statusCode ?? statusHttp,
message: mapMessage,
_error,
errors: errors as IErrors[] | IErrorsImport[],
_metadata: resMetadata,
data,
};

responseExpress
.setHeader('x-custom-lang', customLang)
.setHeader('x-timestamp', __timestamp)
.setHeader('x-timezone', __timezone)
.setHeader('x-request-id', __requestId)
.setHeader('x-version', __version)
.setHeader('x-repo-version', __repoVersion)
.status(statusHttp)
.json(resResponse);
} else {
// In certain situations `httpAdapter` might not be available in the
// constructor method, thus we should resolve it here.
const message: string = (await this.messageService.get(
'http.serverError.internalServerError'
)) as string;

const _metadata: IErrorHttpFilterMetadata = {
languages: customLang,
timestamp: __timestamp,
timezone: __timezone,
requestId: __requestId,
path: __path,
version: __version,
repoVersion: __repoVersion,
};

const responseBody = {
statusCode: HttpStatus.INTERNAL_SERVER_ERROR,
message,
_error:
exception instanceof Error && 'message' in exception
? exception.message
: undefined,
_metadata,
};

// Debugger
try {
this.debuggerService.error(
ErrorHttpFilter.name,
{
description: message,
class: ErrorHttpFilter.name,
function: 'catch',
path: __path,
},
exception
);
} catch (err: unknown) {}

const responseExpress: Response = ctx.getResponse<Response>();
responseExpress
.setHeader('x-custom-lang', customLang)
.setHeader('x-timestamp', __timestamp)
.setHeader('x-timezone', __timezone)
.setHeader('x-request-id', __requestId)
.setHeader('x-version', __version)
.setHeader('x-repo-version', __repoVersion)
.status(HttpStatus.INTERNAL_SERVER_ERROR)
.json(responseBody);
}

// In certain situations `httpAdapter` might not be available in the
// constructor method, thus we should resolve it here.
const message: string = await this.messageService.get(
`http.${statusHttp}`
);

const _metadata: IErrorHttpFilterMetadata = {
languages: customLang,
timestamp: __timestamp,
timezone: __timezone,
requestId: __requestId,
path: __path,
version: __version,
repoVersion: __repoVersion,
};

const responseBody = {
statusCode: statusHttp,
message,
_error:
exception instanceof Error && 'message' in exception
? exception.message
: exception,
_metadata,
};

const responseExpress: Response = ctx.getResponse<Response>();
responseExpress
.setHeader('x-custom-lang', customLang)
.setHeader('x-timestamp', __timestamp)
.setHeader('x-timezone', __timezone)
.setHeader('x-request-id', __requestId)
.setHeader('x-version', __version)
.setHeader('x-repo-version', __repoVersion)
.status(HttpStatus.INTERNAL_SERVER_ERROR)
.json(responseBody);

return;
}

isErrorException(obj: any): obj is IErrorException {
return 'statusCode' in obj && 'message' in obj;
return typeof obj === 'object'
? 'statusCode' in obj && 'message' in obj
: false;
}
}
9 changes: 3 additions & 6 deletions src/common/message/services/message.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,7 @@ export class MessageService implements IMessageService {
return newErrors;
}

async get(
key: string,
options?: IMessageOptions
): Promise<string | IMessage> {
async get<T = string>(key: string, options?: IMessageOptions): Promise<T> {
const properties = options?.properties;
const customLanguages =
options?.customLanguages?.length > 0
Expand All @@ -130,9 +127,9 @@ export class MessageService implements IMessageService {
}

if (customLanguages.length <= 1) {
return messages[customLanguages[0]];
return messages[customLanguages[0]] as T;
}

return messages;
return messages as T;
}
}

This file was deleted.

Loading

0 comments on commit 83a0e4b

Please sign in to comment.