forked from hackclub/sprig
-
Notifications
You must be signed in to change notification settings - Fork 1
/
error.ts
173 lines (149 loc) · 4.69 KB
/
error.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import type { NormalizedError } from '../../state'
export type EsprimaError = Error & {
description: string
index: number
lineNumber: number
column: number
}
export type GameError =
| { kind: 'parse', error: EsprimaError }
| { kind: 'runtime', error: unknown }
| { kind: 'page', error: unknown }
interface StackItem {
callSite: string
fileUrl: string
lineNumber: number | null
column: number | null
}
const firefoxStackRegex = /^(.*)@(.*?)(:(\d+):(\d+))?$/
const chromeStackRegex = /^\s+at ([^()]+) \((.+?):(\d+):(\d+)\)$/
const chromeStackOnlyUrlRegex = /^\s+at (.+?):(\d+):(\d+)$/
const chromeAsRegex = /\[as (.+)\]/
const chromeStackUrlRegex = /\((.+?):(\d+):(\d+)\)/
const normalizeStack = (stack: string): StackItem[] | null => {
const lines = stack.trim().split('\n')
if (lines.every(line => firefoxStackRegex.test(line))) {
return lines.map(line => {
const match = line.match(firefoxStackRegex)!
return {
callSite: match[1]!.split('/<')[0]! || '<unknown>',
fileUrl: match[2]!.split(' ')[0]!,
lineNumber: match[4] ? Number(match[4]) : null,
column: match[5] ? Number(match[5]) : null
}
})
} else if (lines.slice(1).every(
line => chromeStackRegex.test(line) || chromeStackOnlyUrlRegex.test(line)
)) {
lines.shift()
return lines.map(line => {
const match = line.match(chromeStackRegex)
if (match) {
let callSite = match[1]!
const as = callSite.match(chromeAsRegex)
if (as) callSite = as[1]!
callSite = callSite.split('.').at(-1)!
let fileUrl = match[2]!
while (chromeStackUrlRegex.test(fileUrl))
fileUrl = fileUrl.match(chromeStackUrlRegex)![1]!
return {
callSite,
fileUrl,
lineNumber: Number(match[3]),
column: Number(match[4])
}
}
const match2 = line.match(chromeStackOnlyUrlRegex)!
return {
callSite: '<unknown>',
fileUrl: match2[1]!,
lineNumber: Number(match2[2]),
column: Number(match2[3])
}
})
} else {
return null
}
}
export const normalizeGameError = (gameError: GameError): NormalizedError => {
const lineOffset = 3
if (gameError.kind === 'parse') {
const { description, lineNumber, column } = gameError.error
const line = lineNumber - 1
return {
description: `SyntaxError: ${description}\n at <game>:${line}:${column}`,
raw: gameError.error,
line: line,
column: column
}
} else if (gameError.error instanceof Error) {
const descriptionLines: string[] = []
const stack = (gameError.error.stack ? normalizeStack(gameError.error.stack) : null) ?? []
let [line, col] = findErrorLineCol(gameError.error.stack)
stack.reverse()
let foundEval = false
for (const item of stack) {
if (!foundEval && ['eval', 'anonymous'].includes(item.callSite)) {
foundEval = true
if (item.lineNumber) item.lineNumber -= lineOffset
}
if (!foundEval) continue
let fileName
try {
const url = new URL(item.fileUrl)
fileName = url.pathname.split('/').at(-1)!
} catch {
fileName = item.fileUrl
}
if (fileName && item.lineNumber && item.column) {
descriptionLines.unshift(` at ${item.callSite} (${fileName}:${item.lineNumber}:${item.column})`)
} else if (fileName) {
descriptionLines.unshift(` at ${item.callSite} (${fileName})`)
} else {
descriptionLines.unshift(` at ${item.callSite}`)
}
}
descriptionLines.unshift(`${gameError.error.name}: ${gameError.error.message}`)
return {
description: descriptionLines.join('\n'),
raw: gameError.error,
line: line,
column: col
}
} else {
return { description: `Runtime Error: ${gameError.error}`, raw: gameError.error }
}
}
function findErrorLineCol(stack: string | undefined): [number | null, number | null] {
if (!stack) return [null, null]
let line = null
let col = null
let location = stack.match(/<anonymous>:(.+)\)/)
if (location) {
let lineCol = location[1].split(":").map(Number)
line = lineCol[0] - 2 - 1
col = lineCol[1]
}
return [line, col]
}
export function highlightError(line: number) {
console.log(line)
const cmLineGutters = document.querySelectorAll(".cm-lineNumbers > .cm-gutterElement")
for (let i = 0; i < cmLineGutters.length; i++) {
const cmLineGutter = cmLineGutters[i] as HTMLElement;
const innerNumber = cmLineGutter.innerText;
const height = cmLineGutter.style.height;
if (Number(innerNumber) !== line || height === "0px") {
cmLineGutter.removeAttribute("err-line");
continue;
};
cmLineGutter.setAttribute("err-line", "");
}
}
export function clearErrorHighlight() {
const cmLineGutters = document.querySelectorAll(".cm-lineNumbers > .cm-gutterElement")
for (let i = 0; i < cmLineGutters.length; i++) {
const cmLineGutter = cmLineGutters[i] as HTMLElement;
cmLineGutter.removeAttribute("err-line");
}
}