diff --git a/lib/framework.js b/lib/framework.js index 575ca1ab..1604b156 100644 --- a/lib/framework.js +++ b/lib/framework.js @@ -503,8 +503,10 @@ class Framework { changeOrigin: true, agent }); - - return (req, res, next) => proxy.web(req, res, next); + proxy.on("error", (err, req /* , res*/) => { + this.logger.warn(`Failed to proxy ${req.url} (${err.code}: ${err.message})`); + }); + return (req, res) => proxy.web(req, res); } beforeMiddlewareRewriteUrl(req, res, next) { diff --git a/test/unit/__mocks__/http-proxy.js b/test/unit/__mocks__/http-proxy.js index ef163b56..97e457ff 100644 --- a/test/unit/__mocks__/http-proxy.js +++ b/test/unit/__mocks__/http-proxy.js @@ -1,9 +1,8 @@ const httpProxy = jest.genMockFromModule("http-proxy"); httpProxy.createProxyServer = jest.fn(function() { return { - web: jest.fn(function(req, res, next) { - next(); - }) + web: jest.fn(), + on: jest.fn() }; }); module.exports = httpProxy; diff --git a/test/unit/framework.test.js b/test/unit/framework.test.js index 75ff8f45..24bd4035 100644 --- a/test/unit/framework.test.js +++ b/test/unit/framework.test.js @@ -88,7 +88,7 @@ describe("Middleware for UI5", () => { }); describe("Proxy for UI5 ", () => { - it("Should call proxy module from middleware (http)", (done) => { + it("Should call proxy module from middleware (http)", () => { const proxyServer = new Framework().setupProxy({ url: "http://localhost" }); @@ -105,18 +105,18 @@ describe("Proxy for UI5 ", () => { }) }); - // const proxy = require("http-proxy").createProxyServer.mock.results[0].value; + const proxy = createProxyServer.mock.results[createProxyServer.mock.results.length - 1].value; + + expect(proxy.on).toBeCalledWith("error", expect.any(Function)); const req = {}; const res = {}; - const next = function() { - // expect(proxy.web).toBeCalledWith(req, res, next); // TODO: check why this fails - done(); - }; - proxyServer(req, res, next); + proxyServer(req, res); + + expect(proxy.web).toBeCalledWith(req, res); }); - it("Should call proxy module from middleware (https)", (done) => { + it("Should call proxy module from middleware (https)", () => { const proxyServer = new Framework().setupProxy({ url: "https://localhost" }); @@ -133,15 +133,15 @@ describe("Proxy for UI5 ", () => { }) }); - // const proxy = require("http-proxy").createProxyServer.mock.results[0].value; + const proxy = createProxyServer.mock.results[createProxyServer.mock.results.length - 1].value; + + expect(proxy.on).toBeCalledWith("error", expect.any(Function)); const req = {}; const res = {}; - const next = function() { - // expect(proxy.web).toBeCalledWith(req, res, next); // TODO: check why this fails - done(); - }; - proxyServer(req, res, next); + proxyServer(req, res); + + expect(proxy.web).toBeCalledWith(req, res); }); });