From 4e337bf8ac2e06fe5c35ee1f8d4bb50b420aa33f Mon Sep 17 00:00:00 2001 From: Sofia Egan <108308587+EerierGosling@users.noreply.github.com> Date: Wed, 17 Jul 2024 14:58:34 -0400 Subject: [PATCH 1/3] fixing line numbers in sprig editor error handling --- src/lib/engine/error.ts | 14 ++++++++++++-- src/lib/engine/index.ts | 7 ++++++- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/lib/engine/error.ts b/src/lib/engine/error.ts index 065e642051..ef6019f637 100644 --- a/src/lib/engine/error.ts +++ b/src/lib/engine/error.ts @@ -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 @@ -113,6 +114,7 @@ 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 { @@ -120,6 +122,14 @@ export const normalizeGameError = (gameError: GameError): NormalizedError => { } } + // 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'), @@ -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]! - 2 col = lineCol[1]! } diff --git a/src/lib/engine/index.ts b/src/lib/engine/index.ts index 937e82c285..f3dd5ed0e8 100644 --- a/src/lib/engine/index.ts +++ b/src/lib/engine/index.ts @@ -79,9 +79,14 @@ export function runGame(code: string, canvas: HTMLCanvasElement, onPageError: (e // 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.split(":")[0] == 'unknown') { + error.message = error.message.replace('unknown', error.name) + } + return { error: { raw: error, From fa08767be121da9429c3d2e800af69f338d0403f Mon Sep 17 00:00:00 2001 From: Sofia Egan <108308587+EerierGosling@users.noreply.github.com> Date: Wed, 17 Jul 2024 15:22:42 -0400 Subject: [PATCH 2/3] combining magic numbers + protecting against errors --- src/lib/engine/error.ts | 4 ++-- src/lib/engine/index.ts | 8 +++++--- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/lib/engine/error.ts b/src/lib/engine/error.ts index ef6019f637..3c56af212d 100644 --- a/src/lib/engine/error.ts +++ b/src/lib/engine/error.ts @@ -146,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 @@ -157,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 + line = lineCol[0]! - lineOffset col = lineCol[1]! } diff --git a/src/lib/engine/index.ts b/src/lib/engine/index.ts index f3dd5ed0e8..c162ca60a2 100644 --- a/src/lib/engine/index.ts +++ b/src/lib/engine/index.ts @@ -75,7 +75,7 @@ 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 }); @@ -83,8 +83,10 @@ export function runGame(code: string, canvas: HTMLCanvasElement, onPageError: (e } // At least for SyntaxErrors, the type of error shows as "unknown" instead of "SyntaxError" - this replaces that - if (error.message.split(":")[0] == 'unknown') { - error.message = error.message.replace('unknown', error.name) + if (error.message) { + if (error.message.split(":")[0] == 'unknown') { + error.message = error.message.replace('unknown', error.name) + } } return { From 88aa2333f3fcbd715b2862becefd25b679c764f3 Mon Sep 17 00:00:00 2001 From: Sofia Egan <108308587+EerierGosling@users.noreply.github.com> Date: Thu, 18 Jul 2024 11:02:11 -0400 Subject: [PATCH 3/3] update to github action for unit tests --- .github/workflows/typescript_check.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/typescript_check.yml b/.github/workflows/typescript_check.yml index a6f73cbdf3..00152aa2ec 100644 --- a/.github/workflows/typescript_check.yml +++ b/.github/workflows/typescript_check.yml @@ -2,7 +2,7 @@ name: TypeScript Check on: pull_request: - types: [opened, reopened, edited] + types: [opened, reopened, edited, synchronize] jobs: # Test job