GithubHelp home page GithubHelp logo

opensourceportfolio / sourcemapped-stacktrace Goto Github PK

View Code? Open in Web Editor NEW

This project forked from novocaine/sourcemapped-stacktrace

0.0 1.0 0.0 204 KB

apply sourcemaps to js stack traces

License: Other

JavaScript 100.00%

sourcemapped-stacktrace's Introduction

This is a simple module for applying source maps to JS stack traces in the browser.

The problem this solves

You have Error.stack() in JS (maybe you're logging a trace, or you're looking at traces in Jasmine or Mocha), and you need to apply a sourcemap so you can understand whats happening because you're using some fancy compiled-to-js thing like coffeescript or traceur. Unfortunately, the browser only applies sourcemaps when the trace is viewed in its console, not to the underlying stack object, so you're out of luck.

Demo

http://novocaine.github.io/sourcemapped-stacktrace/smst.html

Setup

The latest built source is in dist/sourcemapped-stacktrace.js.

Include sourcemapped-stacktrace.js into your page using either an AMD module loader or a plain old script include. As an AMD module it exposes the method 'mapStackTrace'. If an AMD loader is not found this function will be set on window.sourceMappedStackTrace.mapStackTrace.

Build

webpack

API

mapStackTrace(stack, done)

Re-map entries in a stacktrace using sourcemaps if available.

Arguments:

stack: Array of strings from the browser's stack representation. Currently only Chrome format is supported.

done: Callback invoked with the transformed stacktrace an Array of Strings) passed as the first argument

Example

try {
  // break something
  bork();
} catch (e) {
  // pass e.stack to window.mapStackTrace
  window.mapStackTrace(e.stack, function(mappedStack) {
    // do what you want with mappedStack here
    console.log(mappedStack);
  });
}

Longer Explanation

Several modern browsers support sourcemaps when viewing stack traces from errors in their native console, but as of the time of writing there is no support for applying a sourcemap to the (highly non-standardised) Error.prototype.stack. Error.prototype.stack can be used for logging errors and for displaying errors in test frameworks, and it is not very convenient to have unmapped traces in either of those use cases.

This module fetches all the scripts referenced by the stack trace, determines whether they have an applicable sourcemap, fetches the sourcemap from the server, then uses the Mozilla source-map library to do the mapping. Browsers that support sourcemaps don't offer a standardised sourcemap API, so we have to do all that work ourselves.

The nice part about doing it ourselves is that the library could be extended to work in browsers that don't support sourcemaps, which could be good for logging and debugging problems. Currently, only Chrome is supported, but it would be easy to support those formats by ripping off stacktrace.js.

Known issues

  • Doesn't support exception formats of any browser other than Chrome
  • Only supports JS containing //# sourceMappingURL= declarations (i.e. no support for the SourceMap: HTTP header (yet)
  • Some prominent sourcemap generators (including CoffeeScript and Traceur) don't emit a list of 'names' in the source-map, which means that frames from transpiled code will have (unknown) instead of the original function name. Those generators should support this feature better.

sourcemapped-stacktrace's People

Contributors

novocaine avatar jotto avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.