-
-
Notifications
You must be signed in to change notification settings - Fork 37
feat(errors): Added error handling hook for observability #101
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| 'use strict' | ||
|
|
||
| const gateway = require('./../index') | ||
| const PORT = process.env.PORT || 8080 | ||
|
|
||
| const server = gateway({ | ||
| routes: [{ | ||
| prefix: '/*', | ||
| prefixRewrite: '', | ||
| target: 'http://127.0.0.1:3000', | ||
| hooks: { | ||
| onRequest: () => { | ||
| throw new SyntaxError('Something went wrong') | ||
| }, | ||
| onError(err, req) { | ||
| // allows for extra error handling logic (i.e. Sentry, Newrelic etc.) | ||
| console.warn(req.method, req.url, err) | ||
| } | ||
| } | ||
| }] | ||
| }) | ||
|
|
||
| server.start(PORT).then(server => { | ||
| console.log(`API Gateway listening on ${PORT} port!`) | ||
| }) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -59,6 +59,7 @@ declare namespace fastgateway { | |
| onRequest?: Function | ||
| rewriteHeaders?: Function | ||
| onResponse?: Function | ||
| onError?: (error: Error, request: Request) => void | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Issue: Wrong TypeScript type for Either use |
||
| rewriteRequestHeaders?: Function | ||
| request?: { | ||
| timeout?: number | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -173,6 +173,11 @@ const handler = (route, proxy, proxyHandler) => async (req, res, next) => { | |
| proxyHandler(req, res, req.url, proxy, proxyOpts) | ||
| } | ||
| } catch (err) { | ||
| const { onError } = hooks | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Issue: Return value ignored — The hook is fire-and-forget. Users who want to handle the error themselves (return a custom response, log and suppress, etc.) have no way to signal that. Consider: if |
||
| if (typeof onError === 'function') { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Issue: No Consider adding |
||
| onError(err, req) | ||
| } | ||
|
|
||
| return next(err) | ||
| } | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -31,6 +31,7 @@ | |
| "fast-proxy-lite": "^1.1.2", | ||
| "http-cache-middleware": "^1.4.1", | ||
| "micromatch": "^4.0.8", | ||
| "pump": "^3.0.4", | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Scope creep — Adding |
||
| "restana": "^5.0.0", | ||
| "stream-to-array": "^2.3.0" | ||
| }, | ||
|
|
@@ -42,8 +43,8 @@ | |
| "LICENSE" | ||
| ], | ||
| "devDependencies": { | ||
| "@types/node": "^22.13.11", | ||
| "@types/express": "^5.0.0", | ||
| "@types/node": "^22.13.11", | ||
| "artillery": "^2.0.21", | ||
| "aws-sdk": "^2.1691.0", | ||
| "chai": "^4.5.0", | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor — Missing newline at end of file (
\ No newline at end of filein the diff). POSIX convention.