This repository was archived by the owner on Jan 11, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 419
Support better stack trace with sourcemaps #1357
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| import fs from 'fs'; | ||
| import path from 'path'; | ||
| import { SourceMapConsumer, RawSourceMap } from 'source-map'; | ||
|
|
||
| function get_sourcemap_url(contents: string) { | ||
| const reversed = contents | ||
| .split('\n') | ||
| .reverse() | ||
| .join('\n'); | ||
|
|
||
| const match = /\/[/*]#[ \t]+sourceMappingURL=([^\s'"]+?)(?:[ \t]+|$)/gm.exec(reversed); | ||
| if (match) return match[1]; | ||
|
|
||
| return undefined; | ||
| } | ||
|
|
||
| const file_cache = new Map<string, string>(); | ||
|
|
||
| function get_file_contents(path: string) { | ||
| if (file_cache.has(path)) { | ||
| return file_cache.get(path); | ||
| } | ||
|
|
||
| try { | ||
| const data = fs.readFileSync(path, 'utf8'); | ||
| file_cache.set(path, data); | ||
| return data; | ||
| } catch { | ||
| return undefined; | ||
| } | ||
| } | ||
|
|
||
| export function sourcemap_stacktrace(stack: string) { | ||
| const replace = (line: string) => | ||
| line.replace( | ||
| /^ {4}at (?:(.+?)\s+\()?(?:(.+?):(\d+)(?::(\d+))?)\)?/, | ||
| (input, var_name, file_path, line, column) => { | ||
| if (!file_path) return input; | ||
|
|
||
| const contents = get_file_contents(file_path); | ||
| if (!contents) return input; | ||
|
|
||
| const sourcemap_url = get_sourcemap_url(contents); | ||
| if (!sourcemap_url) return input; | ||
|
|
||
| let dir = path.dirname(file_path); | ||
| let sourcemap_data: string; | ||
|
|
||
| if (/^data:application\/json[^,]+base64,/.test(sourcemap_url)) { | ||
| const raw_data = sourcemap_url.slice(sourcemap_url.indexOf(',') + 1); | ||
| try { | ||
| sourcemap_data = Buffer.from(raw_data, 'base64').toString(); | ||
| } catch { | ||
| return input; | ||
| } | ||
| } else { | ||
| const sourcemap_path = path.resolve(dir, sourcemap_url); | ||
| const data = get_file_contents(sourcemap_path); | ||
|
|
||
| if (!data) return input; | ||
|
|
||
| sourcemap_data = data; | ||
| dir = path.dirname(sourcemap_path); | ||
| } | ||
|
|
||
| let raw_sourcemap: RawSourceMap; | ||
| try { | ||
| raw_sourcemap = JSON.parse(sourcemap_data); | ||
| } catch { | ||
| return input; | ||
| } | ||
|
|
||
| const consumer = new SourceMapConsumer(raw_sourcemap); | ||
| const pos = consumer.originalPositionFor({ | ||
| line: Number(line), | ||
| column: Number(column), | ||
| bias: SourceMapConsumer.LEAST_UPPER_BOUND | ||
| }); | ||
|
|
||
| if (!pos.source) return input; | ||
|
|
||
| const source_path = path.resolve(dir, pos.source); | ||
| const source = `${source_path}:${pos.line || 0}:${pos.column || 0}`; | ||
|
|
||
| if (!var_name) return ` at ${source}`; | ||
| return ` at ${var_name} (${source})`; | ||
| } | ||
| ); | ||
|
|
||
| file_cache.clear(); | ||
|
|
||
| return stack | ||
| .split('\n') | ||
| .map(replace) | ||
| .join('\n'); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,3 +15,5 @@ | |
| <h2>{mounted}</h2> | ||
|
|
||
| <p>{error.message}</p> | ||
|
|
||
| <span>{error.stack}</span> | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| export function oops() { | ||
| throw new Error('oops'); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| <script context="module"> | ||
| import { oops } from './_trace'; | ||
|
|
||
| export function preload() { | ||
| oops(); | ||
| } | ||
| </script> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not really sure what the point of the
file_cacheis if it's cleared everytime we try to source map an exceptionThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I noticed that in a stack trace, sometimes you can hit the same file with different lines and cols. The
file_cachethere is just a means to avoid rereading that file multiple times in a single call tosourcemap_stacktrace.