Skip to content

Commit 09397c8

Browse files
author
Jordan van Wijnen
committed
feat(errors): add error handling hooks and improve error logging
chore(package): add pump, husky, lint-staged, and prettier dependencies
1 parent 944c17e commit 09397c8

4 files changed

Lines changed: 36 additions & 1 deletion

File tree

demos/errors.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
'use strict'
2+
3+
const gateway = require('./../index')
4+
const PORT = process.env.PORT || 8080
5+
6+
const server = gateway({
7+
routes: [{
8+
prefix: '/*',
9+
prefixRewrite: '',
10+
target: 'http://127.0.0.1:3000',
11+
hooks: {
12+
onRequest: () => {
13+
throw new SyntaxError('Something went wrong')
14+
},
15+
onError(err, req) {
16+
// allows for extra error handling logic (i.e. Sentry, Newrelic etc.)
17+
console.warn(req.method, req.url, err)
18+
}
19+
}
20+
}]
21+
})
22+
23+
server.start(PORT).then(server => {
24+
console.log(`API Gateway listening on ${PORT} port!`)
25+
})

index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ declare namespace fastgateway {
5959
onRequest?: Function
6060
rewriteHeaders?: Function
6161
onResponse?: Function
62+
onError?: (error: Error, request: Request) => void
6263
rewriteRequestHeaders?: Function
6364
request?: {
6465
timeout?: number

index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,11 @@ const handler = (route, proxy, proxyHandler) => async (req, res, next) => {
173173
proxyHandler(req, res, req.url, proxy, proxyOpts)
174174
}
175175
} catch (err) {
176+
const { onError } = hooks
177+
if (typeof onError === 'function') {
178+
onError(err, req)
179+
}
180+
176181
return next(err)
177182
}
178183
}

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"fast-proxy-lite": "^1.1.2",
3232
"http-cache-middleware": "^1.4.1",
3333
"micromatch": "^4.0.8",
34+
"pump": "^3.0.4",
3435
"restana": "^5.0.0",
3536
"stream-to-array": "^2.3.0"
3637
},
@@ -42,8 +43,8 @@
4243
"LICENSE"
4344
],
4445
"devDependencies": {
45-
"@types/node": "^22.13.11",
4646
"@types/express": "^5.0.0",
47+
"@types/node": "^22.13.11",
4748
"artillery": "^2.0.21",
4849
"aws-sdk": "^2.1691.0",
4950
"chai": "^4.5.0",
@@ -56,10 +57,13 @@
5657
"fg-multiple-hooks": "^1.3.0",
5758
"helmet": "^7.2.0",
5859
"http-lambda-proxy": "^1.1.4",
60+
"husky": "^9.1.7",
61+
"lint-staged": "^17.0.2",
5962
"load-balancers": "^1.3.52",
6063
"mocha": "^10.8.2",
6164
"nyc": "^17.1.0",
6265
"pem": "^1.14.8",
66+
"prettier": "^3.8.3",
6367
"request-ip": "^3.3.0",
6468
"response-time": "^2.3.3",
6569
"supertest": "^7.0.0"

0 commit comments

Comments
 (0)