handle 500 errors

pull/1/head
ansuz 3 years ago
parent af10547dca
commit 386827d825

@ -0,0 +1,16 @@
<!DOCTYPE html>
<html class="cp" id="five-hundred">
<!-- If this file is not called customize.dist/src/template.html, it is generated -->
<head>
<title data-localization="main_title">CryptPad: Collaboration suite, encrypted and open-source</title>
<meta content="text/html; charset=utf-8" http-equiv="content-type"/>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<link rel="icon" type="image/png" href="/customize/favicon/main-favicon.png" id="favicon"/>
<script async data-bootload="/customize/four-oh-four.js" data-main="/common/boot.js?ver=1.0" src="/bower_components/requirejs/require.js?ver=2.3.5"></script>
</head>
<body class="html">
<noscript>
<h1>500</h1>
<h3>Internal server error</h3>
</noscript>

@ -12,9 +12,12 @@ define([
src: '/customize/CryptPad_logo_grey.svg?' + urlArgs 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 brand = h('h1#cp-brand', 'CryptPad');
var message = h('h2#cp-scramble', Messages.four04_pageNotFound); var message = h('h2#cp-scramble', Messages[is500? 'fivehundred_internalServerError':'four04_pageNotFound']);
var title = h('h2#cp-title', "404"); var title = h('h2#cp-title', is500? "500":"404");
var loggedIn = LocalStore.isLoggedIn(); var loggedIn = LocalStore.isLoggedIn();
var link = h('a#cp-link', { var link = h('a#cp-link', {

@ -72,8 +72,6 @@ module.exports.create = function (config) {
NO_SANDBOX: NO_SANDBOX, NO_SANDBOX: NO_SANDBOX,
httpSafePort: httpSafePort, httpSafePort: httpSafePort,
NODE_ENV: process.env.NODE_ENV,
shouldUpdateNode: !isRecentVersion(), shouldUpdateNode: !isRecentVersion(),
version: Package.version, version: Package.version,

@ -262,7 +262,9 @@ app.get('/api/config', serveConfig);
app.get('/api/broadcast', serveBroadcast); app.get('/api/broadcast', serveBroadcast);
var four04_path = Path.resolve(__dirname + '/customize.dist/404.html'); 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_four04_path = Path.resolve(__dirname + '/customize/404.html');
var custom_fivehundred_path = Path.resolve(__dirname + '/customize/500.html');
var send404 = function (res, path) { var send404 = function (res, path) {
if (!path && path !== four04_path) { path = four04_path; } if (!path && path !== four04_path) { path = four04_path; }
@ -272,6 +274,15 @@ var send404 = function (res, path) {
send404(res); 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) { app.get('/api/profiling', function (req, res, next) {
if (!Env.enableProfiling) { return void send404(res); } if (!Env.enableProfiling) { return void send404(res); }
res.setHeader('Content-Type', 'text/javascript'); res.setHeader('Content-Type', 'text/javascript');
@ -285,6 +296,15 @@ app.use(function (req, res, next) {
send404(res, custom_four04_path); 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); var httpServer = Env.httpServer = Http.createServer(app);
nThen(function (w) { nThen(function (w) {
@ -332,9 +352,6 @@ nThen(function (w) {
currentVersion: process.version, 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.OFFLINE_MODE) { return; }
if (Env.websocketPath) { return; } if (Env.websocketPath) { return; }

Loading…
Cancel
Save