-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from poojakarma/template_api_changes_for_push_…
…notification PS - 2393 Template api changes for push notification
- Loading branch information
Showing
22 changed files
with
512 additions
and
515 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,72 @@ | ||
import { ExceptionFilter, Catch, ArgumentsHost, HttpException } from '@nestjs/common'; | ||
import { | ||
ExceptionFilter, | ||
Catch, | ||
ArgumentsHost, | ||
HttpException, | ||
HttpStatus, | ||
} from '@nestjs/common'; | ||
import { QueryFailedError } from 'typeorm'; | ||
import { Response, Request } from 'express'; | ||
import APIResponse from '../utils/response'; | ||
import { ERROR_MESSAGES } from '../utils/constant.util'; | ||
import { LoggerUtil } from '../logger/LoggerUtil'; | ||
|
||
@Catch() | ||
export class AllExceptionsFilter implements ExceptionFilter { | ||
constructor(private readonly apiId?: string) { } | ||
|
||
catch(exception: unknown, host: ArgumentsHost) { | ||
catch( | ||
exception: Error | HttpException | QueryFailedError, | ||
host: ArgumentsHost, | ||
) { | ||
const ctx = host.switchToHttp(); | ||
const response = ctx.getResponse<Response>(); | ||
const status = exception instanceof HttpException ? exception.getStatus() : 500; | ||
const exceptionResponse = exception instanceof HttpException ? exception.getResponse() : null; | ||
const errorMessage = | ||
exception instanceof HttpException | ||
? (exceptionResponse as any).message || exception.message | ||
: 'Internal server error'; | ||
const request = ctx.getRequest<Request>(); | ||
const userId = request?.query?.userid; | ||
const status = | ||
exception instanceof HttpException ? exception.getStatus() : 500; | ||
|
||
let errorMessage = | ||
exception?.message || ERROR_MESSAGES.INTERNAL_SERVER_ERROR; | ||
|
||
LoggerUtil.error(`Error occurred on API: ${request.url} Method : ${request.method}`, | ||
errorMessage, 'Notification Service', `requested by userId: ${userId}`, | ||
) | ||
|
||
if (exception instanceof HttpException) { | ||
const statusCode = exception.getStatus(); | ||
const errorResponse = APIResponse.error( | ||
this.apiId, | ||
(exception.getResponse() as any)?.message, | ||
ERROR_MESSAGES.BAD_REQUEST, | ||
statusCode.toString(), | ||
); | ||
return response.status(statusCode).json(errorResponse); | ||
} else if (exception instanceof QueryFailedError) { | ||
const statusCode = HttpStatus.UNPROCESSABLE_ENTITY; | ||
const errorResponse = APIResponse.error( | ||
this.apiId, | ||
(exception as QueryFailedError).message, | ||
ERROR_MESSAGES.INTERNAL_SERVER_ERROR, | ||
statusCode.toString(), | ||
); | ||
LoggerUtil.error(`Database Query Failed on API: ${request.url} requested by userId: ${userId}`, | ||
request.method, | ||
(exception as QueryFailedError).message, | ||
'QueryFailedError') | ||
|
||
return response.status(statusCode).json(errorResponse); | ||
} | ||
const detailedErrorMessage = `${errorMessage}`; | ||
APIResponse.error( | ||
response, | ||
|
||
const errorResponse = APIResponse.error( | ||
this.apiId, | ||
detailedErrorMessage, | ||
exception instanceof HttpException ? exception.name : 'InternalServerError', // error | ||
status | ||
exception instanceof HttpException | ||
? exception.name | ||
: ERROR_MESSAGES.INTERNAL_SERVER_ERROR, | ||
status.toString(), | ||
); | ||
return response.status(status).json(errorResponse); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import * as winston from 'winston'; | ||
|
||
export class LoggerUtil { | ||
private static logger: winston.Logger; | ||
|
||
static getLogger() { | ||
if (!this.logger) { | ||
const customFormat = winston.format.printf( | ||
({ timestamp, level, message, context, user, error }) => { | ||
return JSON.stringify({ | ||
timestamp: timestamp, | ||
context: context, | ||
user: user, | ||
level: level, | ||
message: message, | ||
error: error, | ||
}); | ||
}, | ||
); | ||
|
||
this.logger = winston.createLogger({ | ||
level: 'info', | ||
format: winston.format.combine(winston.format.timestamp(), customFormat), | ||
transports: [ | ||
new winston.transports.Console(), | ||
new winston.transports.File({ filename: 'error.log', level: 'error' }), | ||
new winston.transports.File({ filename: 'combined.log' }), | ||
], | ||
}); | ||
} | ||
return this.logger; | ||
} | ||
static log( | ||
message: string, | ||
context?: string, | ||
user?: string, | ||
level: string = 'info', | ||
) { | ||
this.getLogger().log({ | ||
level: level, | ||
message: message, | ||
context: context, | ||
user: user, | ||
timestamp: new Date().toISOString(), | ||
}); | ||
} | ||
|
||
static error( | ||
message: string, | ||
error?: string, | ||
context?: string, | ||
user?: string, | ||
) { | ||
this.getLogger().error({ | ||
message: message, | ||
error: error, | ||
context: context, | ||
user: user, | ||
timestamp: new Date().toISOString(), | ||
}); | ||
} | ||
|
||
static warn(message: string, context?: string) { | ||
this.getLogger().warn({ | ||
message: message, | ||
context: context, | ||
timestamp: new Date().toISOString(), | ||
}); | ||
} | ||
|
||
static debug(message: string, context?: string) { | ||
this.getLogger().debug({ | ||
message: message, | ||
context: context, | ||
timestamp: new Date().toISOString(), | ||
}); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.