-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
General error handling for express routes
- Loading branch information
Showing
5 changed files
with
34 additions
and
12 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
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,26 @@ | ||
/* eslint-disable no-param-reassign */ | ||
import { Application } from 'express'; | ||
|
||
const wrapHandler = (originalHandler: any) => async (req: any, res: any, next: any) => { | ||
try { | ||
await originalHandler(req, res, next); | ||
} catch (err) { | ||
next(err); | ||
} | ||
}; | ||
|
||
const routesErrorHandler = (app: Application) => { | ||
const originalGet = app.get.bind(app); | ||
app.get = (path: any, ...args: any[]) => originalGet(path, ...args.map(wrapHandler)); | ||
|
||
const originalPost = app.post.bind(app); | ||
app.post = (path: any, ...args: any[]) => originalPost(path, ...args.map(wrapHandler)); | ||
|
||
const originalDelete = app.delete.bind(app); | ||
app.delete = (path: any, ...args: any[]) => originalDelete(path, ...args.map(wrapHandler)); | ||
|
||
const originalPut = app.put.bind(app); | ||
app.put = (path: any, ...args: any[]) => originalPut(path, ...args.map(wrapHandler)); | ||
}; | ||
|
||
export { routesErrorHandler }; |
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