Skip to content

Commit ca5cd75

Browse files
committed
Add fix for throws without the usercode in the stack
1 parent 75813f7 commit ca5cd75

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

src/UserCodeRunner.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import path from 'path';
44
import { defaultErrorCodeMessageMappers } from './defaultErrorCodeMessageMappers.js';
55
import { createMapDiagnosticMessage } from './utils/errorMessageMapping.js';
66
import ts from 'typescript';
7-
import { parse } from 'stack-trace';
7+
import { parse, StackFrame } from 'stack-trace';
88
import { BasicSourceMapConsumer, IndexedSourceMapConsumer, SourceMapConsumer } from 'source-map';
99
import LRUCache from 'lru-cache';
1010
import { Result } from './utils/monads.js';
@@ -348,21 +348,27 @@ export class UserCodeRuntimeError extends UserCodeError {
348348
private readonly error: Error;
349349
private readonly sourceMap: SourceMapConsumer;
350350
private readonly tsFileCache: Map<string, ts.SourceFile>;
351+
private readonly stackFrames: StackFrame[];
351352

352353
protected constructor(error: Error, sourceMap: SourceMapConsumer, tsFileCache: Map<string, ts.SourceFile>) {
353354
super();
354355
this.error = error;
355356
this.sourceMap = sourceMap;
356357
this.tsFileCache = tsFileCache;
358+
this.stackFrames = parse(this.error);
359+
const userCodeFrame = this.stackFrames.find(frame => frame.getFileName() === USER_CODE_FILENAME);
360+
if (userCodeFrame === undefined) {
361+
this.error.message = 'Error: Runtime error detected outside of user code execution path. This is most likely a bug in the additional library source.\nInherited from:\n' + this.error.message;
362+
throw this.error;
363+
}
357364
}
358365

359366
public get message(): string {
360367
return 'Error: ' + this.error.message;
361368
}
362369

363370
public get stack(): string {
364-
const stack = parse(this.error);
365-
const stackWithoutHarness = stack
371+
const stackWithoutHarness = this.stackFrames
366372
.filter(callSite => callSite.getFileName()?.endsWith(USER_CODE_FILENAME))
367373
.filter(callSite => {
368374
if (callSite.getFileName() === undefined) {
@@ -390,10 +396,7 @@ export class UserCodeRuntimeError extends UserCodeError {
390396

391397
public get location(): { line: number; column: number } {
392398
const stack = parse(this.error);
393-
const userFileStackFrame = stack.find(callSite => callSite.getFileName() === USER_CODE_FILENAME);
394-
if (userFileStackFrame === undefined) {
395-
throw new Error('Runtime error detected outside of user code execution path. This is most likely a bug in the additional library source.');
396-
}
399+
const userFileStackFrame = stack.find(callSite => callSite.getFileName() === USER_CODE_FILENAME)!;
397400
const originalPosition = this.sourceMap.originalPositionFor({
398401
line: userFileStackFrame.getLineNumber()!,
399402
column: userFileStackFrame.getColumnNumber()!,

0 commit comments

Comments
 (0)