Permalink
Browse files

Fixed errorHandler's JSON response for 0.6.x

err.message and err.stack are no longer enumerable,
so must be explicitly included.
Also updated examples/error.js for 2.0 API
  • Loading branch information...
1 parent 8141905 commit b2eafa26f0854deb0d6dbbc30b940d2aa54c0fc3 @sky-glenjamin sky-glenjamin committed Nov 27, 2011
Showing with 16 additions and 16 deletions.
  1. +11 −13 examples/error.js
  2. +5 −3 lib/middleware/errorHandler.js
View
@@ -1,21 +1,19 @@
-/**
- * Module dependencies.
- */
-
-var connect = require('../');
+var connect = require('../')
+ , http = require('http');
// try:
// - viewing in a browser
// - curl http://localhost:3000
// - curl -H "Accept: application/json" http://localhost:3000
-var server = connect.createServer(
- function(req, res, next){
- throw new Error('oh noes!');
- },
- connect.errorHandler({ stack: true, dump: true })
-);
+var app = connect()
+ .use(function(req, res, next){
+ var err = new Error('oh noes!');
+ err.number = 7;
+ throw err;
+ })
+ .use(connect.errorHandler());
-server.listen(3000);
-console.log('Connect server listening on port 3000');
+http.Server(app).listen(3000);
+console.log('Server started on port 3000');
@@ -30,7 +30,7 @@ var env = process.env.NODE_ENV || 'development';
* JSON:
*
* When _application/json_ is accepted, connect will respond with
- * an object in the form of `{ "error": error }`.
+ * an object in the form of `{ "error": error }`.
*
* HTML:
*
@@ -65,7 +65,9 @@ exports = module.exports = function errorHandler(){
});
// json
} else if (~accept.indexOf('json')) {
- var json = JSON.stringify({ error: err });
+ var error = { message: err.message, stack: err.stack };
@bminer
bminer Dec 14, 2011 Contributor

Nice catch...

+ for (var prop in err) error[prop] = err[prop];
+ var json = JSON.stringify({ error: error });
res.setHeader('Content-Type', 'application/json');
res.end(json);
// plain text
@@ -80,4 +82,4 @@ exports = module.exports = function errorHandler(){
* Template title.
*/
-exports.title = 'Connect';
+exports.title = 'Connect';

1 comment on commit b2eafa2

@bminer
Contributor
bminer commented on b2eafa2 Dec 14, 2011

Would like to see this merged with Connect 1.8.2, if possible??? Maybe just the changes to errorHandler.js??

Please sign in to comment.