Merge pull request +9523 from c9/issue-9491-fix-errors

Issue 9491 fix errors
pull/199/head
Matthijs van Henten 2015-09-23 10:17:41 +02:00
commit c6cddc3454
2 zmienionych plików z 53 dodań i 40 usunięć

Wyświetl plik

@ -125,6 +125,7 @@ module.exports = function(config, optimist) {
}, },
{ {
packagePath: "./c9.error/error_handler", packagePath: "./c9.error/error_handler",
mode: config.mode,
scope: "standalone", scope: "standalone",
hostname: config.hostname hostname: config.hostname
}, },

Wyświetl plik

@ -14,7 +14,12 @@ plugin.provides = [
module.exports = plugin; module.exports = plugin;
var errorPages = { 404: 1, 401: 1, 500: 1, 503: 1}; var errorPages = {
404: 1,
401: 1,
500: 1,
503: 1
};
var statusCodes = { var statusCodes = {
400: "Bad Request", 400: "Bad Request",
@ -72,6 +77,10 @@ function plugin(options, imports, register) {
var frontdoor = require("frontdoor"); var frontdoor = require("frontdoor");
var statics = imports["connect.static"]; var statics = imports["connect.static"];
function isDev(mode) {
return /^(onlinedev|devel)$/.test(mode);
}
// serve index.html // serve index.html
statics.addStatics([{ statics.addStatics([{
path: __dirname + "/www", path: __dirname + "/www",
@ -81,60 +90,63 @@ function plugin(options, imports, register) {
// make sure res.json is available // make sure res.json is available
connect.useStart(frontdoor.middleware.jsonWriter()); connect.useStart(frontdoor.middleware.jsonWriter());
function sanitizeErrorCode(code) {
code = parseInt(code, 10);
if (isNaN(code)) return 500;
if (code < 400) return 500;
return code;
}
connect.useError(function(err, req, res, next) { connect.useError(function(err, req, res, next) {
if (typeof err == "string") if (typeof err == "string")
err = new errors.InternalServerError(err); err = new errors.InternalServerError(err);
var statusCode = parseInt(err.code || err.status || res.statusCode, 10) || 500; var statusCode = sanitizeErrorCode(err.code || err.status || res.statusCode);
var stack;
if (statusCode < 400) if (isDev(options.mode))
statusCode = 500; stack = err.stack || err.message || err.toString();
if (statusCode < 100)
statusCode = 500;
var stack = err.stack || err.message || err.toString();
console.error(stack);
var accept = req.headers.accept || ''; var accept = req.headers.accept || '';
// html
if (~accept.indexOf('html')) {
stack = stack.split('\n').slice(1);
var path = errorPages[statusCode] if (/json/.test(accept)) {
? __dirname + "/views/error-" + statusCode + ".html.ejs"
: __dirname + "/views/error.html.ejs";
res.setHeader('Content-Type', 'text/html; charset=utf-8');
res.render(path, {
title: statusCodes[statusCode] || NICE_USER_ERROR_MSG,
scope: options.scope || "",
showStackTrace: showStackTrace,
stack: stack,
statusCode: statusCode,
error: err.toString()
}, next);
// json
} else if (~accept.indexOf('json')) {
var error = { var error = {
code: statusCode,
message: err.message, message: err.message,
hostname: options.hostname, hostname: options.hostname,
scope: options.scope scope: options.scope,
stack: stack,
}; };
for (var prop in err) error[prop] = err[prop];
try { try {
JSON.stringify(error); JSON.stringify(error);
} catch (e) { }
catch (e) {
console.error("Cannot send error as JSON: ", error); console.error("Cannot send error as JSON: ", error);
error.message = NICE_USER_ERROR_MSG; error.message = NICE_USER_ERROR_MSG;
error.scope = null; error.scope = null;
} }
res.json({ error: error }, null, statusCode);
// plain text return res.json({
} else { error: error
res.writeHead(statusCode, { 'Content-Type': 'text/plain' }); }, null, statusCode);
res.end(stack);
} }
var path = errorPages[statusCode] ? __dirname + "/views/error-" + statusCode + ".html.ejs" : __dirname + "/views/error.html.ejs";
res.setHeader('Content-Type', 'text/html; charset=utf-8');
res.render(path, {
title: statusCodes[statusCode] || NICE_USER_ERROR_MSG,
scope: options.scope || "",
showStackTrace: showStackTrace,
stack: stack.split('\n').slice(1),
statusCode: statusCode,
error: err.toString()
}, next);
}); });
register(null, { register(null, {