error/index.js
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by TJ Holowaychuk · committed by TJ Holowaychuk · parent 13c18fa · Browse files · 4 files·+76 added−74 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+76 added−74 removed
- tests:+28 added−26 removed
- other:+48 added−48 removed
Provenance
- Commit
9719b58- Tree
ce4f114- Author
TJ Holowaychuk <tj@vision-media.ca>- Committer
TJ Holowaychuk <tj@vision-media.ca>- Parent
13c18fa
4 files changed+76 added−74 removed
deleted:examples/error/app.js-48 removed
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
/** * Module dependencies. */
var express = require('../../') , app = module.exports = express() , test = app.get('env') == 'test';
if (!test) app.use(express.logger('dev'));app.use(app.router);
// the error handler is strategically// placed *below* the app.router; if it// were above it would not receive errors// from app.get() etc app.use(error);
// error handling middleware have an arity of 4// instead of the typical (req, res, next),// otherwise they behave exactly like regular// middleware, you may have several of them,// in different orders etc.
function error(err, req, res, next) { // log it if (!test) console.error(err.stack);
// respond with 500 "Internal Server Error". res.send(500);}
app.get('/', function(req, res){ // Caught and passed down to the errorHandler middleware throw new Error('something broke!');});
app.get('/next', function(req, res, next){ // We can also pass exceptions to next() process.nextTick(function(){ next(new Error('oh no!')); });});
if (!module.parent) { app.listen(3000); console.log('Express started on port 3000');}No newline at end of fileadded:examples/error/index.js+48 added
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
/** * Module dependencies. */
var express = require('../../') , app = module.exports = express() , test = app.get('env') == 'test';
if (!test) app.use(express.logger('dev'));app.use(app.router);
// the error handler is strategically// placed *below* the app.router; if it// were above it would not receive errors// from app.get() etc app.use(error);
// error handling middleware have an arity of 4// instead of the typical (req, res, next),// otherwise they behave exactly like regular// middleware, you may have several of them,// in different orders etc.
function error(err, req, res, next) { // log it if (!test) console.error(err.stack);
// respond with 500 "Internal Server Error". res.send(500);}
app.get('/', function(req, res){ // Caught and passed down to the errorHandler middleware throw new Error('something broke!');});
app.get('/next', function(req, res, next){ // We can also pass exceptions to next() process.nextTick(function(){ next(new Error('oh no!')); });});
if (!module.parent) { app.listen(3000); console.log('Express started on port 3000');}No newline at end of filemodified:test/acceptance/error-pages.js+26 added-25 removed
1234567889910111213141516171718181920212223242525262627282930313233333434353637383940414243444445464546474849505152535354555657585954555657585960616263646566666768676869707172733 unmodified lines7778798080818283848511 unmodified lines979899100100101102101102103104105106107
var app = require('../../examples/error-pages') , request = require('../support/http');
describe('error-pages', function(){ describe('GET /', function(){ it('should respond with page list', function(done){ request(app) .get('/') .expect(/Pages Example/,done) .get('/') .expect(/Pages Example/, done) }) })
describe('Accept: text/html',function(){ describe('GET /403', function(){ it('should respond with 403', function(done){ request(app) .get('/403') .expect(403,done) .get('/403') .expect(403, done) }) })
describe('GET /404', function(){ it('should respond with 404', function(done){ request(app) .get('/404') .expect(404,done) .get('/404') .expect(404, done) }) })
describe('GET /500', function(){ it('should respond with 500', function(done){ request(app) .get('/500') .expect(500,done) .get('/500') .expect(500, done) }) }) })
describe('Accept: application/json',function(){ describe('GET /403', function(){ it('should respond with 403', function(done){ request(app) .get('/403') .set('Accept','application/json') .expect(403,done) .get('/403') .set('Accept','application/json') .expect(403, done) }) })
describe('GET /404', function(){ it('should respond with 404', function(done){ request(app) .get('/404') .set('Accept','application/json') .end(function(res){ res.should.have.property('statusCode',200) res.should.have.property('body',JSON.stringify({error:'Not found'})) done() }) .get('/404') .set('Accept','application/json') .end(function(res){ res.should.have.property('statusCode',200) res.should.have.property('body',JSON.stringify({error:'Not found'})) done() }) }) })
describe('GET /500', function(){ it('should respond with 500', function(done){ request(app) .get('/500') .set('Accept','application/json') .expect(500,done) .get('/500') .set('Accept','application/json') .expect(500, done) }) }) })
3 unmodified lines it('should respond with 403', function(done){ request(app) .get('/403') .set('Accept','text/plain') .expect(403,done) .expect(403, done) }) })
describe('GET /404', function(){11 unmodified lines
describe('GET /500', function(){ it('should respond with 500', function(done){ request(app) .get('/500') .set('Accept','text/plain') .expect(500,done) .get('/500') .set('Accept','text/plain') .expect(500, done) }) }) })})No newline at end of fileNo newline at end of filemodified:test/acceptance/error.js+2 added-1 removed
112345623 unmodified lines
var app = require('../../examples/error/app')var app = require('../../examples/error') , request = require('../support/http');
describe('error', function(){ describe('GET /', function(){23 unmodified lines