Permalink
Browse files

Bugfix (did attempt to print lines before line 1)

Compatibility improvement (registering the error handler had no effect when called in the very first file)
Added a screenshot
Added example files (JavaScript and CoffeeScript)
  • Loading branch information...
1 parent d2e9129 commit ce59e8d916cf2e0ba8572431ae6786944eea8ff8 @DennisKehrig committed May 4, 2012
Showing with 136 additions and 11 deletions.
  1. +53 −0 README.html
  2. +36 −6 README.md
  3. +3 −0 examples/lib.js
  4. +5 −0 examples/long.coffee
  5. +6 −0 examples/long.js
  6. +4 −0 examples/short.coffee
  7. +5 −0 examples/short.js
  8. +14 −3 lib/exception-handler.js
  9. BIN screenshot.png
  10. +10 −2 src/exception-handler.coffee
View
@@ -0,0 +1,53 @@
+<h1>ANSInception</h1>
+
+<p>Colorful exception handler for Node.js with CoffeeScript support and improved nodemon/supervisor compatibility</p>
+
+<p><img src="screenshot.png" alt="Screenshot" title="" /></p>
+
+<h2>Features</h2>
+
+<ul>
+<li>Quits after a delay to prevent nodemon, supervisor, etc. from eating console.log output sent shortly before the exception was thrown</li>
+<li>Shows up to five lines of code from each entry in the stack trace</li>
+<li>Color codes the offending part of the code (based on the column number)</li>
+<li>Compiles CoffeeScript to show the actual offending code (otherwise the line numbers don't make sense)</li>
+<li>Highlights the file name in stack trace paths</li>
+<li>Uses different colors depending on the location of the code
+(<span style="background-color:black;color:#0f0">your code</span>, <span style="background-color:black;color:#ff0">local module</span>, <span style="background-color:black;color:#f0f">global module/node.js internals</span>)</li>
+</ul>
+
+<p>Unfortunately registering with 'uncaughtException' has no effect until the next tick.
+So in order to benefit from the exception handler right away, you need to postpone execution of your code.
+You can either do this manually or use the convience version (see below)</p>
+
+<h2>Usage with CoffeeScript</h2>
+
+<p><strong>Short version:</strong></p>
+
+<pre><code>require('ansinception') -&gt;
+ # Your code
+</code></pre>
+
+<p><strong>Long version:</strong></p>
+
+<pre><code>process.on 'uncaughtException', require 'ansinception'
+process.nextTick -&gt;
+ # Your code
+</code></pre>
+
+<h2>Usage with JavaScript</h2>
+
+<p><strong>Short version:</strong></p>
+
+<pre><code>require('ansinception')(function() {
+ // Your code
+});
+</code></pre>
+
+<p><strong>Long version:</strong></p>
+
+<pre><code>process.on('uncaughtException', require('ansinception'));
+process.nextTick(function() {
+ // Your code
+});
+</code></pre>
View
@@ -3,19 +3,49 @@ ANSInception
Colorful exception handler for Node.js with CoffeeScript support and improved nodemon/supervisor compatibility
-* Quits after a delay to prevent nodemon, supervisor, etc. from eating console.log output
-* Show up to five lines of code from each entry in the stack trace
+![Screenshot](screenshot.png)
+
+Features
+--------
+
+* Quits after a delay to prevent nodemon, supervisor, etc. from eating console.log output sent shortly before the exception was thrown
+* Shows up to five lines of code from each entry in the stack trace
* Color codes the offending part of the code (based on the column number)
* Compiles CoffeeScript to show the actual offending code (otherwise the line numbers don't make sense)
* Highlights the file name in stack trace paths
+* Uses different colors depending on the location of the code
+ (<span style="background-color:black;color:#0f0">your code</span>, <span style="background-color:black;color:#ff0">local module</span>, <span style="background-color:black;color:#f0f">global module/node.js internals</span>)
+
+Unfortunately registering with 'uncaughtException' has no effect until the next tick.
+So in order to benefit from the exception handler right away, you need to postpone execution of your code.
+You can either do this manually or use the convience version (see below)
+
+Usage with CoffeeScript
+-----------------------
-Usage
------
+__Short version:__
-__CoffeeScript:__
+ require('ansinception') ->
+ # Your code
+
+__Long version:__
process.on 'uncaughtException', require 'ansinception'
+ process.nextTick ->
+ # Your code
+
+Usage with JavaScript
+---------------------
+
+__Short version:__
+
+ require('ansinception')(function() {
+ // Your code
+ });
-__JavaScript:__
+__Long version:__
process.on('uncaughtException', require('ansinception'));
+ process.nextTick(function() {
+ // Your code
+ });
View
@@ -0,0 +1,3 @@
+module.exports = function(name) {
+ return name.toUpperCase();
+}
View
@@ -0,0 +1,5 @@
+process.on 'uncaughtException', require 'ansinception'
+process.nextTick ->
+ upper = require './lib'
+ console.log upper("I like turtles")
+ console.log upper()
View
@@ -0,0 +1,6 @@
+process.on('uncaughtException', require('ansinception'));
+process.nextTick(function() {
+ upper = require('./lib');
+ console.log(upper("I like turtles"));
+ console.log(upper());
+});
View
@@ -0,0 +1,4 @@
+require('ansinception') ->
+ upper = require './lib'
+ console.log upper("I like turtles")
+ console.log upper()
View
@@ -0,0 +1,5 @@
+require('ansinception')(function() {
+ var upper = require('./lib');
+ console.log(upper("I like turtles"));
+ console.log(upper());
+});
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@@ -44,7 +44,15 @@ format1 = /^\s*at (.*) \(([^\)]+):(\d+):(\d+)\)$/
# at <path>:<line>:<column>
format2 = /^\s*at ()([^\)]+):(\d+):(\d+)$/
# The exception handler
-module.exports = (exception) ->
+
+exports = module.exports = (callback) ->
+ if callback instanceof Error
+ exports.handler callback
+ else
+ process.on 'uncaughtException', exports.handler
+ process.nextTick callback
+
+exports.handler = (exception) ->
# Start off with how the app was started in the first place
console.log "\n#{baseColor} ,-- $ #{brightCyan}#{process.argv.join ' '}#{reset}"
@@ -74,7 +82,6 @@ module.exports = (exception) ->
# Delay exiting when an exception occurs so console.log calls that occured
# just before the error are also printed by nodemon, supervisor, etc.
setTimeout ->
- console.log "Now quitting"
process.exit 1
, 500
@@ -121,6 +128,7 @@ logStackEntry = (context, file, lineNumber, columnNumber) ->
# Read two lines before and after the offending one
for i in [(lineIndex-extraLines)..(lineIndex+extraLines)]
+ continue if i < 0
break if i >= lines.length
numColor = darkWhite

0 comments on commit ce59e8d

Please sign in to comment.