3

Following coffee-script code:

try
    any_error_here
catch err
    alert err.stack

Gives following stack trace:

ReferenceError: any_error_here is not defined 
  at eval (coffeescript:5:3)

How to get coffeescript line number for this stack (line 2)?

Here is simple html with same question:

<html>
    <head>
        <meta http-equiv="content-type" content="text/html; charset=utf-8" />
        <title>Coffee-script stack trace problems</title>
        <script type="text/javascript" language="javascript" src="http://coffeescript.org/extras/coffee-script.js"></script>
    </head>
    <body id="dt_example">
        <script type="text/coffeescript" language="javascript">
    try
        any_error_here
    catch err
        alert err.message+'\n'+err.stack + '\n\nWhat is coffeescript line number for this stack?'
        </script>
    </body>
</html>

1 Answers1

0

This sounds like a problem for source maps.

When you compile your CoffeeScript, you can generate a source map by specifying the -m option:

coffee -c -m so.coffee

In browsers that support source maps (e.g. Chrome), the CoffeeScript file will appear in the Sources tab, and errors and log statements will refer to the corresponding line in the CoffeeScript file.

Chrome developer console with source-mapped CoffeeScript

For more information, check out the HTML5 tutorial on source maps, and a short article about using source maps with CoffeeScript.

If you have a source map, you can also use it to parse a stack trace string and refer back to the original uncompiled file as outlined in this question.

Community
  • 1
  • 1
rphv
  • 5,409
  • 3
  • 29
  • 47