fix(Code Node): Error formatting fix (#16719)

This commit is contained in:
Michael Kret
2025-06-27 12:18:26 +03:00
committed by GitHub
parent 0775fd859e
commit 8f9ce72dc4
2 changed files with 78 additions and 7 deletions

View File

@@ -28,15 +28,16 @@ export class ExecutionError extends ApplicationError {
* Populate error `message` and `description` from error `stack`. * Populate error `message` and `description` from error `stack`.
*/ */
private populateFromStack() { private populateFromStack() {
const stackRows = this.stack.split('\n'); const stackRows = this.stack && typeof this.stack === 'string' ? this.stack.split('\n') : [];
if (stackRows.length === 0) { if (stackRows.length === 0) {
this.message = 'Unknown error'; this.message = 'Unknown error';
return;
} }
const messageRow = stackRows.find((line) => line.includes('Error:')); const messageRow = stackRows.find((line) => line.includes('Error:'));
const lineNumberRow = stackRows.find((line) => line.includes('Code:')); const lineNumberRow = stackRows.find((line) => line.includes('Code:'));
const lineNumberDisplay = this.toLineNumberDisplay(lineNumberRow); const lineNumberDisplay = this.toLineNumberDisplay(lineNumberRow) || '';
if (!messageRow) { if (!messageRow) {
this.message = `Unknown error ${lineNumberDisplay}`; this.message = `Unknown error ${lineNumberDisplay}`;
@@ -52,7 +53,7 @@ export class ExecutionError extends ApplicationError {
return; return;
} }
this.message = `${errorDetails} ${lineNumberDisplay}`; this.message = `${errorDetails} ${lineNumberDisplay}`.trim();
} }
private toLineNumberDisplay(lineNumberRow?: string) { private toLineNumberDisplay(lineNumberRow?: string) {
@@ -74,10 +75,16 @@ export class ExecutionError extends ApplicationError {
private toErrorDetailsAndType(messageRow?: string) { private toErrorDetailsAndType(messageRow?: string) {
if (!messageRow) return [null, null]; if (!messageRow) return [null, null];
const [errorDetails, errorType] = messageRow // Remove "Error: " prefix added by stacktrace formatting
.split(':') messageRow = messageRow.replace(/^Error: /, '');
.reverse()
.map((i) => i.trim()); const colonIndex = messageRow.indexOf(': ');
if (colonIndex === -1) {
return [messageRow.trim(), null];
}
const errorType = messageRow.substring(0, colonIndex).trim();
const errorDetails = messageRow.substring(colonIndex + 2).trim();
return [errorDetails, errorType === 'Error' ? null : errorType]; return [errorDetails, errorType === 'Error' ? null : errorType];
} }

View File

@@ -0,0 +1,64 @@
import { ExecutionError } from '../ExecutionError';
describe('ExecutionError', () => {
describe('constructor', () => {
it('should set message to "Unknown error" when stack is empty', () => {
const error = new Error('test');
error.stack = '';
const executionError = new ExecutionError(error);
expect(executionError.message).toBe('Unknown error');
});
it('should extract error details and type from stack', () => {
const error = new Error('ErrorType: Error Details');
error.stack = 'Error: ErrorType: Error Details\n at Code:123';
const executionError = new ExecutionError(error);
expect(executionError.message).toBe('Error Details [line 123]');
expect(executionError.description).toBe('ErrorType');
});
it('should extract error details when no error type is present', () => {
const error = new Error('Error Details');
error.stack = 'Error: Error Details\n at Code:123';
const executionError = new ExecutionError(error);
expect(executionError.message).toBe('Error Details [line 123]');
expect(executionError.description).toBe(null);
});
it('should handle stack with only "Error: " prefix', () => {
const error = new Error('Error: ');
error.stack = 'Error: Error: \n at Code:123';
const executionError = new ExecutionError(error);
expect(executionError.message).toBe('Unknown error [line 123]');
expect(executionError.description).toBe(null);
});
it('should handle stack with colon and space', () => {
const error = new Error(': ');
error.stack = 'Error: : \n at Code:123';
const executionError = new ExecutionError(error);
expect(executionError.message).toBe('Unknown error [line 123]');
expect(executionError.description).toBe(null);
});
it('should handle itemIndex', () => {
const error = new Error('ErrorType: Error Details');
error.stack = 'Error: ErrorType: Error Details\n at Code:123';
const executionError = new ExecutionError(error, 1);
expect(executionError.message).toBe('Error Details [line 123, for item 1]');
expect(executionError.description).toBe('ErrorType');
expect(executionError.itemIndex).toBe(1);
expect(executionError.context).toEqual({ itemIndex: 1 });
});
it('should handle stack without line number', () => {
const error = new Error('ErrorType: Error Details');
error.stack = 'Error: ErrorType: Error Details';
const executionError = new ExecutionError(error, 1);
expect(executionError.message).toBe('Error Details');
expect(executionError.description).toBe('ErrorType');
expect(executionError.itemIndex).toBe(1);
expect(executionError.context).toEqual({ itemIndex: 1 });
});
});
});