fix error handling
This commit is contained in:
parent
db7e736659
commit
0ae9e5a069
|
@ -28,41 +28,41 @@ module.exports.create = function (securePort, certsPath, vhostsdir) {
|
||||||
return dummyCerts;
|
return dummyCerts;
|
||||||
}
|
}
|
||||||
|
|
||||||
function handleAppScopedError(fn) {
|
function handleAppScopedError(req, res, fn) {
|
||||||
return function (req, res, next) {
|
function next(err) {
|
||||||
next(function (err) {
|
if (!err) {
|
||||||
if (!err) {
|
fn(req, res);
|
||||||
fn(req, res);
|
return;
|
||||||
return;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
console.error(err);
|
console.error(err);
|
||||||
res.writeHead(500);
|
res.writeHead(500);
|
||||||
res.end(
|
res.end(
|
||||||
"<html>"
|
"<html>"
|
||||||
+ "<head>"
|
+ "<head>"
|
||||||
+ '<link rel="icon" href="favicon.ico" />'
|
+ '<link rel="icon" href="favicon.ico" />'
|
||||||
+ "</head>"
|
+ "</head>"
|
||||||
+ "<body>"
|
+ "<body>"
|
||||||
+ "<pre>"
|
+ "<pre>"
|
||||||
+ "<code>"
|
+ "<code>"
|
||||||
+ "Method: " + req.method
|
+ "Method: " + req.method
|
||||||
+ '\n'
|
+ '\n'
|
||||||
+ "Hostname: " + domaininfo.hostname
|
+ "Hostname: " + domaininfo.hostname
|
||||||
+ '\n'
|
+ '\n'
|
||||||
+ "App: " + (domaininfo.pathname ? (domaininfo.pathname + '/') : '')
|
+ "App: " + (domaininfo.pathname ? (domaininfo.pathname + '/') : '')
|
||||||
+ '\n'
|
+ '\n'
|
||||||
+ "Route: " + req.url//.replace(/^\//, '')
|
+ "Route: " + req.url//.replace(/^\//, '')
|
||||||
+ '\n'
|
+ '\n'
|
||||||
// TODO better sanatization
|
// TODO better sanatization
|
||||||
+ 'Error: ' + (err.message || err.toString()).replace(/</g, '<')
|
+ 'Error: ' + (err.message || err.toString()).replace(/</g, '<')
|
||||||
+ "</code>"
|
+ "</code>"
|
||||||
+ "</pre>"
|
+ "</pre>"
|
||||||
+ "</body>"
|
+ "</body>"
|
||||||
+ "</html>"
|
+ "</html>"
|
||||||
);
|
);
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return next;
|
||||||
}
|
}
|
||||||
|
|
||||||
function createSecureContext(certs) {
|
function createSecureContext(certs) {
|
||||||
|
@ -135,14 +135,14 @@ module.exports.create = function (securePort, certsPath, vhostsdir) {
|
||||||
// we always add it explicitly
|
// we always add it explicitly
|
||||||
function localAppWrapped(req, res) {
|
function localAppWrapped(req, res) {
|
||||||
console.log('[debug]', domaininfo.hostname + '/' + domaininfo.pathname, req.url);
|
console.log('[debug]', domaininfo.hostname + '/' + domaininfo.pathname, req.url);
|
||||||
localApp(req, res, handleAppScopedError(function () {
|
localApp(req, res, handleAppScopedError(req, res, function (req, res) {
|
||||||
if (!serveFavicon) {
|
if (!serveFavicon) {
|
||||||
serveFavicon = require('serve-favicon')(path.join(__dirname, '..', 'public', 'favicon.ico'));
|
serveFavicon = require('serve-favicon')(path.join(__dirname, '..', 'public', 'favicon.ico'));
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO redirect GET /favicon.ico to GET (req.headers.referer||'') + /favicon.ico
|
// TODO redirect GET /favicon.ico to GET (req.headers.referer||'') + /favicon.ico
|
||||||
// TODO other common root things - robots.txt, app-icon, etc
|
// TODO other common root things - robots.txt, app-icon, etc
|
||||||
serveFavicon(req, res, handleAppScopedError(function () {
|
serveFavicon(req, res, handleAppScopedError(req, res, function (req, res) {
|
||||||
res.writeHead(404);
|
res.writeHead(404);
|
||||||
res.end(
|
res.end(
|
||||||
"<html>"
|
"<html>"
|
||||||
|
|
Loading…
Reference in New Issue