express-error
Advanced tools
Comparing version 0.0.2 to 0.0.3
@@ -29,3 +29,3 @@ /** | ||
app.configure('development', function() { | ||
app.use(expressError.express3({contextLinesCount: 3})); | ||
app.use(expressError.express3({contextLinesCount: 3, handleUncaughtException: true})); | ||
}); | ||
@@ -32,0 +32,0 @@ |
// Generated by CoffeeScript 1.3.3 | ||
var FS, HOME, Path, Utils, alignLeft, betterStack, color, env, formatHtml, formatText, handleUncaughtExceptions, htmlEscape, injectSourceLines; | ||
var FS, HOME, Path, Utils, alignLeft, betterStack, env, formatHtml, formatText, handleUncaughtExceptions, htmlEscape, injectSourceLines; | ||
@@ -14,6 +14,2 @@ FS = require("fs"); | ||
color = function(s) { | ||
return s; | ||
}; | ||
alignLeft = function(lines) { | ||
@@ -40,34 +36,8 @@ var i, left, line, result, _i, _j, _k, _len, _len1, _ref, _results; | ||
/* | ||
Gets original source line and injects into stack. | ||
htmlEscape = function(s) { | ||
return String(s).replace(/&(?!\w+;)/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"'); | ||
}; | ||
@param {Array} lines Array of lines from stack. | ||
@param {String} fileName The file being tested. | ||
@returns [ | ||
{ | ||
frame: 'stack line', code: [ | ||
{ linenum: 123, code: 'compiled line'}, | ||
{ linenum: 124, code: 'compiled line'}, | ||
{ linenum: 125, code: 'compiled line', isErrorLine: true}, | ||
{ linenum: 126, code: 'compiled line'}, | ||
{ linenum: 127, code: 'compiled line'}, | ||
] | ||
}, | ||
{ | ||
frame: 'stack line', code: [ | ||
{ linenum: 123, code: 'compiled line'}, | ||
{ linenum: 124, code: 'compiled line'}, | ||
{ linenum: 125, code: 'compiled line', isErrorLine: true}, | ||
{ linenum: 126, code: 'compiled line'}, | ||
{ linenum: 127, code: 'compiled line'}, | ||
] | ||
} | ||
] | ||
*/ | ||
injectSourceLines = function(lines, fileName, contextLinesCount) { | ||
var buffer, cache, codeFile, coffee, col, collectSource, ext, i, lineObj, linenum, matches, newLines, pushLine, re, text; | ||
injectSourceLines = function(lines, contextLinesCount) { | ||
var cache, codeFile, coffee, col, collectSource, ext, i, lineObj, linenum, matches, newLines, pushLine, re, text; | ||
newLines = []; | ||
@@ -126,19 +96,14 @@ collectSource = true; | ||
try { | ||
if (ext === ".js") { | ||
text = cache[codeFile]; | ||
if (!text) { | ||
text = FS.readFileSync(codeFile, "utf8"); | ||
text = cache[codeFile]; | ||
if (!text) { | ||
text = FS.readFileSync(codeFile, "utf8"); | ||
if (ext === ".js") { | ||
cache[codeFile] = text; | ||
} | ||
pushLine(text); | ||
} else if (ext === ".coffee") { | ||
text = cache[codeFile]; | ||
if (!text) { | ||
} else if (ext === ".coffee") { | ||
coffee = require("coffee-script"); | ||
buffer = FS.readFileSync(codeFile, "utf8"); | ||
text = coffee.compile(buffer, {}); | ||
text = coffee.compile(text, {}); | ||
cache[codeFile] = text; | ||
} | ||
pushLine(text); | ||
} | ||
pushLine(text); | ||
} catch (err) { | ||
@@ -164,27 +129,11 @@ console.log(err.stack); | ||
betterStack = function(stack, contextLinesCount, fileName) { | ||
var i, line, lines, result; | ||
if (fileName == null) { | ||
fileName = 'foo'; | ||
} | ||
betterStack = function(stack, contextLinesCount) { | ||
var line, lines, result, _i, _len; | ||
lines = stack.split("\n"); | ||
result = []; | ||
i = 0; | ||
while (i < lines.length) { | ||
line = lines[i]; | ||
if (i === 0) { | ||
line = " " + line; | ||
} else { | ||
if (line.indexOf(fileName) >= 0) { | ||
line = color(line, "white+b"); | ||
} else { | ||
if (line.indexOf(HOME) < 0) { | ||
line = color(line, "black+b"); | ||
} | ||
} | ||
} | ||
for (_i = 0, _len = lines.length; _i < _len; _i++) { | ||
line = lines[_i]; | ||
result.push(line.replace(HOME, "~")); | ||
i += 1; | ||
} | ||
return result = injectSourceLines(result, fileName, contextLinesCount); | ||
return injectSourceLines(result, contextLinesCount); | ||
}; | ||
@@ -239,10 +188,6 @@ | ||
handleUncaughtExceptions = function() { | ||
handleUncaughtExceptions = function(contextLinesCount) { | ||
return process.on("uncaughtException", function(err) { | ||
var message, stack; | ||
message = err; | ||
stack = ""; | ||
if (err.stack) { | ||
stack = formatText(betterStack(err.stack, contextLinesCount)); | ||
} | ||
var stack; | ||
stack = err.stack ? formatText(betterStack(err.stack, contextLinesCount)) : ""; | ||
return console.error("Uncaught exception", "" + err.message + "\n" + stack); | ||
@@ -253,3 +198,3 @@ }); | ||
exports.express3 = function(options) { | ||
var contextLinesCount, dumpExceptions, enableUncaughtExceptions, showStack; | ||
var contextLinesCount, dumpExceptions, handleUncaughtException, showStack, title; | ||
if (options == null) { | ||
@@ -260,6 +205,7 @@ options = {}; | ||
dumpExceptions = options.dumpExceptions || false; | ||
enableUncaughtExceptions = options.enableUncaughtExceptions || false; | ||
handleUncaughtException = options.handleUncaughtException || false; | ||
contextLinesCount = options.contextLinesCount || 0; | ||
if (enableUncaughtExceptions) { | ||
handleUncaughtExceptions(); | ||
title = options.title || "express-error"; | ||
if (handleUncaughtException) { | ||
handleUncaughtExceptions(contextLinesCount); | ||
} | ||
@@ -283,3 +229,3 @@ return function(err, req, res, next) { | ||
stack = formatHtml(stack); | ||
html = html.replace("{style}", style).replace("{stack}", stack).replace("{title}", exports.title).replace("{statusCode}", res.statusCode).replace(/\{error\}/g, htmlEscape(err.toString())); | ||
html = html.replace("{style}", style).replace("{stack}", stack).replace("{title}", title).replace("{statusCode}", res.statusCode).replace(/\{error\}/g, htmlEscape(err.toString())); | ||
res.setHeader("Content-Type", "text/html; charset=utf-8"); | ||
@@ -311,6 +257,2 @@ return res.end(html); | ||
htmlEscape = function(s) { | ||
return String(s).replace(/&(?!\w+;)/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"'); | ||
}; | ||
exports.title = "express-error"; | ||
exports.express2 = exports.express3; |
{ | ||
"name": "express-error", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"description": "Display source code in express' error stack for JavaScript or CoffeeScript", | ||
@@ -5,0 +5,0 @@ "keywords": "express3 error stack coffee-script coffee", |
@@ -6,4 +6,3 @@ # express-error | ||
Express-error is an open source project from [Barc](http://barc.com), | ||
instant real-time forum on any website. | ||
Open sourced by [Barc](http://barc.com), instant real-time forum on any website. | ||
@@ -16,7 +15,18 @@ ## Usage | ||
app.configure('development', function() { | ||
# show 3 lines before and after the error line which is helpful for CoffeeScript | ||
app.use(expressError.express3({contextLinesCount: 3})); | ||
# Show 3 lines before and after the error line which is helpful for CoffeeScript. | ||
# Also handle uncaught exception. | ||
app.use(expressError.express3({contextLinesCount: 3, handleUncaughtException: true})); | ||
}); | ||
``` | ||
## Options | ||
``` | ||
{ | ||
contextLinesCount: Integer, // Number of lines to insert before and after the error line. | ||
handleUncaughtException: Boolean, // Whether to handle uncaught exception. | ||
title: String // The title for HTML error page | ||
} | ||
``` | ||
## Screenshot | ||
@@ -23,0 +33,0 @@ |
Sorry, the diff of this file is not supported yet
21
45
88399
511
6
2