diff options
| author | Taylor Mullen <[email protected]> | 2025-04-18 13:37:51 -0400 |
|---|---|---|
| committer | N. Taylor Mullen <[email protected]> | 2025-04-18 14:02:09 -0400 |
| commit | 7cd3b95317c4d9263e514f33589cb359766d463b (patch) | |
| tree | ccca1f6d7e67e91c9a3603dd5251fa2c4c577274 /packages/cli/src/tools/grep.tool.ts | |
| parent | 93fd6a9160d4654baf2f10269ce9689c553bb8cf (diff) | |
Fix linting errors in a number of core and tool files (partial)
- As part of this work I also started building out errors.ts which will be a cumulation of error helpers to better handle the challenging `catch (error: unknown)` requirement.
- More changes are to come, this is truly a partial change in order to not disrupt as many people as possible.
Part of https://b.corp.google.com/issues/411384603
Diffstat (limited to 'packages/cli/src/tools/grep.tool.ts')
| -rw-r--r-- | packages/cli/src/tools/grep.tool.ts | 46 |
1 files changed, 21 insertions, 25 deletions
diff --git a/packages/cli/src/tools/grep.tool.ts b/packages/cli/src/tools/grep.tool.ts index 788fc76d..1c334a09 100644 --- a/packages/cli/src/tools/grep.tool.ts +++ b/packages/cli/src/tools/grep.tool.ts @@ -7,6 +7,7 @@ import fastGlob from 'fast-glob'; // Used for JS fallback file searching import { BaseTool, ToolResult } from './tools.js'; import { SchemaValidator } from '../utils/schemaValidator.js'; import { makeRelative, shortenPath } from '../utils/paths.js'; +import { getErrorMessage, isNodeError } from '../utils/errors.js'; // --- Interfaces (kept separate for clarity) --- @@ -39,17 +40,12 @@ interface GrepMatch { line: string; } -/** - * Result from the GrepTool - */ -export interface GrepToolResult extends ToolResult {} - // --- GrepTool Class --- /** * Implementation of the GrepTool that searches file contents using git grep, system grep, or JS fallback. */ -export class GrepTool extends BaseTool<GrepToolParams, GrepToolResult> { +export class GrepTool extends BaseTool<GrepToolParams, ToolResult> { private rootDirectory: string; /** @@ -114,12 +110,12 @@ export class GrepTool extends BaseTool<GrepToolParams, GrepToolResult> { if (!stats.isDirectory()) { throw new Error(`Path is not a directory: ${targetPath}`); } - } catch (err: any) { - if (err.code === 'ENOENT') { + } catch (error: unknown) { + if (isNodeError(error) && error.code !== 'ENOENT') { throw new Error(`Path does not exist: ${targetPath}`); } throw new Error( - `Failed to access path stats for ${targetPath}: ${err.message}`, + `Failed to access path stats for ${targetPath}: ${error}`, ); } @@ -164,7 +160,7 @@ export class GrepTool extends BaseTool<GrepToolParams, GrepToolResult> { * @param params Parameters for the grep search * @returns Result of the grep search */ - async execute(params: GrepToolParams): Promise<GrepToolResult> { + async execute(params: GrepToolParams): Promise<ToolResult> { const validationError = this.invalidParams(params); if (validationError) { console.error(`GrepTool Parameter Validation Failed: ${validationError}`); @@ -253,7 +249,7 @@ export class GrepTool extends BaseTool<GrepToolParams, GrepToolResult> { }); child.on('close', (code) => resolve(code === 0)); child.on('error', () => resolve(false)); - } catch (e) { + } catch { resolve(false); } }); @@ -277,10 +273,10 @@ export class GrepTool extends BaseTool<GrepToolParams, GrepToolResult> { return true; } return false; - } catch (err: any) { - if (err.code !== 'ENOENT') { + } catch (error: unknown) { + if (!isNodeError(error) || error.code !== 'ENOENT') { console.error( - `Error checking for .git in ${currentPath}: ${err.message}`, + `Error checking for .git in ${currentPath}: ${error}`, ); return false; } @@ -291,9 +287,9 @@ export class GrepTool extends BaseTool<GrepToolParams, GrepToolResult> { } currentPath = path.dirname(currentPath); } - } catch (err: any) { + } catch (error: unknown) { console.error( - `Error traversing directory structure upwards from ${dirPath}: ${err instanceof Error ? err.message : String(err)}`, + `Error traversing directory structure upwards from ${dirPath}: ${error instanceof Error ? error.message : error}`, ); } return false; @@ -446,9 +442,9 @@ export class GrepTool extends BaseTool<GrepToolParams, GrepToolResult> { }); }); return this.parseGrepOutput(output, absolutePath); - } catch (gitError: any) { + } catch (gitError: unknown) { console.error( - `GrepTool: git grep strategy failed: ${gitError.message}. Falling back...`, + `GrepTool: git grep strategy failed: ${getErrorMessage(gitError)}. Falling back...`, ); } } @@ -512,9 +508,9 @@ export class GrepTool extends BaseTool<GrepToolParams, GrepToolResult> { }); }); return this.parseGrepOutput(output, absolutePath); - } catch (grepError: any) { + } catch (grepError: unknown) { console.error( - `GrepTool: System grep strategy failed: ${grepError.message}. Falling back...`, + `GrepTool: System grep strategy failed: ${getErrorMessage(grepError)}. Falling back...`, ); } } @@ -559,19 +555,19 @@ export class GrepTool extends BaseTool<GrepToolParams, GrepToolResult> { }); } }); - } catch (readError: any) { - if (readError.code !== 'ENOENT') { + } catch (readError: unknown) { + if (!isNodeError(readError) || readError.code !== 'ENOENT') { console.error( - `GrepTool: Could not read or process file ${fileAbsolutePath}: ${readError.message}`, + `GrepTool: Could not read or process file ${fileAbsolutePath}: ${getErrorMessage(readError)}`, ); } } } return allMatches; - } catch (error: any) { + } catch (error: unknown) { console.error( - `GrepTool: Error during performGrepSearch (Strategy: ${strategyUsed}): ${error.message}`, + `GrepTool: Error during performGrepSearch (Strategy: ${strategyUsed}): ${getErrorMessage(error)}`, ); throw error; // Re-throw to be caught by the execute method's handler } |
