From 1eb6a85d3993fb5e40a6f3fe0716286b40c609c6 Mon Sep 17 00:00:00 2001 From: chimurai <655241+chimurai@users.noreply.github.com> Date: Tue, 6 Apr 2021 21:14:39 +0200 Subject: [PATCH] fix(error handler): re-throw http-proxy missing target error --- src/handlers.ts | 5 +++++ test/unit/handlers.spec.ts | 7 +++++++ 2 files changed, 12 insertions(+) diff --git a/src/handlers.ts b/src/handlers.ts index 7e2b0e3f..e44e5dc7 100644 --- a/src/handlers.ts +++ b/src/handlers.ts @@ -54,6 +54,11 @@ export function getHandlers(options: Options) { } function defaultErrorHandler(err, req: express.Request, res: express.Response) { + // Re-throw error. Not recoverable since req & res are empty. + if (!req && !res) { + throw err; // "Error: Must provide a proper URL as target" + } + const host = req.headers && req.headers.host; const code = err.code; diff --git a/test/unit/handlers.spec.ts b/test/unit/handlers.spec.ts index 792b7333..55789270 100644 --- a/test/unit/handlers.spec.ts +++ b/test/unit/handlers.spec.ts @@ -133,4 +133,11 @@ describe('default proxy error handler', () => { proxyError(mockError, mockReq, mockRes, proxyOptions); expect(errorMessage).toBe('Error occured while trying to proxy: localhost:3000/api'); }); + + it('should re-throw error from http-proxy when target is missing', () => { + mockRes.headersSent = true; + const error = new Error('Must provide a proper URL as target'); + const fn = () => proxyError(error, undefined, undefined, proxyOptions); + expect(fn).toThrowError(error); + }); });