| 229 | } |
| 230 | |
| 231 | export function formatError(e: any, code: string) { |
| 232 | let stackTrace = |
| 233 | "" + (typeof e === "object" && e !== null && "stack" in e ? e.stack : ""); |
| 234 | let irrelevantStackIdx = stackTrace |
| 235 | .split("\n") |
| 236 | .findIndex((line) => line.includes("doNotWriteAnotherFunctionCalledThis")); |
| 237 | if (irrelevantStackIdx === -1) { |
| 238 | irrelevantStackIdx = stackTrace |
| 239 | .split("\n") |
| 240 | .findIndex((line) => line.includes("execute-code-2")); |
| 241 | } |
| 242 | return stackTrace |
| 243 | .split("\n") |
| 244 | .slice(0, irrelevantStackIdx) |
| 245 | .map((line) => |
| 246 | line.replace( |
| 247 | /eval at <anonymous>.*<anonymous>:(\d{1,3}):(\d{1,4})/g, |
| 248 | (_, lineNumStr, charNumStr) => { |
| 249 | const errLineNum = parseInt(lineNumStr); |
| 250 | const aiCodeStartLineNum = |
| 251 | code |
| 252 | .split("\n") |
| 253 | .findIndex((line) => line === "// AI code starts below") + 1; |
| 254 | return `line ${errLineNum - aiCodeStartLineNum}, char ${charNumStr}`; |
| 255 | }, |
| 256 | ), |
| 257 | ) |
| 258 | .join("\n"); |
| 259 | } |