Skip to content

Commit 9ab8749

Browse files
committed
[feature] started working on error propagation, kinda sucks, gotta think it over
1 parent bd3df45 commit 9ab8749

File tree

4 files changed

+36
-15
lines changed

4 files changed

+36
-15
lines changed

lib/caronte/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ function createRightProxy(type) {
3939
var event = ev + pass.name.toLowerCase();
4040

4141
self.emit(event + 'begin', req, res);
42-
pass(req, res, options);
42+
pass(req, res, options, self);
4343
self.emit(event + 'end');
4444
});
4545

lib/caronte/passes/web.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -79,17 +79,18 @@ function XHeaders(req, res, options) {
7979
* @param {ClientRequest} Req Request object
8080
* @param {IncomingMessage} Res Response object
8181
* @param {Object} Options Config object passed to the proxy
82+
* @param {Object} Instance Proxy object that emits events
8283
*
8384
* @api private
8485
*/
8586

86-
function stream(req, res, options) {
87+
function stream(req, res, options, instance) {
8788
if(options.forward) {
88-
req.pipe(new ForwardStream(options.forward));
89+
req.pipe(new ForwardStream(options, instance));
8990
}
9091

9192
if(options.target) {
92-
return req.pipe(new ProxyStream(res, options)).pipe(res);
93+
return req.pipe(new ProxyStream(options, res, instance)).pipe(res);
9394
}
9495

9596
res.end();

lib/caronte/streams/forward.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,12 @@ module.exports = ForwardStream;
2020
* @api private
2121
*/
2222

23-
function ForwardStream() {
23+
function ForwardStream(options) {
2424
var self = this;
2525

26+
self.options = options;
27+
self.res = res;
28+
2629
Writable.call(this);
2730

2831
this.once('pipe', function(pipe) { self.onPipe(pipe) });
@@ -48,10 +51,12 @@ ForwardStream.prototype.onPipe = function(request) {
4851
this.forwardReq = (options.ssl ? https : http).request(
4952
common.setupOutgoing(options.ssl || {}, options, request)
5053
);
54+
55+
this.forwardReq.on('error', function() {}); /** Fire and forget */
5156
};
5257

5358
/**
54-
* Closes forwarded request when `pipe` is finished
59+
* Closes forwarded request when `pipe` is finished.
5560
*
5661
* Examples:
5762
*

lib/caronte/streams/proxy.js

+24-9
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ var Duplex = require('stream').Duplex,
33
http = require('http'),
44
https = require('https');
55

6-
function ProxyStream() {
6+
function ProxyStream(options, res, instance) {
7+
this.options = options;
8+
this.res = res;
9+
this.instance = instance;
10+
711
var self = this;
812

913
Duplex.call(this);
@@ -14,26 +18,37 @@ function ProxyStream() {
1418

1519
require('util').inherits(ProxyStream, Duplex);
1620

17-
ProxyStream.prototype.onPipe = function(request) {
21+
ProxyStream.prototype.onPipe = function(req) {
22+
this.req = req;
23+
1824
var self = this;
1925

2026
this.proxyReq = (options.ssl ? https : http).request(
21-
common.setupOutgoing(options.ssl || {}, options, request)
27+
common.setupOutgoing(options.ssl || {}, options, req)
2228
);
2329

24-
this.proxyReq.once('response', function(response) {
25-
self.onResponse(response);
30+
this.proxyReq.once('response', function(proxyRes) {
31+
self.onResponse(proxyRes);
2632
});
27-
this.proxyReq.on('error', function() {}); // XXX TODO: add error handling
28-
}
33+
this.proxyReq.on('error', function(e) {
34+
self.onError(e);
35+
});
36+
};
2937

3038
ProxyStream.prototype.onFinish = function() {
3139

32-
}
40+
};
3341

3442
ProxyStream.prototype.onResponse = function(proxyRes) {
3543
this.proxyRes = proxyRes;
36-
}
44+
};
45+
46+
ProxyStream.prototype.onError = function(e) {
47+
if(this.instance.emit('error', this.req, this.res, e)) return;
48+
49+
this.res.writeHead(500, { 'Content-Type': 'text/plain' });
50+
this.res.end('Internal Server Error');
51+
};
3752

3853
ProxyStream.prototype._write = function(chunk, encoding, callback) {
3954
this.proxyReq.write(chunk, encoding, callback);

0 commit comments

Comments
 (0)