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

Exceptions from parsing malformed source maps are not caught by promise #62

Open
3bl3gamer opened this issue Apr 12, 2020 · 2 comments
Open

Comments

@3bl3gamer
Copy link

3bl3gamer commented Apr 12, 2020

If _parseJson (and internal JSON.parse) throws an error, it will not be passed to reject, so outer getMappedLocation (and StackTrace.fromError from stacktrace-js) will never resolve/reject.

https://github.com/stacktracejs/stacktrace-gps/blob/v3.0.4/stacktrace-gps.js#L240-L251

var sourceMapConsumerPromise = new Promise(function(resolve, reject) {
    return this._get(sourceMappingURL).then(function(sourceMapSource) {
        if (typeof sourceMapSource === 'string') {
            // throws from here vvv
            sourceMapSource = _parseJson(sourceMapSource.replace(/^\)\]\}'/, ''));
        }
        if (typeof sourceMapSource.sourceRoot === 'undefined') {
            sourceMapSource.sourceRoot = defaultSourceRoot;
        }

        resolve(new SourceMap.SourceMapConsumer(sourceMapSource));
    }, reject); //<- not caught in here
}.bind(this));

Expected Behavior

Invalid source map errors can be caught by StackTrace.fromError(...).catch an others.

Current Behavior

Error becomes unhandled rejection and can not be caught.

Steps to Reproduce

Try to getMappedLocation for source map with some invalid JSON content (for example <html).

Context

I'm trying to catch every possible error with addEventListener('error' and addEventListener('unhandledrejection', enhance stack trace and send it to server. Currently, server is configured to respond with index.html for non-exiting paths. If .map file is missing for some reason, stacktrace-js receives some HTML instead of JSON, fails and no error is sent at all.

What is worse, this unhandled rejection is caught by 'unhandledrejection' handler and passed it to StackTrace.fromError again, which results in an infinite loop.

Your Environment

  • stacktrace.js version: 2.0.2
  • Browser Name and version: Chromium 80.0.3987.116
  • Operating System and version: Arch Linux (desktop)
  • Link to your project: it is private

Possible Solution

- return this._get(sourceMappingURL).then(..., reject);
+ return this._get(sourceMappingURL).then(...).catch(reject);
@niftylettuce
Copy link
Contributor

PR welcome

@3bl3gamer
Copy link
Author

I was checking latest version (v3.0.4), but it looks like bug was already fixed two days before in master 4642d8c#diff-a09efda47d18a7651ae9897eecf7da20

So, I guess, this issue may be closed when new release comes out.

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

No branches or pull requests

2 participants