Skip to content

Commit 16c4f5b

Browse files
committed
refactor: use Promise.allSettled() for better error isolation
Related #5749
1 parent 7cfbbf7 commit 16c4f5b

File tree

1 file changed

+37
-20
lines changed

1 file changed

+37
-20
lines changed

packages/core/src/utils/memoryDiscovery.ts

Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ interface GeminiFileContent {
3838
content: string | null;
3939
}
4040

41+
4142
async function findProjectRoot(startDir: string): Promise<string | null> {
4243
let currentDir = path.resolve(startDir);
4344
while (true) {
@@ -103,25 +104,30 @@ async function getGeminiMdFilePathsInternal(
103104

104105
for (let i = 0; i < dirsArray.length; i += CONCURRENT_LIMIT) {
105106
const batch = dirsArray.slice(i, i + CONCURRENT_LIMIT);
106-
const batchPromises = batch.map(async (dir) => {
107-
try {
108-
return await getGeminiMdFilePathsInternalForEachDir(
109-
dir,
110-
userHomePath,
111-
debugMode,
112-
fileService,
113-
extensionContextFilePaths,
114-
fileFilteringOptions,
115-
maxDirs,
116-
);
117-
} catch (error) {
107+
const batchPromises = batch.map((dir) =>
108+
getGeminiMdFilePathsInternalForEachDir(
109+
dir,
110+
userHomePath,
111+
debugMode,
112+
fileService,
113+
extensionContextFilePaths,
114+
fileFilteringOptions,
115+
maxDirs,
116+
)
117+
);
118+
119+
const batchResults = await Promise.allSettled(batchPromises);
120+
121+
for (const result of batchResults) {
122+
if (result.status === 'fulfilled') {
123+
pathsArrays.push(result.value);
124+
} else {
125+
const error = result.reason;
118126
const message = error instanceof Error ? error.message : String(error);
119-
logger.error(`Error discovering files in directory ${dir}: ${message}`);
120-
return []; // Return an empty array to allow other directories to be processed
127+
logger.error(`Error discovering files in directory: ${message}`);
128+
// Continue processing other directories
121129
}
122-
});
123-
const batchResults = await Promise.all(batchPromises);
124-
pathsArrays.push(...batchResults);
130+
}
125131
}
126132

127133
const paths = pathsArrays.flat();
@@ -248,7 +254,7 @@ async function readGeminiMdFiles(
248254

249255
for (let i = 0; i < filePaths.length; i += CONCURRENT_LIMIT) {
250256
const batch = filePaths.slice(i, i + CONCURRENT_LIMIT);
251-
const batchPromises = batch.map(async (filePath) => {
257+
const batchPromises = batch.map(async (filePath): Promise<GeminiFileContent> => {
252258
try {
253259
const content = await fs.readFile(filePath, 'utf-8');
254260

@@ -282,8 +288,19 @@ async function readGeminiMdFiles(
282288
}
283289
});
284290

285-
const batchResults = await Promise.all(batchPromises);
286-
results.push(...batchResults);
291+
const batchResults = await Promise.allSettled(batchPromises);
292+
293+
for (const result of batchResults) {
294+
if (result.status === 'fulfilled') {
295+
results.push(result.value);
296+
} else {
297+
// This case shouldn't happen since we catch all errors above,
298+
// but handle it for completeness
299+
const error = result.reason;
300+
const message = error instanceof Error ? error.message : String(error);
301+
logger.error(`Unexpected error processing file: ${message}`);
302+
}
303+
}
287304
}
288305

289306
return results;

0 commit comments

Comments
 (0)