From 0d0abaaa9f2d876eb7e86658daae698270482223 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Boschi?= Date: Fri, 9 Jan 2026 14:25:10 +0100 Subject: [PATCH] fix(typescript-client): Add error handling to all API methods (#139) Previously, most methods in HindsightClient would silently return undefined when API calls failed (e.g., connection refused). Only the `recall` method had proper error checking. This change adds a `validateResponse` helper method and applies it consistently to all API methods: - retain - retainBatch - recall - reflect - listMemories - createBank - getBankProfile Now all methods properly throw an error with details when the API request fails, instead of returning undefined. --- hindsight-clients/typescript/src/index.ts | 28 ++++++++++++++--------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/hindsight-clients/typescript/src/index.ts b/hindsight-clients/typescript/src/index.ts index 4a509380..2d91d233 100644 --- a/hindsight-clients/typescript/src/index.ts +++ b/hindsight-clients/typescript/src/index.ts @@ -78,6 +78,16 @@ export class HindsightClient { ); } + /** + * Validates the API response and throws an error if the request failed. + */ + private validateResponse(response: { data?: T; error?: unknown }, operation: string): T { + if (!response.data) { + throw new Error(`${operation} failed: ${JSON.stringify(response.error || 'Unknown error')}`); + } + return response.data; + } + /** * Retain a single memory for a bank. */ @@ -126,7 +136,7 @@ export class HindsightClient { body: { items: [item], async: options?.async }, }); - return response.data!; + return this.validateResponse(response, 'retain'); } /** @@ -160,7 +170,7 @@ export class HindsightClient { }, }); - return response.data!; + return this.validateResponse(response, 'retainBatch'); } /** @@ -198,11 +208,7 @@ export class HindsightClient { }, }); - if (!response.data) { - throw new Error(`API returned no data: ${JSON.stringify(response.error || 'Unknown error')}`); - } - - return response.data; + return this.validateResponse(response, 'recall'); } /** @@ -223,7 +229,7 @@ export class HindsightClient { }, }); - return response.data!; + return this.validateResponse(response, 'reflect'); } /** @@ -244,7 +250,7 @@ export class HindsightClient { }, }); - return response.data!; + return this.validateResponse(response, 'listMemories'); } /** @@ -264,7 +270,7 @@ export class HindsightClient { }, }); - return response.data!; + return this.validateResponse(response, 'createBank'); } /** @@ -276,7 +282,7 @@ export class HindsightClient { path: { bank_id: bankId }, }); - return response.data!; + return this.validateResponse(response, 'getBankProfile'); } }