14
15var contentDisposition = require('content-disposition');
16▶var createError = require('http-errors')
17var deprecate = require('depd')('express');
18var encodeUrl = require('encodeurl');
· · ·
53 *
54 * Expects an integer value between 100 and 999 inclusive.
55▶ * Throws an error if the provided status code is not an integer or if it's outside the allowable range.
56 *
57 * @param {number} code - The HTTP status code to set.
· · ·
58 * @return {ServerResponse} - Returns itself for chaining methods.
59▶ * @throws {TypeError} If `code` is not an integer.
60 * @throws {RangeError} If `code` is outside the range 100 to 999.
61 * @public
· · ·
60▶ * @throws {RangeError} If `code` is outside the range 100 to 999.
61 * @public
62 */
· · ·
65 // Check if the status code is not an integer
66 if (!Number.isInteger(code)) {
67▶ throw new TypeError(`Invalid status code: ${JSON.stringify(code)}. Status code must be an integer.`);
68 }
69 // Check if the status code is outside of Node's valid range
+ 18 more matches in this file
144 * Dispatch a req, res pair into the application. Starts pipeline processing.
145 *
146▶ * If no callback is provided, then default error handlers will respond
147 * in the event of an error bubbling through the stack.
148 *
· · ·
147▶ * in the event of an error bubbling through the stack.
148 *
149 * @private
· · ·
154 var done = callback || finalhandler(req, res, {
155 env: this.get('env'),
156▶ onerror: logerror.bind(this)
157 });
158
· · ·
211
212 if (fns.length === 0) {
213▶ throw new TypeError('app.use() requires a middleware function')
214 }
215
· · ·
294app.engine = function engine(ext, fn) {
295 if (typeof fn !== 'function') {
296▶ throw new Error('callback function required');
297 }
298
+ 7 more matches in this file
29
30app.use(function(req, res, next){
31▶ var err = req.session.error;
32 var msg = req.session.success;
33 delete req.session.error;
· · ·
33▶ delete req.session.error;
34 delete req.session.success;
35 res.locals.message = '';
· · ·
36▶ if (err) res.locals.message = '<p class="msg error">' + err + '</p>';
37 if (msg) res.locals.message = '<p class="msg success">' + msg + '</p>';
38 next();
· · ·
77 next();
78 } else {
79▶ req.session.error = 'Access denied!';
80 res.redirect('/login');
81 }
· · ·
120 });
121 } else {
122▶ req.session.error = 'Authentication failed, please check your '
123 + ' username and password.'
124 + ' (use "tj" and "foobar")';
30 next();
31 } else {
32▶ next(new Error('Failed to load user ' + req.params.id));
33 }
34}
· · ·
41 } else {
42 // You may want to implement specific exceptions
43▶ // such as UnauthorizedError or similar so that you
44 // can handle these can be special-cased in an error handler
45 // (view ./examples/pages for this)
· · ·
44▶ // can handle these can be special-cased in an error handler
45 // (view ./examples/pages for this)
46 next(new Error('Unauthorized'));
· · ·
46▶ next(new Error('Unauthorized'));
47 }
48}
· · ·
53 next();
54 } else {
55▶ next(new Error('Unauthorized'));
56 }
57 }
9var app = module.exports = express();
10
11▶// create an error with .status. we
12// can then use the property in our
13// custom error handler (Connect respects this prop as well)
· · ·
13▶// custom error handler (Connect respects this prop as well)
14
15function error(status, msg) {
· · ·
15▶function error(status, msg) {
16 var err = new Error(msg);
17 err.status = status;
· · ·
16▶ var err = new Error(msg);
17 err.status = status;
18 return err;
· · ·
32
33 // key isn't present
34▶ if (!key) return next(error(400, 'api key required'));
35
36 // key is invalid
+ 4 more matches in this file
59
60 if (!this.ext && !this.defaultEngine) {
61▶ throw new Error('No default engine was specified and no extension was provided.');
62 }
63
· · ·
82
83 if (typeof fn !== 'function') {
84▶ throw new Error('Module "' + mod + '" does not provide a view engine.')
85 }
86
5 */
6
7▶var createError = require('http-errors')
8var express = require('../../');
9var app = module.exports = express();
· · ·
24 req.params[name] = parseInt(num, 10);
25 if( isNaN(req.params[name]) ){
26▶ next(createError(400, 'failed to parseInt '+num));
27 } else {
28 next();
· · ·
37 next();
38 } else {
39▶ next(createError(404, 'failed to find user'));
40 }
41});
17app.set('view engine', 'ejs');
18
19▶// set views for error and 404 pages
20app.set('views', path.join(__dirname, 'views'));
21
· · ·
78app.use(function(err, req, res, next){
79 // log it
80▶ if (!module.parent) console.error(err.stack);
81
82 // error page
· · ·
82▶ // error page
83 res.status(500).render('5xx');
84});
12if (!test) app.use(logger('dev'));
13
14▶// error handling middleware have an arity of 4
15// instead of the typical (req, res, next),
16// otherwise they behave exactly like regular
· · ·
18// in different orders etc.
19
20▶function error(err, req, res, next) {
21 // log it
22 if (!test) console.error(err.stack);
· · ·
22▶ if (!test) console.error(err.stack);
23
24 // respond with 500 "Internal Server Error".
· · ·
24▶ // respond with 500 "Internal Server Error".
25 res.status(500);
26 res.send('Internal Server Error');
· · ·
26▶ res.send('Internal Server Error');
27}
28
+ 6 more matches in this file
44 },
45 show: function(req, res){
46▶ res.send(users[req.params.id] || { error: 'Cannot find user' });
47 },
48 destroy: function(req, res, id){
· · ·
70// curl http://localhost:3000/users -- responds with all users
71// curl http://localhost:3000/users/1 -- responds with user 1
72▶// curl http://localhost:3000/users/4 -- responds with error
73// curl http://localhost:3000/users/1..3 -- responds with several users
74// curl -X DELETE http://localhost:3000/users/1 -- deletes the user
41 await db.sAdd('cat', 'luna');
42 } catch (err) {
43▶ console.error('Error initializing Redis:', err);
44 process.exit(1);
45 }
· · ·
55 .then((vals) => res.send(vals))
56 .catch((err) => {
57▶ console.error(`Redis error for query "${query}":`, err);
58 next(err);
59 });
27 res.download(req.params.file.join('/'), { root: FILES_DIR }, function (err) {
28 if (!err) return; // file sent
29▶ if (err.status !== 404) return next(err); // non-404 error
30 // file for download not found
31 res.statusCode = 404;
15app.set('view engine', 'ejs');
16
17▶// our custom "verbose errors" setting
18// which we can use in the templates
19// via settings['verbose errors']
· · ·
19▶// via settings['verbose errors']
20app.enable('verbose errors');
21
· · ·
20▶app.enable('verbose errors');
21
22// disable them in production
· · ·
23▶// use $ NODE_ENV=production node examples/error-pages
24if (app.settings.env === 'production') app.disable('verbose errors')
25
· · ·
24▶if (app.settings.env === 'production') app.disable('verbose errors')
25
26silent || app.use(logger('dev'));
+ 15 more matches in this file
59 })
60
61▶ it('should handle render error throws', function(done){
62 var app = express();
63
· · ·
68
69 View.prototype.render = function(options, fn){
70▶ throw new Error('err!');
71 };
72
· · ·
81
82 describe('when the file does not exist', function(){
83▶ it('should provide a helpful error', function(done){
84 var app = createApp();
85
· · ·
93 })
94
95▶ describe('when an error occurs', function(){
96 it('should invoke the callback', function(done){
97 var app = createApp();
· · ·
101 app.render('user.tmpl', function (err) {
102 assert.ok(err)
103▶ assert.equal(err.name, 'RenderError')
104 done()
105 })
+ 1 more matches in this file
917 app.get('/foo', function (req, res, next) {
918 calls.push('/foo');
919▶ next(new Error('fail'));
920 });
921
· · ·
927 res.json({
928 calls: calls,
929▶ error: err.message
930 })
931 })
· · ·
933 request(app)
934 .get('/foo')
935▶ .expect(200, { calls: ['/foo/:bar?', '/foo'], error: 'fail' }, done)
936 })
937
· · ·
940
941 function fn1(req, res, next) {
942▶ next(new Error('boom!'));
943 }
944
· · ·
954
955 app.use(function (err, req, res, next) {
956▶ res.end('error!');
957 })
958
+ 28 more matches in this file
46
47 router.use(function (req, res) {
48▶ throw new Error('should not be called')
49 });
50
· · ·
56
57 router.use(function (req, res) {
58▶ throw new Error('should not be called')
59 })
60
· · ·
68 var use = false
69
70▶ route.post(function (req, res, next) { next(new Error('should not run')) })
71 route.all(function (req, res, next) {
72 all = true
· · ·
73 next()
74 })
75▶ route.get(function (req, res, next) { next(new Error('should not run')) })
76
77 router.get('/foo', function (req, res, next) { next(new Error('should not run')) })
· · ·
77▶ router.get('/foo', function (req, res, next) { next(new Error('should not run')) })
78 router.use(function (req, res, next) {
79 use = true
+ 13 more matches in this file
41 })
42
43▶ // The old node error message modification in body parser is catching this
44 it('should 400 when only whitespace', function (done) {
45 request(createApp())
· · ·
47 .set('Content-Type', 'application/json')
48 .send(' \n')
49▶ .expect(400, '[entity.parse.failed] ' + parseError(' \n'), done)
50 })
51
· · ·
98 .set('Content-Type', 'application/json')
99 .send('{:')
100▶ .expect(400, '[entity.parse.failed] ' + parseError('{:'), done)
101 })
102
· · ·
106 .set('Content-Type', 'application/json')
107 .send('{"user"')
108▶ .expect(400, '[entity.parse.failed] ' + parseError('{"user"'), done)
109 })
110
· · ·
111▶ it('should include original body on error object', function (done) {
112 request(this.app)
113 .post('/')
+ 25 more matches in this file
147 })
148
149▶ it('should not error when inflating', function (done) {
150 var app = createApp({ limit: '1kb' })
151 var test = request(app).post('/')
· · ·
267
268 function accept (req) {
269▶ throw new Error('oops!')
270 }
271
· · ·
280 it('should assert value is function', function () {
281 assert.throws(createApp.bind(null, { verify: 'lol' }),
282▶ /TypeError: option verify must be function/)
283 })
284
· · ·
285▶ it('should error from verify', function (done) {
286 var app = createApp({
287 verify: function (req, res, buf) {
· · ·
288▶ if (buf[0] === 0x00) throw new Error('no leading null')
289 }
290 })
+ 5 more matches in this file
126
127 route.get(function () {
128▶ throw new Error('not me!');
129 })
130
· · ·
168 })
169
170▶ describe('errors', function(){
171 it('should handle errors via arity 4 functions', function(done){
172 var req = { order: '', method: 'GET', url: '/' };
· · ·
171▶ it('should handle errors via arity 4 functions', function(done){
172 var req = { order: '', method: 'GET', url: '/' };
173 var route = new Route('');
· · ·
174
175 route.all(function(req, res, next){
176▶ next(new Error('foobar'));
177 });
178
· · ·
200
201 route.all(function () {
202▶ throw new Error('foobar');
203 });
204
+ 6 more matches in this file
304 })
305
306▶ it('should not error when inflating', function (done) {
307 var app = createApp({ limit: '1kb' })
308 var test = request(app).post('/')
· · ·
318 it('should reject 0', function () {
319 assert.throws(createApp.bind(null, { extended: false, parameterLimit: 0 }),
320▶ /TypeError: option parameterLimit must be a positive number/)
321 })
322
· · ·
323 it('should reject string', function () {
324 assert.throws(createApp.bind(null, { extended: false, parameterLimit: 'beep' }),
325▶ /TypeError: option parameterLimit must be a positive number/)
326 })
327
· · ·
373 it('should reject 0', function () {
374 assert.throws(createApp.bind(null, { extended: true, parameterLimit: 0 }),
375▶ /TypeError: option parameterLimit must be a positive number/)
376 })
377
· · ·
378 it('should reject string', function () {
379 assert.throws(createApp.bind(null, { extended: true, parameterLimit: 'beep' }),
380▶ /TypeError: option parameterLimit must be a positive number/)
381 })
382
+ 11 more matches in this file