Skip to content

Commit

Permalink
fixing line numbers in sprig editor error handling (#1979)
Browse files Browse the repository at this point in the history
* fixing line numbers in sprig editor error handling

* combining magic numbers + protecting against errors
  • Loading branch information
EerierGosling authored Jul 17, 2024
1 parent f84f08f commit ed5c0a0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
16 changes: 13 additions & 3 deletions src/lib/engine/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ const normalizeStack = (stack: string): StackItem[] | null => {
}

export const normalizeGameError = (gameError: GameError): NormalizedError => {
const lineOffset = 3
const lineOffset = 2
let addedLineCol = false

if (gameError.kind === 'parse') {
const { description, lineNumber, column } = gameError.error
Expand Down Expand Up @@ -113,13 +114,22 @@ export const normalizeGameError = (gameError: GameError): NormalizedError => {

if (fileName && item.lineNumber && item.column) {
descriptionLines.unshift(` at ${item.callSite} (${fileName}:${item.lineNumber}:${item.column})`)
addedLineCol = true
} else if (fileName) {
descriptionLines.unshift(` at ${item.callSite} (${fileName})`)
} else {
descriptionLines.unshift(` at ${item.callSite}`)
}
}

// adds line number to infinite recursion
if (!addedLineCol && line && col && stack[0]) {
descriptionLines.unshift(` at ${stack[0]!.callSite} (index.ts:${line}:${col})`)
} else if (!addedLineCol && line && col) {
// might not always be eval in some edge cases - change if this is the case
descriptionLines.unshift(` at eval (index.ts:${line}:${col})`)
}

descriptionLines.unshift(`${gameError.error.name}: ${gameError.error.message}`)
return {
description: descriptionLines.join('\n'),
Expand All @@ -136,7 +146,7 @@ export const normalizeGameError = (gameError: GameError): NormalizedError => {
* Finds the line and column of innermost error from a stack.
* This is modified code from V1.
*/
function findErrorLineCol(stack: string | undefined): [number | null, number | null] {
function findErrorLineCol(stack: string | undefined, lineOffset: number): [number | null, number | null] {
if (!stack) return [null, null]

let line = null
Expand All @@ -147,7 +157,7 @@ function findErrorLineCol(stack: string | undefined): [number | null, number | n

if (location) {
let lineCol = location[1]!.split(":").map(Number)
line = lineCol[0]! - 2 - 1
line = lineCol[0]! - lineOffset
col = lineCol[1]!
}

Expand Down
11 changes: 9 additions & 2 deletions src/lib/engine/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,20 @@ export function runGame(code: string, canvas: HTMLCanvasElement, onPageError: (e
fn(...Object.values(api))
return { error: null, cleanup }
} catch (error: any) {
// if there's an error code, it's most likely a babel error of some kind
// if there's an error code, it's most likely a babel error of some kind
// other errors do not have an error code attached
if (!error.code) {
const normalizedError = normalizeGameError({ kind: "runtime", error });
normalizedError!.line! += 1;
return { error: normalizedError, cleanup };
}

// At least for SyntaxErrors, the type of error shows as "unknown" instead of "SyntaxError" - this replaces that
if (error.message) {
if (error.message.split(":")[0] == 'unknown') {
error.message = error.message.replace('unknown', error.name)
}
}

return {
error: {
raw: error,
Expand Down

0 comments on commit ed5c0a0

Please sign in to comment.