title: How to send a response inside the domain error handler
body: <p>I have something similar to this:</p>
  <pre><code>var domain = require (&quot;domain&quot;);
  var http = require (&quot;http&quot;);
  
  var d = domain.create ();
  d.on (&quot;error&quot;, function (error){
      console.error (error);
      //res.writeHead (500)
      //res.end ()
  });
  d.run (function (){
      http.createServer (function (req, res){
          null.error
      }).listen (1337, &quot;localhost&quot;);
  });
  </code></pre>
  <p>All my server runs inside a domain. If an error occurs during a request I want to send to the user a 500 error but inside the error handler I can't access to the response object.</p>
  <hr />
  <p>My first attempt is to use an explicit domain for each request as the docs says:</p>
  <pre><code>var d = domain.create ();
  d.on (&quot;error&quot;, function (error){
      //res.writeHead (500);
      //res.end ()
      console.error (error);
  });
  d.run (function (){
      http.createServer (function (req, res){
          var dreq = domain.create ();
          dreq.add (req)
          dreq.add (res)
          dreq.on (&quot;error&quot;, function (error){
              res.writeHead (500);
              res.end ()
              console.error (error);
          });
          null.error
      }).listen (1337, &quot;localhost&quot;);
  });
  </code></pre>
  <p>The <code>dreq</code> error handler is never called because <code>req</code> and <code>res</code> never emit, the error occurs in execution-time, it's a TypeError.</p>
  <hr />
  <p>My second attempt is to save a reference to the <code>res</code> object using a closure, for example an event emitter:</p>
  <pre><code>var domain = require (&quot;domain&quot;);
  var http = require (&quot;http&quot;);
  var events = require (&quot;events&quot;);
  
  var emitter = new events.EventEmitter ();
  
  var d = domain.create ();
  d.on (&quot;error&quot;, function (error){
      emitter.emit (&quot;error&quot;, error);
  });
  d.run (function (){
      http.createServer (function (req, res){
          emitter.once (&quot;error&quot;, function (error){
              res.writeHead (500);
              res.end ();
              console.error (error);
          });
          null.error
      }).listen (1337, &quot;localhost&quot;);
  });
  </code></pre>
  <p>This works, the error is catched in the error handler and then emitted so I can use the response object to send a message to the user. If you have a very little knowledge on the node.js basics you'll see that this workaround is completely bugged. If there are 5 user requests on the event loop and 1 of them does a task that throws an error, the emitter will execute all the error handlers and the 5 users wil be notified with a 500 error when only 1 of them should receive a 500 error.</p>
  <hr />
  <p>My last attempt is to create sub-contexts for each request. The docs says nothing about this pattern. This is a bit confusing to me:</p>
  <blockquote>
  <p>In order to prevent excessive memory usage, Domain objects themselves are not implicitly added as children of the active domain. If they were, then it would be too easy to prevent request and response objects from being properly garbage collected.</p>
  <p>If you want to nest Domain objects as children of a parent Domain, then you must explicitly add them, and then dispose of them later.</p>
  </blockquote>
  <pre><code>var domain = require (&quot;domain&quot;);
  var http = require (&quot;http&quot;);
  
  var d = domain.create ();
  d.on (&quot;error&quot;, function (error){
      console.error (error);
  });
  d.run (function (){
      http.createServer (function (req, res){
          var dreq = domain.create ();
          dreq.on (&quot;error&quot;, function (error){
              res.writeHead (500);
              res.end ()
              console.error (error);
              dreq.dispose ();
          });
          dreq.run (function (){
              null.error
              //...
              //dreq.dispose ();
          });
      }).listen (1337, &quot;localhost&quot;);
  });
  </code></pre>
  <p>Am I introducing memory leaks?</p>
  <hr />
  <p>The easy solution would be to assign a unique ID to each request and save them in an object or an array and when the emitter emits find the handler that must be executed given the unique ID. Totally inacceptable.</p>
  <hr />
  <p>If I can't save a reference to the <code>res</code> object how can I send an error to the user inside the domain error handler?</p>