fix(core): Fix task runner error report from user-defined function (#13706)

This commit is contained in:
Iván Ovejero 2025-03-05 16:04:09 +01:00 committed by GitHub
parent 3cd34b5af6
commit 9bedd87744
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 61 additions and 18 deletions

View file

@ -1436,4 +1436,42 @@ describe('JsTaskRunner', () => {
expect(Duration.fromObject({ hours: 1 }).maliciousKey).toBeUndefined(); expect(Duration.fromObject({ hours: 1 }).maliciousKey).toBeUndefined();
}); });
}); });
describe('stack trace', () => {
it('should extract correct line number from user-defined function stack trace', async () => {
const runner = createRunnerWithOpts({});
const taskId = '1';
const task = newTaskState(taskId);
const taskSettings: JSExecSettings = {
code: 'function my_function() {\n null.map();\n}\nmy_function();\nreturn []',
nodeMode: 'runOnceForAllItems',
continueOnFail: false,
workflowMode: 'manual',
};
runner.runningTasks.set(taskId, task);
const sendSpy = jest.spyOn(runner.ws, 'send').mockImplementation(() => {});
jest.spyOn(runner, 'sendOffers').mockImplementation(() => {});
jest
.spyOn(runner, 'requestData')
.mockResolvedValue(newDataRequestResponse([wrapIntoJson({ a: 1 })]));
await runner.receivedSettings(taskId, taskSettings);
expect(sendSpy).toHaveBeenCalled();
const calledWith = sendSpy.mock.calls[0][0] as string;
expect(typeof calledWith).toBe('string');
const calledObject = JSON.parse(calledWith);
expect(calledObject).toEqual({
type: 'runner:taskerror',
taskId,
error: {
stack: expect.any(String),
message: expect.stringContaining('Cannot read properties of null'),
description: 'TypeError',
lineNumber: 2, // from user-defined function
},
});
});
});
}); });

View file

@ -1,8 +1,6 @@
import type { ErrorLike } from './error-like'; import type { ErrorLike } from './error-like';
import { SerializableError } from './serializable-error'; import { SerializableError } from './serializable-error';
const VM_WRAPPER_FN_NAME = 'VmCodeWrapper';
export class ExecutionError extends SerializableError { export class ExecutionError extends SerializableError {
description: string | null = null; description: string | null = null;
@ -42,8 +40,7 @@ export class ExecutionError extends SerializableError {
} }
const messageRow = stackRows.find((line) => line.includes('Error:')); const messageRow = stackRows.find((line) => line.includes('Error:'));
const lineNumberRow = stackRows.find((line) => line.includes(`at ${VM_WRAPPER_FN_NAME} `)); const lineNumberDisplay = this.toLineNumberDisplay(stackRows);
const lineNumberDisplay = this.toLineNumberDisplay(lineNumberRow);
if (!messageRow) { if (!messageRow) {
this.message = `Unknown error ${lineNumberDisplay}`; this.message = `Unknown error ${lineNumberDisplay}`;
@ -62,26 +59,34 @@ export class ExecutionError extends SerializableError {
this.message = `${errorDetails} ${lineNumberDisplay}`; this.message = `${errorDetails} ${lineNumberDisplay}`;
} }
private toLineNumberDisplay(lineNumberRow?: string) { private toLineNumberDisplay(stackRows: string[]) {
if (!lineNumberRow) return ''; if (!stackRows || stackRows.length === 0) return '';
// TODO: This doesn't work if there is a function definition in the code const userFnLine = stackRows.find(
// and the error is thrown from that function. (row) => row.match(/\(evalmachine\.<anonymous>:\d+:\d+\)/) && !row.includes('VmCodeWrapper'),
const regex = new RegExp(
`at ${VM_WRAPPER_FN_NAME} \\(evalmachine\\.<anonymous>:(?<lineNumber>\\d+):`,
); );
const errorLineNumberMatch = lineNumberRow.match(regex);
if (!errorLineNumberMatch?.groups?.lineNumber) return null;
const lineNumber = errorLineNumberMatch.groups.lineNumber; if (userFnLine) {
if (!lineNumber) return ''; const match = userFnLine.match(/evalmachine\.<anonymous>:(\d+):/);
if (match) this.lineNumber = Number(match[1]);
}
this.lineNumber = Number(lineNumber); if (this.lineNumber === undefined) {
const topLevelLine = stackRows.find(
(row) => row.includes('VmCodeWrapper') && row.includes('evalmachine.<anonymous>'),
);
if (topLevelLine) {
const match = topLevelLine.match(/evalmachine\.<anonymous>:(\d+):/);
if (match) this.lineNumber = Number(match[1]);
}
}
if (this.lineNumber === undefined) return '';
return this.itemIndex === undefined return this.itemIndex === undefined
? `[line ${lineNumber}]` ? `[line ${this.lineNumber}]`
: `[line ${lineNumber}, for item ${this.itemIndex}]`; : `[line ${this.lineNumber}, for item ${this.itemIndex}]`;
} }
private toErrorDetailsAndType(messageRow?: string) { private toErrorDetailsAndType(messageRow?: string) {