-
-
Notifications
You must be signed in to change notification settings - Fork 82
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 #614 from santoshshinde2012/dev
fixed snyk vulnerability
- Loading branch information
Showing
3 changed files
with
94 additions
and
106 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
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,43 +1,33 @@ | ||
import util from 'util'; | ||
import { Request, Response, NextFunction } from 'express'; | ||
import * as util from 'util'; | ||
import * as express from 'express'; | ||
import { StatusCodes } from 'http-status-codes'; | ||
import ApiError from '../abstractions/ApiError'; | ||
import ApiError, { IError } from '../abstractions/ApiError'; | ||
import logger from '../lib/logger'; | ||
import { getEncryptedText } from '../utils'; | ||
|
||
|
||
const addErrorHandler = ( | ||
err: ApiError | null, | ||
req: Request, | ||
res: Response, | ||
next: NextFunction, | ||
err: ApiError, | ||
req: express.Request, | ||
res: express.Response, | ||
next: express.NextFunction, | ||
): void => { | ||
if (err) { | ||
const status = err.status || StatusCodes.INTERNAL_SERVER_ERROR; | ||
const errorMessage = err.message || 'An error occurred during the request.'; | ||
const errorDetails = { | ||
const status: number = err.status || StatusCodes.INTERNAL_SERVER_ERROR; | ||
logger.debug(`REQUEST HANDLING ERROR: | ||
\nERROR:\n${JSON.stringify(err)} | ||
\nREQUEST HEADERS:\n${util.inspect(req.headers)} | ||
\nREQUEST PARAMS:\n${util.inspect(req.params)} | ||
\nREQUEST QUERY:\n${util.inspect(req.query)} | ||
\nBODY:\n${util.inspect(req.body)}`); | ||
const body: IError | string = { | ||
fields: err.fields, | ||
message: errorMessage, | ||
message: err.message || 'An error occurred during the request.', | ||
name: err.name, | ||
status, | ||
}; | ||
|
||
// Logging error details | ||
logger.error(`REQUEST HANDLING ERROR: | ||
\nERROR:\n${JSON.stringify(err)} | ||
\nREQUEST HEADERS:\n${util.inspect(req.headers)} | ||
\nREQUEST PARAMS:\n${util.inspect(req.params)} | ||
\nREQUEST QUERY:\n${util.inspect(req.query)} | ||
\nBODY:\n${util.inspect(req.body)}`); | ||
|
||
// Encrypting error details if encryption is enabled | ||
const body = getEncryptedText(errorDetails); | ||
|
||
res.status(status).send(body); | ||
} else { | ||
next(); | ||
res.status(status); | ||
res.send(body); | ||
} | ||
next(); | ||
}; | ||
|
||
|
||
export default addErrorHandler; |
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,92 +1,90 @@ | ||
import { StatusCodes } from 'http-status-codes'; | ||
import { Request, Response, NextFunction } from 'express'; | ||
import 'jest'; | ||
import { NextFunction, Request, Response } from 'express'; | ||
import addErrorHandler from '../../../src/middleware/error-handler'; | ||
import logger from '../../../src/lib/logger'; | ||
import { getEncryptedText } from '../../../src/utils'; | ||
import ApiError from '../../../src/abstractions/ApiError'; | ||
|
||
jest.mock('../../../src/lib/logger', () => ({ | ||
error: jest.fn(), | ||
})); | ||
|
||
jest.mock('../../../src/utils', () => ({ | ||
getEncryptedText: jest.fn((errorDetails) => JSON.stringify(errorDetails)), | ||
})); | ||
import { StatusCodes } from 'http-status-codes'; | ||
|
||
describe('addErrorHandler', () => { | ||
let req: Partial<Request>; | ||
let res: Partial<Response>; | ||
let next: jest.Mock<NextFunction, []>; | ||
describe('ErrorHandler middleware', () => { | ||
let mockRequest: Partial<Request>; | ||
let mockResponse: Partial<Response>; | ||
let nextFunction: NextFunction = jest.fn(); | ||
|
||
beforeEach(() => { | ||
req = { | ||
headers: {}, | ||
params: {}, | ||
query: {}, | ||
body: {}, | ||
}; | ||
res = { | ||
status: jest.fn().mockReturnThis(), | ||
send: jest.fn(), | ||
mockRequest = {}; | ||
mockResponse = { | ||
status : jest.fn(), | ||
send: jest.fn() | ||
}; | ||
next = jest.fn(); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
test('with 0 status code', async () => { | ||
const status: number = StatusCodes.INTERNAL_SERVER_ERROR; | ||
addErrorHandler({ | ||
status: 0, | ||
success: false, | ||
fields: { | ||
name: { | ||
message: '' | ||
} | ||
}, | ||
name: '', | ||
message: '' | ||
}, mockRequest as Request, mockResponse as Response, nextFunction); | ||
|
||
it('should send encrypted error response', () => { | ||
const err = new Error('Test Error') as ApiError; | ||
err.status = StatusCodes.BAD_REQUEST; | ||
expect(mockResponse.status).toHaveBeenCalledWith(status); | ||
}); | ||
|
||
addErrorHandler(err, req as Request, res as Response, next); | ||
test('with 200 status code', async () => { | ||
const status: number = 200; | ||
addErrorHandler({ | ||
status, | ||
success: false, | ||
fields: { | ||
name: { | ||
message: '' | ||
} | ||
}, | ||
name: '', | ||
message: '' | ||
}, mockRequest as Request, mockResponse as Response, nextFunction); | ||
|
||
expect(logger.error).toHaveBeenCalled(); | ||
expect(res.status).toHaveBeenCalledWith(StatusCodes.BAD_REQUEST); | ||
expect(getEncryptedText).toHaveBeenCalledWith({ | ||
fields: undefined, | ||
message: 'Test Error', | ||
name: 'Error', | ||
status: StatusCodes.BAD_REQUEST, | ||
}); | ||
expect(res.send).toHaveBeenCalledWith(JSON.stringify({ | ||
fields: undefined, | ||
message: 'Test Error', | ||
name: 'Error', | ||
status: StatusCodes.BAD_REQUEST, | ||
})); | ||
expect(next).not.toHaveBeenCalled(); | ||
expect(mockResponse.status).toHaveBeenCalledWith(status); | ||
}); | ||
|
||
it('should call next if no error', () => { | ||
addErrorHandler(null, req as Request, res as Response, next); | ||
|
||
expect(logger.error).not.toHaveBeenCalled(); | ||
expect(res.status).not.toHaveBeenCalled(); | ||
expect(res.send).not.toHaveBeenCalled(); | ||
expect(next).toHaveBeenCalled(); | ||
}); | ||
|
||
test('with 200 status code', async () => { | ||
const status: number = 200; | ||
addErrorHandler({ | ||
status, | ||
success: false, | ||
fields: { | ||
name: { | ||
message: '' | ||
} | ||
}, | ||
name: '', | ||
message: '' | ||
}, mockRequest as Request, mockResponse as Response, nextFunction); | ||
|
||
it('should handle error without status', () => { | ||
const err = new Error('Test Error') as ApiError; | ||
expect(mockResponse.status).toHaveBeenCalledWith(status); | ||
}); | ||
|
||
addErrorHandler(err, req as Request, res as Response, next); | ||
test('with 200 status code and updated env variables', async () => { | ||
process.env.APPLY_ENCRYPTION = 'true'; | ||
process.env.SECRET_KEY = 'key'; | ||
const status: number = 200; | ||
addErrorHandler({ | ||
status, | ||
success: false, | ||
fields: { | ||
name: { | ||
message: '' | ||
} | ||
}, | ||
name: '', | ||
message: '' | ||
}, mockRequest as Request, mockResponse as Response, nextFunction); | ||
|
||
expect(logger.error).toHaveBeenCalled(); | ||
expect(res.status).toHaveBeenCalledWith(StatusCodes.INTERNAL_SERVER_ERROR); | ||
expect(getEncryptedText).toHaveBeenCalledWith({ | ||
fields: undefined, | ||
message: 'Test Error', | ||
name: 'Error', | ||
status: StatusCodes.INTERNAL_SERVER_ERROR, | ||
}); | ||
expect(res.send).toHaveBeenCalledWith(JSON.stringify({ | ||
fields: undefined, | ||
message: 'Test Error', | ||
name: 'Error', | ||
status: StatusCodes.INTERNAL_SERVER_ERROR, | ||
})); | ||
expect(next).not.toHaveBeenCalled(); | ||
expect(mockResponse.status).toHaveBeenCalledWith(status); | ||
}); | ||
}); | ||
|
||
}) |