Skip to content

Commit

Permalink
[FIX] Add error logging for proxy requests (#254)
Browse files Browse the repository at this point in the history
  • Loading branch information
matz3 committed Oct 13, 2020
1 parent 5a22f4b commit e79e77d
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 19 deletions.
6 changes: 4 additions & 2 deletions lib/framework.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
5 changes: 2 additions & 3 deletions test/unit/__mocks__/http-proxy.js
Original file line number Diff line number Diff line change
@@ -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;
28 changes: 14 additions & 14 deletions test/unit/framework.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"
});
Expand All @@ -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"
});
Expand All @@ -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);
});
});

Expand Down

0 comments on commit e79e77d

Please sign in to comment.