Note that there are some explanatory texts on larger screens.

plurals
  1. PONode.js - Domain per Express request, inside another domain
    primarykey
    data
    text
    <p><strong>Error handling in Node. Argh!</strong></p> <p>I'm trying to layout a basic Node app like this...</p> <blockquote> <p>Cluster -> Worker -> Server Domain -> Express Request Domain</p> </blockquote> <p>So, if an error is thrown 18 layers deep into a call stack because someone misspelled their name on a login form, the entire server doesn't crash.</p> <p>Here's some basic code to simulate the worker part:</p> <pre><code>var domain, server; domain = require('domain'); server = domain.create(); server.on('error', function(e) { console.log('total meltdown...', e.stack); }); server.run(function() { var express = require('express')(); express.configure(function() { // Domain on EVERY request express.use(function(req, res, next) { var d = domain.create(); d.on('error', function(e) { console.log('fired REQUEST error', e.stack); next(e); }); d.run(next); }); // Generic error handler express.use(function(e, req, res, next) { res.status(500); res.end('oops'); }); // Serve the request with a blatent error express.get('/', function(req, res) { this_function_does_not_exist(); res.end('we will never get here'); }); }); // Fire 'er up express.listen(3000); }); </code></pre> <p><strong>What I'm expecting...</strong></p> <p>I curl <code>http://localhost:3000/</code>, get a nice little 'oops' error, and see 'fired REQUEST error' and the error stack in the console.</p> <p><strong>What actually happens...</strong></p> <p>I get this both as the browser response, and in the console...</p> <blockquote> <p>ReferenceError: this_function_does_not_exist is not defined at /Stuff/test.js:38:13 at callbacks (/Stuff/node_modules/express/lib/router/index.js:161:37) at param (/Stuff/node_modules/express/lib/router/index.js:135:11) at pass (/Stuff/node_modules/express/lib/router/index.js:142:5) at Router._dispatch (/Stuff/node_modules/express/lib/router/index.js:170:5) at Object.router (/Stuff/node_modules/express/lib/router/index.js:33:10) at next (/Stuff/node_modules/express/node_modules/connect/lib/proto.js:190:15) at next (/Stuff/node_modules/express/node_modules/connect/lib/proto.js:192:9) at b (domain.js:183:18) at Domain.run (domain.js:123:23)</p> </blockquote> <p>Now why would it go and do something like that?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload