From 474527c1420268c341e2cc371ce98602cd8daf7a Mon Sep 17 00:00:00 2001 From: Raashish Aggarwal <94279692+raashish1601@users.noreply.github.com> Date: Mon, 11 May 2026 15:38:49 +0530 Subject: [PATCH] Skip default logging for client errors --- lib/application.js | 42 ++++++++++++++++++++++++++++++++- test/app.js | 59 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 100 insertions(+), 1 deletion(-) diff --git a/lib/application.js b/lib/application.js index 47be2a20c1a..b8b30003117 100644 --- a/lib/application.js +++ b/lib/application.js @@ -609,14 +609,54 @@ app.listen = function listen() { * Log error using console.error. * * @param {Error} err + * @param {IncomingMessage} req + * @param {ServerResponse} res * @private */ -function logerror(err) { +function logerror(err, req, res) { + if (isClientError(err, res)) return + /* istanbul ignore next */ if (this.get('env') !== 'test') console.error(err.stack || err.toString()); } +/** + * Check if an error should be considered a client error. + * + * @param {Error} err + * @param {ServerResponse} res + * @private + */ + +function isClientError(err, res) { + var status = getErrorStatusCode(err) + + if (status === undefined && res) { + status = res.statusCode + } + + return typeof status === 'number' && status >= 400 && status < 500 +} + +/** + * Get status code from Error object. + * + * @param {Error} err + * @return {number} + * @private + */ + +function getErrorStatusCode(err) { + if (typeof err.status === 'number' && err.status >= 400 && err.status < 600) { + return err.status + } + + if (typeof err.statusCode === 'number' && err.statusCode >= 400 && err.statusCode < 600) { + return err.statusCode + } +} + /** * Try rendering a view. * @private diff --git a/test/app.js b/test/app.js index c1e815a052d..d62e7d576dc 100644 --- a/test/app.js +++ b/test/app.js @@ -21,6 +21,65 @@ describe('app', function(){ .get('/') .expect(404, done); }) + + it('should not log client errors', function(done){ + var app = express() + var errors = [] + var original = console.error + + app.set('env', 'development') + app.use(function (req, res, next) { + var err = new Error('missing') + err.status = 404 + next(err) + }) + + console.error = function (err) { + errors.push(err) + } + + request(app) + .get('/') + .expect(404, function (err) { + setImmediate(function () { + console.error = original + + if (err) return done(err) + + assert.strictEqual(errors.length, 0) + done() + }) + }) + }) + + it('should log server errors', function(done){ + var app = express() + var errors = [] + var original = console.error + + app.set('env', 'development') + app.use(function (req, res, next) { + next(new Error('boom')) + }) + + console.error = function (err) { + errors.push(err) + } + + request(app) + .get('/') + .expect(500, function (err) { + setImmediate(function () { + console.error = original + + if (err) return done(err) + + assert.strictEqual(errors.length, 1) + assert.match(errors[0], /Error: boom/) + done() + }) + }) + }) }) describe('app.parent', function(){