Skip to content

Commit c6f7cea

Browse files
committed
Handle backend errors
1 parent f22c7bd commit c6f7cea

File tree

2 files changed

+35
-13
lines changed

2 files changed

+35
-13
lines changed

lib/configproxy.js

+21-13
Original file line numberDiff line numberDiff line change
@@ -379,19 +379,23 @@ class ConfigurableProxy extends EventEmitter {
379379
}
380380

381381
targetForReq(req) {
382-
var metricsTimerEnd = this.metrics.findTargetForReqSummary.startTimer();
383-
// return proxy target for a given url path
384-
var basePath = this.hostRouting ? "/" + parseHost(req) : "";
385-
var path = basePath + decodeURIComponent(URL.parse(req.url).pathname);
386-
387-
return this._routes.getTarget(path).then(function (route) {
388-
metricsTimerEnd();
389-
if (route) {
390-
return {
391-
prefix: route.prefix,
392-
target: route.data.target,
393-
};
394-
}
382+
return new Promise((resolve, reject) => {
383+
var metricsTimerEnd = this.metrics.findTargetForReqSummary.startTimer();
384+
// return proxy target for a given url path
385+
var basePath = this.hostRouting ? "/" + parseHost(req) : "";
386+
var path = basePath + decodeURIComponent(URL.parse(req.url).pathname);
387+
388+
resolve(
389+
this._routes.getTarget(path).then(function (route) {
390+
metricsTimerEnd();
391+
if (route) {
392+
return {
393+
prefix: route.prefix,
394+
target: route.data.target,
395+
};
396+
}
397+
})
398+
).catch((e) => reject(e));
395399
});
396400
}
397401

@@ -593,6 +597,10 @@ class ConfigurableProxy extends EventEmitter {
593597
}
594598
});
595599
}
600+
})
601+
.catch(function (e) {
602+
if (res.finished) throw e;
603+
that.handleProxyError(500, kind, req, res, e);
596604
});
597605
}
598606

test/proxy_spec.js

+14
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,20 @@ describe("Proxy Tests", function () {
379379
.then(done);
380380
});
381381

382+
it("backend error", function (done) {
383+
var proxyPort = 55550;
384+
util
385+
.setupProxy(proxyPort, { errorTarget: "http://127.0.0.1:55565" }, [])
386+
.then(() => r("http://127.0.0.1:" + proxyPort + "/%"))
387+
.then((body) => done.fail("Expected 500"))
388+
.catch((err) => {
389+
expect(err.statusCode).toEqual(500);
390+
expect(err.response.headers["content-type"]).toEqual("text/plain");
391+
expect(err.response.body).toEqual("/%");
392+
})
393+
.then(done);
394+
});
395+
382396
it("Redirect location untouched without rewrite options", function (done) {
383397
var redirectTo = "http://foo.com:12345/whatever";
384398
util

0 commit comments

Comments
 (0)