Search code examples
javascripterror-handlingcompressionminimization

Catch JavaScript errors from compressed files


I catch JS errors by subscribing to window.onerror event, so if someone catches 'undefined variable' error, I send it to server for debugging purposes. As you know this event return 'message of error', 'url' and 'line' where error occurred.

The problem are in compressed files.

If file is compressed, all the code goes in one line and it's big problem to determine the exact place of error.

Is there any solution for this problem?


Solution

  • JavaScript compressors usually do two things:

    1. Remove all unneccessary white-space (“unneccessary” in terms of syntactical validaty).
    2. shorten variable names, if possible. This applies to local variables, i.e. those which are not in the global scope or members of an object.

    There are some other optimizations, such as function inlining, but these are usually not so problematic.

    As for the first point, you can run the code through one of the many JavaScript source formatters. This should give you a pretty readable JavaScript file.

    As for the second point, the obfuscation is usually not reversible. If “speaking” variable names like width, height or whatever have been changed to a or b, you cannot know what they were meant to express in the first place.

    If this problem applies to an open source product, you can usually download the sources and try to reconstruct the problem with them.

    If it's closed source, and only “beautifying” the code doesn't help, you have to write a bug report to the vendor.