Skip to content

Commit 097b1c4

Browse files
committed
Handle backend errors
1 parent dda9e0b commit 097b1c4

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
@@ -384,19 +384,23 @@ class ConfigurableProxy extends EventEmitter {
384384
}
385385

386386
targetForReq(req) {
387-
var timer = this.statsd.createTimer("find_target_for_req");
388-
// return proxy target for a given url path
389-
var basePath = this.hostRouting ? "/" + parseHost(req) : "";
390-
var path = basePath + decodeURIComponent(URL.parse(req.url).pathname);
391-
392-
return this._routes.getTarget(path).then(function (route) {
393-
timer.stop();
394-
if (route) {
395-
return {
396-
prefix: route.prefix,
397-
target: route.data.target,
398-
};
399-
}
387+
return new Promise((resolve, reject) => {
388+
var timer = this.statsd.createTimer("find_target_for_req");
389+
// return proxy target for a given url path
390+
var basePath = this.hostRouting ? "/" + parseHost(req) : "";
391+
var path = basePath + decodeURIComponent(URL.parse(req.url).pathname);
392+
393+
resolve(
394+
this._routes.getTarget(path).then(function (route) {
395+
timer.stop();
396+
if (route) {
397+
return {
398+
prefix: route.prefix,
399+
target: route.data.target,
400+
};
401+
}
402+
})
403+
).catch((e) => reject(e));
400404
});
401405
}
402406

@@ -598,6 +602,10 @@ class ConfigurableProxy extends EventEmitter {
598602
}
599603
});
600604
}
605+
})
606+
.catch(function (e) {
607+
if (res.finished) throw e;
608+
that.handleProxyError(500, kind, req, res, e);
601609
});
602610
}
603611

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)