-
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.
[INV-3662] Create Error Handler, move middleware to named files, crea…
…te common Database caller (#3696) * create error handling middleware * Create commonDbRequest function to handle setup/teardown * Move bearer logic to separate file * move cors logic to separate file, create index for imports * refactor App.ts to use the imported functions * Remove common Db Request
- Loading branch information
1 parent
4a1d06e
commit 929f6e8
Showing
5 changed files
with
76 additions
and
58 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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { MDC, MDCAsyncLocal } from 'mdc'; | ||
import { authenticate, InvasivesRequest } from 'utils/auth-utils'; | ||
import { getLogger } from 'utils/logger'; | ||
|
||
const bearerHandler = async (req) => { | ||
const logger = getLogger('Error'); | ||
try { | ||
let mdc = MDCAsyncLocal.getStore(); | ||
if (!mdc) { | ||
mdc = new MDC(); | ||
await MDCAsyncLocal.run(mdc, authenticate, <InvasivesRequest>req); | ||
} else { | ||
await authenticate(<InvasivesRequest>req); | ||
} | ||
} catch (e) { | ||
logger.error({ error: e }); | ||
return false; | ||
} | ||
return true; | ||
}; | ||
export default bearerHandler; |
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,21 @@ | ||
import { MDC, MDCAsyncLocal } from 'mdc'; | ||
|
||
const cors = (_: any, res: any, next) => { | ||
res.setHeader('Access-Control-Allow-Credentials', true); | ||
res.setHeader( | ||
'Access-Control-Allow-Headers', | ||
'X-Requested-With, Content-Type, Authorization, responseType, Access-Control-Allow-Origin, If-None-Match, filterForSelectable' | ||
); | ||
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE, HEAD'); | ||
res.setHeader('Access-Control-Allow-Origin', '*'); | ||
|
||
// create a context if there isn't one | ||
let mdc = MDCAsyncLocal.getStore(); | ||
if (!mdc) { | ||
mdc = new MDC(); | ||
MDCAsyncLocal.run(mdc, next); | ||
} else { | ||
next(); | ||
} | ||
}; | ||
export default cors; |
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,27 @@ | ||
import { NextFunction, Request, Response } from 'express'; | ||
import { getLogger } from 'utils/logger'; | ||
|
||
export class CustomError extends Error { | ||
code: number; | ||
constructor(message?: string, code?: number) { | ||
super(message); | ||
this.code = code; | ||
} | ||
} | ||
const globalErrorHandler = (err: Error, req: Request, res: Response, next: NextFunction) => { | ||
const logger = getLogger('Error'); | ||
const code = err instanceof CustomError ? err.code : 500; | ||
const errorResponse = { | ||
req: req.body, | ||
error: err.message || 'Internal Server Error', | ||
method: req.method, | ||
namespace: req.url | ||
}; | ||
logger.error(errorResponse); | ||
if (!res.headersSent) { | ||
res.status(code).json(errorResponse); | ||
} | ||
next(); | ||
}; | ||
|
||
export default globalErrorHandler; |
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,3 @@ | ||
export { default as globalErrorHandler } from './globalErrorHandler'; | ||
export { default as bearerHandler } from './bearerHandler'; | ||
export { default as cors } from './cors'; |