Skip to content
This repository
Browse code

default error handling for bounce streams and requests

  • Loading branch information...
commit e36877f41de9222373ecc8cdc8d8f88ec386d701 1 parent aa9e41b
James Halliday authored

Showing 3 changed files with 14 additions and 7 deletions. Show diff stats Hide diff stats

  1. +2 5 README.markdown
  2. +2 2 example/route.js
  3. +10 0 index.js
7 README.markdown
Source Rendered
@@ -24,14 +24,11 @@ var bouncy = require('bouncy');
24 24
25 25 bouncy(function (req, bounce) {
26 26 if (req.headers.host === 'beep.example.com') {
27   - bounce(8001).on('error', onerror);
  27 + bounce(8001);
28 28 }
29 29 else if (req.headers.host === 'boop.example.com') {
30   - bounce(8002).on('error', onerror);
  30 + bounce(8002);
31 31 }
32   -
33   - req.on('error', onerror);
34   - function onerror () { req.destroy() }
35 32 }).listen(8000);
36 33 ````
37 34
4 example/route.js
@@ -2,10 +2,10 @@ var bouncy = require('bouncy');
2 2
3 3 bouncy(function (req, bounce) {
4 4 if (req.headers.host === 'beep.example.com') {
5   - bounce(8001).on('error', onerror);
  5 + bounce(8001);
6 6 }
7 7 else if (req.headers.host === 'boop.example.com') {
8   - bounce(8002).on('error', onerror);
  8 + bounce(8002);
9 9 }
10 10
11 11 req.on('error', onerror);
10 index.js
@@ -49,6 +49,8 @@ var handler = bouncy.handler = function (cb, c) {
49 49
50 50 function onHeaders () {
51 51 req.removeListener('error', onError);
  52 + // don't kill the server on subsequent request errors
  53 + req.on('error', function () {});
52 54 var bounce = makeBounce(stream, c, req);
53 55 cb(req, bounce);
54 56 }
@@ -96,6 +98,14 @@ function makeBounce (bs, client, req) {
96 98 opts.emitter.emit('drop', client);
97 99 }
98 100
  101 + stream.on('error', function (err) {
  102 + if (stream.listeners('error').length === 1) {
  103 + // destroy the request and stream if nobody is listening
  104 + req.destroy();
  105 + stream.destroy();
  106 + }
  107 + });
  108 +
99 109 return stream;
100 110 };
101 111

0 comments on commit e36877f

Please sign in to comment.
Something went wrong with that request. Please try again.