From 0fffc7c055bbafc54da6f31a05342e033bc822c3 Mon Sep 17 00:00:00 2001 From: Tom Boucher Date: Mon, 4 May 2026 20:20:39 -0400 Subject: [PATCH] refactor: centralize gsd-tools error wrapping path --- sdk/src/gsd-tools.ts | 27 +++++++++++---------------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/sdk/src/gsd-tools.ts b/sdk/src/gsd-tools.ts index 28d49d9e9..3d67696dd 100644 --- a/sdk/src/gsd-tools.ts +++ b/sdk/src/gsd-tools.ts @@ -118,17 +118,22 @@ export class GSDTools { registryArgs: string[], mode: 'json' | 'raw', ): Promise { - try { - return await this.nativeHotpathAdapter.dispatch( + return this.executeWithToolsError(legacyCommand, legacyArgs, () => + this.nativeHotpathAdapter.dispatch( legacyCommand, legacyArgs, registryCommand, registryArgs, mode, - ); + )); + } + + private async executeWithToolsError(command: string, args: string[], work: () => Promise): Promise { + try { + return await work(); } catch (err) { if (err instanceof GSDToolsError) throw err; - throw this.toToolsError(legacyCommand, legacyArgs, err); + throw this.toToolsError(command, args, err); } } @@ -139,12 +144,7 @@ export class GSDTools { * Handles the `@file:` prefix pattern for large results. */ async exec(command: string, args: string[] = []): Promise { - try { - return await this.commandExecutor.exec(command, args, 'json'); - } catch (err) { - if (err instanceof GSDToolsError) throw err; - throw this.toToolsError(command, args, err); - } + return this.executeWithToolsError(command, args, () => this.commandExecutor.exec(command, args, 'json')); } // ─── Raw exec (no JSON parsing) ─────────────────────────────────────── @@ -154,12 +154,7 @@ export class GSDTools { * Use for commands like `config-set` that return plain text, not JSON. */ async execRaw(command: string, args: string[] = []): Promise { - try { - return await this.commandExecutor.exec(command, args, 'raw') as string; - } catch (err) { - if (err instanceof GSDToolsError) throw err; - throw this.toToolsError(command, args, err); - } + return this.executeWithToolsError(command, args, async () => this.commandExecutor.exec(command, args, 'raw') as string); }