Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SyntaxError stack trace not generated #81

Open
devcorpio opened this issue Jun 15, 2022 · 0 comments
Open

SyntaxError stack trace not generated #81

devcorpio opened this issue Jun 15, 2022 · 0 comments
Labels

Comments

@devcorpio
Copy link

devcorpio commented Jun 15, 2022

Current Behavior

We have detected a problem when generating stack trace from a SyntaxError. This takes place when the error cause is due to a malformed javascript code, the output of the library is not consistent:

  • On chromium browsers it returns a malformed single stack trace element not containing the lineNumber property

Screenshot 2022-06-15 at 14 46 00

  • On non-chromium browsers it throws an exception.

Screenshot 2022-06-15 at 14 49 36

Expected Behavior

We would expect one the following two behaviours, being number 2 the preferred:

  1. Consistency across all browsers. In this case, we would expect the exception on all of them.
  2. Handle this case and return a well formed single stack trace element, for example:

Screenshot 2022-06-15 at 15 31 58

Steps to Reproduce (for bugs)

The code below reproduces the issue:

<html>
    <head>
        <title>Inline errors page test</title>
    </head>
    <body>
        <h1>Inline errors page test</h1>
        <script type="text/javascript" src="error-stack-parser.js"></script>
        <script>
            window.addEventListener("error", (errorEvent) => {
                const stack = ErrorStackParser.parse(errorEvent.error)
                console.log("stack", stack);
            })
        </script>
        <script>
            var a: // the malformed javascript here
        </script>
    </body>
</html>

Important: A SyntaxError generated from a bad usage of a API like JSON.parse does not reproduce the issue

<html>
    <head>
        <title>Inline errors page test</title>
    </head>
    <body>
        <h1>Inline errors page test</h1>
        <script type="text/javascript" src="error-stack-parser.js"></script>
        <script>
            window.addEventListener("error", (errorEvent) => {
                const stack = ErrorStackParser.parse(errorEvent.error);
                console.log("stack", stack);
            })
        </script>
        <script>
            JSON.parse('<I am not a json>') --> wrong input
        </script>
    </body>
</html>

Context

Your Environment

This happens on all versions of the library (including the latest one).
Tested on different OS, such as MacOS, Fedora Linux, and Windows 10

Possible Solution

It seems that the parse function it might be the good place to start with the fix. Presumably, there is new condition to be added or a change on the regex that needs to be done, or both.
On Chromium browsers the line return this.parseFFOrSafari(error); is the one being executed, which is wrong.

On our product we have followed a defensive approach to overcome the issue:

We leverage the information available in ErrorEvent. So, if the library throws an exception or generates a malformed stack trace what we do is to extract lineno, colno, and filename from such event.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

1 participant