Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I take a minified javascript stack trace and run it against a source map to get the proper error?

On our production server, I have minified javascript published and I'm not including a map file with it, because I don't want the user to be able to understand what's happening based on the error.

I have a logging service I've written to forward the angular exceptions (caught by $exceptionHandler) to myself via email. However, this stack trace is near unreadable:

n is not defined     at o (http://localhost:9000/build/app.min.js:1:3284)     at new NameController (http://localhost:9000/build/app.min.js:1:3412)     at e (http://localhost:9000/build/bower.min.js:44:193)     at Object.g.instantiate (http://localhost:9000/build/bower.min.js:44:310)     at b.$get (http://localhost:9000/build/bower.min.js:85:313)     at d.compile (http://localhost:9000/build/bower.min.js:321:23333)     at aa (http://localhost:9000/build/bower.min.js:78:90)     at K (http://localhost:9000/build/bower.min.js:67:39)     at g (http://localhost:9000/build/bower.min.js:59:410)     at http://localhost:9000/build/bower.min.js:58:480 <ui-view class="ng-scope"> 

What I'm wondering is: Is there a program where I can analyze this stack trace against the actual non-minified source code via map file (or not via map file if there's another way)

like image 987
Alex Kibler Avatar asked Oct 14 '15 14:10

Alex Kibler


People also ask

How do you use source maps in JavaScript?

To enable source maps in Google Chrome, go to Developer Tools, click the little cog icon, and then make sure that “Enable Javascript source maps” is checked. That's it.

What is stack trace in JavaScript?

A stack trace is a list of the functions, in order, that lead to a given point in a software program. A stack trace is essentially a breadcrumb trail for your software. You can easily see the stack trace in JavaScript by adding the following into your code: console. trace();


1 Answers

What you want to do is parse the source maps. This has nothing to do with web browsers. All you need to do is translate the minified reference into the unminified resource.

If you have any experience with NodeJS there is already a package that does this for you.

https://github.com/mozilla/source-map/

To install the library

npm install -g source-map 

or

yarn global add source-map 

Create a file named "issue.js"

fs = require('fs'); var sourceMap = require('source-map'); var smc = new sourceMap.SourceMapConsumer(fs.readFileSync("./app.min.js.map","utf8")); console.log(smc.originalPositionFor({line: 1, column: 3284})); 

Run the file with node

node issue.js 

It should output the location in the original file to the console for first line from the stack trace.

Note: I tell you install source-map globally for ease of use, but you could create a node project that does what you need and installs it locally.

like image 89
Reactgular Avatar answered Oct 25 '22 23:10

Reactgular