Skip to content

Commit

Permalink
fastify#92 feat: add optional callback to handle errors
Browse files Browse the repository at this point in the history
  • Loading branch information
jcbain committed Aug 7, 2023
1 parent 9ef68a1 commit 4e3ff0f
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ function fastProxy (opts = {}) {
const onResponse = opts.onResponse
const rewriteHeaders = opts.rewriteHeaders || rewriteHeadersNoOp
const rewriteRequestHeaders = opts.rewriteRequestHeaders || rewriteRequestHeadersNoOp
const onError = opts.onError

const url = getReqUrl(source || req.url, cache, base, opts)
const sourceHttp2 = req.httpVersionMajor === 2
Expand Down Expand Up @@ -88,6 +89,13 @@ function fastProxy (opts = {}) {
}
request(reqParams, (err, response) => {
if (err) {

// allow for errors to be passed to a custom callback
if (onError) {
onError(err, req, res)
return
}

// check if response has already been sent and all data has been flushed
// before configuring error response headers
if (res.sent === false || res.writableFinished === false) {
Expand Down

0 comments on commit 4e3ff0f

Please sign in to comment.