From 4e3ff0f8bf5bc1524f54f71f56802f73e686fb00 Mon Sep 17 00:00:00 2001 From: James Bain Date: Mon, 7 Aug 2023 16:12:33 -0600 Subject: [PATCH 1/4] #92 feat: add optional callback to handle errors --- index.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/index.js b/index.js index ea60c34..c392b80 100644 --- a/index.js +++ b/index.js @@ -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 @@ -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) { From d44dbf8825e5dfd27683bbc8e64d2ce5438a9310 Mon Sep 17 00:00:00 2001 From: James Bain Date: Tue, 8 Aug 2023 08:21:50 -0600 Subject: [PATCH 2/4] #92 docs: document the onError option --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index a25742a..b84262f 100644 --- a/README.md +++ b/README.md @@ -152,6 +152,9 @@ Called when an http response is received from the source. The default behavior is `pump(stream, res)`, which will be disabled if the option is specified. +##### onError(err, req, res) +Called when an error is thrown from the source. If this option is specified, the default error handling will not be applied. + ##### rewriteRequestHeaders(req, headers) Called to rewrite the headers of the request, before them being sent to the downstream server. It must return the new headers object. From b296029f86f6750fb05ef5b8ac789561dc36c3a4 Mon Sep 17 00:00:00 2001 From: James Bain Date: Tue, 8 Aug 2023 13:52:18 -0600 Subject: [PATCH 3/4] #92 test: add test for onError option --- index.js | 1 - test/4.opts.test.js | 17 +++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index c392b80..d98caad 100644 --- a/index.js +++ b/index.js @@ -89,7 +89,6 @@ 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) diff --git a/test/4.opts.test.js b/test/4.opts.test.js index db67cda..c37f329 100644 --- a/test/4.opts.test.js +++ b/test/4.opts.test.js @@ -17,6 +17,10 @@ nock('http://dev.com') url: 'http://dev.com' }) +nock('http://dev.error') + .get('/service/error') + .replyWithError('Error') + describe('fast-proxy smoke', () => { it('init', async () => { const fastProxy = require('../index')({ @@ -40,6 +44,10 @@ describe('fast-proxy smoke', () => { queryString: { age: 33 }, onResponse (req, res, stream) { pump(stream, res) + }, + onError (err, req, res) { + res.statusCode = 418 + res.send(err) } }) }) @@ -99,6 +107,15 @@ describe('fast-proxy smoke', () => { .expect(302) }) + it('should allow for errors to be handled', async () => { + await request(gHttpServer) + .get('/service/error') + .set('base', 'http://dev.com') + .then(response => { + expect(response.statusCode).to.equal(418) + }) + }) + it('close all', async () => { close() await gateway.close() From 48b1a4fa74634766cb727c260d32984f750d7fcb Mon Sep 17 00:00:00 2001 From: James Bain Date: Tue, 8 Aug 2023 14:04:06 -0600 Subject: [PATCH 4/4] #92 fix: send error code in test --- test/4.opts.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/4.opts.test.js b/test/4.opts.test.js index c37f329..a113d18 100644 --- a/test/4.opts.test.js +++ b/test/4.opts.test.js @@ -47,7 +47,7 @@ describe('fast-proxy smoke', () => { }, onError (err, req, res) { res.statusCode = 418 - res.send(err) + res.send(err.code) } }) })