Skip to content

Add line numbers to errors#322

Open
Andrew-Bonilla wants to merge 3 commits into
hydra-synth:mainfrom
Andrew-Bonilla:add-line-numbers-to-errors
Open

Add line numbers to errors#322
Andrew-Bonilla wants to merge 3 commits into
hydra-synth:mainfrom
Andrew-Bonilla:add-line-numbers-to-errors

Conversation

@Andrew-Bonilla
Copy link
Copy Markdown

Problem: When a user runs code and an error occurs (syntax, nonexistent function), they are not given the line number of the issue. This can be a problem for live coding scenarios where the user is adding several lines all at once or is picking up off of incomplete code from a previous performer.

Solution: For runtime errors, parse the line number from the stack trace via err.stack. For syntax errors, use acorn (since it's already a project dependency) to parse the code before evaluation, giving the line number. The acorn error messages can sometimes be imprecise about which token/item is reported (reporting a space character ' ' instead of the symbol). Improving the quality of syntax error messages could be a future PR. I think it might require replacing acorn with something else. I didn't do this here because it seemed like too large of a change for this PR. Line numbers for syntax errors are still accurate even if the message text is not at times. It's also worth noting that runtime errors display as "message (line N)" while syntax errors follow acorn's format which includes the column position, for instance, "Unexpected token (3:5)". The column info is useful for finding the exact error location, but can be formatted out.

A utils directory and file src/utils/error-utils.js are now implemented to keep the logic in one place, used by both repl-v2.js and store.js.

Note: views/editor/repl.js appears to be unused legacy code, but I updated the error formatting calls to fit the new functionality.

The acorn pre-parse was rejecting valid sketches that use top-level
await (e.g. await loadScript for extensions), because acorn parses
with script semantics by default. The actual eval wraps the code in
an async IIFE, so these sketches run fine — only the pre-check was
failing.

Pass allowAwaitOutsideFunction: true so the syntax check matches
what's actually executed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant