Skip to content

Commit bd0cca3

Browse files
molty3000molty3000
andauthored
fix(errors): add res param to onError hook and guard against internal throw (#102)
* fix(errors): add res param to onError hook and guard against internal throw - onError(err, req, res): response object now available for observability - Wrap onError call in try/catch — hook failure must not suppress original error - Type definition updated: onError?: (error: Error, request: Request, response: Response) => void Closes #101 * fix(types): use Node http.IncomingMessage/ServerResponse for onError params restana.Request/Response are generic types requiring Protocol<P> — incompatible with the Hooks interface which is not parameterized. Node's native types are the correct base types (restana extends them) and require no type arguments. * fix(onError): use Function type, await async hooks, add 4 tests - Type: use Function for onError (consistent with all other hooks in Hooks interface) - Async: await onError call so async hooks that reject are caught by try/catch - Tests: 4 new tests covering sync, async, throwing, and no-error paths - All 4 new tests pass (54 total, 1 pre-existing failure unchanged) * docs: add onError hook to lifecycle hooks documentation * fix(test): update on-request-error assertion for restana v5.2.0 restana v5.2.0 sanitizes error messages — returns 'Internal Server Error' instead of the thrown message. The test contract is '500 is returned', not the specific error string. --------- Co-authored-by: molty3000 <molty@21no.de>
1 parent 944c17e commit bd0cca3

6 files changed

Lines changed: 11934 additions & 3 deletions

File tree

docs/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,11 @@ module.exports.handler = serverless(service)
201201
onResponse (req, res, stream) {
202202
// do some post-processing here
203203
// ...
204+
},
205+
onError (err, req, res) {
206+
// called when the request pipeline throws (e.g., onRequest error, proxy failure)
207+
// hook failures are silently caught — the original error still propagates to the client
208+
// can be sync or async
204209
}
205210

206211
// if proxyType= 'http', other options allowed https://www.npmjs.com/package/fast-proxy-lite#opts

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?: Function
6263
rewriteRequestHeaders?: Function
6364
request?: {
6465
timeout?: number

index.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,15 @@ 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+
try {
179+
await onError(err, req, res)
180+
} catch (_) {
181+
// onError hook failure must not suppress the original error
182+
}
183+
}
184+
176185
return next(err)
177186
}
178187
}

0 commit comments

Comments
 (0)