From e42c6b73020f83cb2ed40ab6ec1338f81f556a51 Mon Sep 17 00:00:00 2001 From: David Snelling Date: Wed, 28 May 2025 16:02:20 -0700 Subject: [PATCH] feat: improve error handling and storage status reporting in BrainyData Updated error messages for initialization failures to use "BrainyData" for clarity. Enhanced `getStorageStatus` to handle storage adapters without the `getStorageStatus` method, ensuring proper reporting of storage type, usage, and adapter details. --- src/brainyData.ts | 39 +++++++++++++++++++++++++++++++++------ 1 file changed, 33 insertions(+), 6 deletions(-) diff --git a/src/brainyData.ts b/src/brainyData.ts index 5e9ec5fb..110fa3ea 100644 --- a/src/brainyData.ts +++ b/src/brainyData.ts @@ -108,8 +108,8 @@ export class BrainyData { this.isInitialized = true } catch (error) { - console.error('Failed to initialize vector database:', error) - throw new Error(`Failed to initialize vector database: ${error}`) + console.error('Failed to initialize BrainyData:', error) + throw new Error(`Failed to initialize BrainyData: ${error}`) } } @@ -628,6 +628,22 @@ export class BrainyData { } try { + // Check if the storage adapter has a getStorageStatus method + if (typeof this.storage.getStorageStatus !== 'function') { + // If not, determine the storage type based on the constructor name + const storageType = this.storage.constructor.name.toLowerCase().replace('storage', ''); + return { + type: storageType || 'unknown', + used: 0, + quota: null, + details: { + error: 'Storage adapter does not implement getStorageStatus method', + storageAdapter: this.storage.constructor.name, + indexSize: this.size() + } + } + } + // Get storage status from the storage adapter const storageStatus = await this.storage.getStorageStatus() @@ -636,20 +652,31 @@ export class BrainyData { indexSize: this.size() } + // Ensure all required fields are present return { - ...storageStatus, + type: storageStatus.type || 'unknown', + used: storageStatus.used || 0, + quota: storageStatus.quota || null, details: { - ...storageStatus.details, + ...(storageStatus.details || {}), index: indexInfo } } } catch (error) { console.error('Failed to get storage status:', error) + + // Determine the storage type based on the constructor name + const storageType = this.storage.constructor.name.toLowerCase().replace('storage', ''); + return { - type: 'unknown', + type: storageType || 'unknown', used: 0, quota: null, - details: { error: String(error) } + details: { + error: String(error), + storageAdapter: this.storage.constructor.name, + indexSize: this.size() + } } } }