fix: Remove fallback subtasks in parseSubtasksFromText to properly throw errors on invalid input

This commit is contained in:
Ralph Khreish
2025-04-09 10:22:16 +02:00
parent 5e01399dca
commit 42bf897f81
2 changed files with 73 additions and 100 deletions

View File

@@ -873,13 +873,23 @@ Note on dependencies: Subtasks can depend on other subtasks with lower IDs. Use
* @param {number} expectedCount - Expected number of subtasks * @param {number} expectedCount - Expected number of subtasks
* @param {number} parentTaskId - Parent task ID * @param {number} parentTaskId - Parent task ID
* @returns {Array} Parsed subtasks * @returns {Array} Parsed subtasks
* @throws {Error} If parsing fails or JSON is invalid
*/ */
function parseSubtasksFromText(text, startId, expectedCount, parentTaskId) { function parseSubtasksFromText(text, startId, expectedCount, parentTaskId) {
try { // Set default values for optional parameters
startId = startId || 1;
expectedCount = expectedCount || 2; // Default to 2 subtasks if not specified
// Handle empty text case
if (!text || text.trim() === '') {
throw new Error('Empty text provided, cannot parse subtasks');
}
// Locate JSON array in the text // Locate JSON array in the text
const jsonStartIndex = text.indexOf('['); const jsonStartIndex = text.indexOf('[');
const jsonEndIndex = text.lastIndexOf(']'); const jsonEndIndex = text.lastIndexOf(']');
// If no valid JSON array found, throw error
if ( if (
jsonStartIndex === -1 || jsonStartIndex === -1 ||
jsonEndIndex === -1 || jsonEndIndex === -1 ||
@@ -890,17 +900,19 @@ function parseSubtasksFromText(text, startId, expectedCount, parentTaskId) {
// Extract and parse the JSON // Extract and parse the JSON
const jsonText = text.substring(jsonStartIndex, jsonEndIndex + 1); const jsonText = text.substring(jsonStartIndex, jsonEndIndex + 1);
let subtasks = JSON.parse(jsonText); let subtasks;
// Validate try {
subtasks = JSON.parse(jsonText);
} catch (parseError) {
throw new Error(`Failed to parse JSON: ${parseError.message}`);
}
// Validate array
if (!Array.isArray(subtasks)) { if (!Array.isArray(subtasks)) {
throw new Error('Parsed content is not an array'); throw new Error('Parsed content is not an array');
} }
// Set default values for optional parameters
startId = startId || 1;
expectedCount = expectedCount || subtasks.length;
// Log warning if count doesn't match expected // Log warning if count doesn't match expected
if (expectedCount && subtasks.length !== expectedCount) { if (expectedCount && subtasks.length !== expectedCount) {
log( log(
@@ -941,11 +953,6 @@ function parseSubtasksFromText(text, startId, expectedCount, parentTaskId) {
}); });
return subtasks; return subtasks;
} catch (error) {
log('error', `Error parsing subtasks: ${error.message}`);
// Re-throw the error to be handled by the caller
throw error;
}
} }
/** /**

View File

@@ -196,29 +196,12 @@ These subtasks will help you implement the parent task efficiently.`;
expect(result[2].dependencies).toEqual([1, 2]); expect(result[2].dependencies).toEqual([1, 2]);
}); });
test('should create fallback subtasks for empty text', () => { test('should throw an error for empty text', () => {
const emptyText = ''; const emptyText = '';
const result = parseSubtasksFromText(emptyText, 1, 2, 5); expect(() => parseSubtasksFromText(emptyText, 1, 2, 5)).toThrow(
'Empty text provided, cannot parse subtasks'
// Verify fallback subtasks structure );
expect(result).toHaveLength(2);
expect(result[0]).toMatchObject({
id: 1,
title: 'Subtask 1',
description: 'Auto-generated fallback subtask',
status: 'pending',
dependencies: [],
parentTaskId: 5
});
expect(result[1]).toMatchObject({
id: 2,
title: 'Subtask 2',
description: 'Auto-generated fallback subtask',
status: 'pending',
dependencies: [],
parentTaskId: 5
});
}); });
test('should normalize subtask IDs', () => { test('should normalize subtask IDs', () => {
@@ -272,29 +255,12 @@ These subtasks will help you implement the parent task efficiently.`;
expect(typeof result[1].dependencies[0]).toBe('number'); expect(typeof result[1].dependencies[0]).toBe('number');
}); });
test('should create fallback subtasks for invalid JSON', () => { test('should throw an error for invalid JSON', () => {
const text = `This is not valid JSON and cannot be parsed`; const text = `This is not valid JSON and cannot be parsed`;
const result = parseSubtasksFromText(text, 1, 2, 5); expect(() => parseSubtasksFromText(text, 1, 2, 5)).toThrow(
'Could not locate valid JSON array in the response'
// Verify fallback subtasks structure );
expect(result).toHaveLength(2);
expect(result[0]).toMatchObject({
id: 1,
title: 'Subtask 1',
description: 'Auto-generated fallback subtask',
status: 'pending',
dependencies: [],
parentTaskId: 5
});
expect(result[1]).toMatchObject({
id: 2,
title: 'Subtask 2',
description: 'Auto-generated fallback subtask',
status: 'pending',
dependencies: [],
parentTaskId: 5
});
}); });
}); });