From 386827d825232a10f99d54d121d4f5eb5f92eebe Mon Sep 17 00:00:00 2001 From: ansuz Date: Tue, 22 Mar 2022 16:38:42 +0530 Subject: [PATCH] handle 500 errors --- customize.dist/500.html | 16 ++++++++++++++++ customize.dist/four-oh-four.js | 7 +++++-- lib/env.js | 2 -- server.js | 23 ++++++++++++++++++++--- 4 files changed, 41 insertions(+), 7 deletions(-) create mode 100644 customize.dist/500.html diff --git a/customize.dist/500.html b/customize.dist/500.html new file mode 100644 index 000000000..7e84c8737 --- /dev/null +++ b/customize.dist/500.html @@ -0,0 +1,16 @@ + + + + + CryptPad: Collaboration suite, encrypted and open-source + + + + + + + diff --git a/customize.dist/four-oh-four.js b/customize.dist/four-oh-four.js index 568faf951..b344a4af8 100644 --- a/customize.dist/four-oh-four.js +++ b/customize.dist/four-oh-four.js @@ -12,9 +12,12 @@ define([ src: '/customize/CryptPad_logo_grey.svg?' + urlArgs }); + Messages.fivehundred_internalServerError = 'Internal Server Error'; // XXX + + var is500 = Boolean(document.querySelector('#five-hundred')); var brand = h('h1#cp-brand', 'CryptPad'); - var message = h('h2#cp-scramble', Messages.four04_pageNotFound); - var title = h('h2#cp-title', "404"); + var message = h('h2#cp-scramble', Messages[is500? 'fivehundred_internalServerError':'four04_pageNotFound']); + var title = h('h2#cp-title', is500? "500":"404"); var loggedIn = LocalStore.isLoggedIn(); var link = h('a#cp-link', { diff --git a/lib/env.js b/lib/env.js index 516a9af7d..1923dcc9e 100644 --- a/lib/env.js +++ b/lib/env.js @@ -72,8 +72,6 @@ module.exports.create = function (config) { NO_SANDBOX: NO_SANDBOX, httpSafePort: httpSafePort, - NODE_ENV: process.env.NODE_ENV, - shouldUpdateNode: !isRecentVersion(), version: Package.version, diff --git a/server.js b/server.js index 30920abef..e347cf16c 100644 --- a/server.js +++ b/server.js @@ -262,7 +262,9 @@ app.get('/api/config', serveConfig); app.get('/api/broadcast', serveBroadcast); var four04_path = Path.resolve(__dirname + '/customize.dist/404.html'); +var fivehundred_path = Path.resolve(__dirname + '/customize.dist/500.html'); var custom_four04_path = Path.resolve(__dirname + '/customize/404.html'); +var custom_fivehundred_path = Path.resolve(__dirname + '/customize/500.html'); var send404 = function (res, path) { if (!path && path !== four04_path) { path = four04_path; } @@ -272,6 +274,15 @@ var send404 = function (res, path) { send404(res); }); }; +var send500 = function (res, path) { + if (!path && path !== fivehundred_path) { path = fivehundred_path; } + Fs.exists(path, function (exists) { + res.setHeader('Content-Type', 'text/html; charset=utf-8'); + if (exists) { return Fs.createReadStream(path).pipe(res); } + send500(res); + }); +}; + app.get('/api/profiling', function (req, res, next) { if (!Env.enableProfiling) { return void send404(res); } res.setHeader('Content-Type', 'text/javascript'); @@ -285,6 +296,15 @@ app.use(function (req, res, next) { send404(res, custom_four04_path); }); +// default message for thrown errors in ExpressJS routes +app.use(function (err, req, res, next) { + Env.Log.error('EXPRESSJS_ROUTING', { + error: err.stack || err, + }); + res.status(500); + send500(res, custom_fivehundred_path); +}); + var httpServer = Env.httpServer = Http.createServer(app); nThen(function (w) { @@ -332,9 +352,6 @@ nThen(function (w) { currentVersion: process.version, }); } - if (Env.NODE_ENV !== 'production') { - Env.Log.warn("NODE_ENV", `If this server is running in a production context then it is recommended that you set NODE_ENV=production to prevent Expressjs from responding with stack traces when it catches an error.`); - } if (Env.OFFLINE_MODE) { return; } if (Env.websocketPath) { return; }