Skip to content

Commit 899f76e

Browse files
Copilothotlong
andcommitted
refactor(redis): replace throw new Error with throw new ObjectQLError
Co-authored-by: hotlong <50353452+hotlong@users.noreply.github.com>
1 parent b1b1ecc commit 899f76e

5 files changed

Lines changed: 21 additions & 16 deletions

File tree

packages/drivers/pg-wasm/src/wasm-loader.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
* Handles lazy loading and initialization of the PostgreSQL WASM module
1212
*/
1313

14+
import { ObjectQLError } from '@objectql/types';
15+
1416
let pgliteModule: any = null;
1517

1618
/**
@@ -34,7 +36,7 @@ export async function loadWasmModule(): Promise<any> {
3436
*/
3537
export function getPGlite(): any {
3638
if (!pgliteModule) {
37-
throw new Error('PGlite WASM module not loaded. Call loadWasmModule() first.');
39+
throw new ObjectQLError({ code: 'DRIVER_ERROR', message: 'PGlite WASM module not loaded. Call loadWasmModule() first.' });
3840
}
3941
return pgliteModule;
4042
}

packages/drivers/redis/src/index.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ type DriverInterface = z.infer<typeof Data.DriverInterface>;
3939
* @version 4.1.0 - Production-ready with distinct() and aggregate()
4040
*/
4141

42-
import { Driver } from '@objectql/types';
42+
import { Driver, ObjectQLError } from '@objectql/types';
4343
import { createClient, RedisClientType } from 'redis';
4444

4545
/**
@@ -264,7 +264,7 @@ export class RedisDriver implements Driver {
264264
}
265265
}
266266

267-
throw new Error(`[RedisDriver] Failed to connect after ${maxAttempts} attempts: ${lastError?.message}`);
267+
throw new ObjectQLError({ code: 'DRIVER_CONNECTION_FAILED', message: `[RedisDriver] Failed to connect after ${maxAttempts} attempts: ${lastError?.message}` });
268268
}
269269

270270
/**
@@ -399,7 +399,7 @@ export class RedisDriver implements Driver {
399399
const result = await this.client.set(key, JSON.stringify(doc), { NX: true });
400400

401401
if (!result) {
402-
throw new Error(`Record with ID ${id} already exists in ${objectName}`);
402+
throw new ObjectQLError({ code: 'DRIVER_QUERY_FAILED', message: `Record with ID ${id} already exists in ${objectName}` });
403403
}
404404

405405
return doc;
@@ -415,7 +415,7 @@ export class RedisDriver implements Driver {
415415
const existing = await this.client.get(key);
416416

417417
if (!existing) {
418-
throw new Error(`Record not found: ${objectName}:${id}`);
418+
throw new ObjectQLError({ code: 'NOT_FOUND', message: `Record not found: ${objectName}:${id}` });
419419
}
420420

421421
const existingDoc = JSON.parse(existing);
@@ -806,7 +806,7 @@ export class RedisDriver implements Driver {
806806
switch (command.type) {
807807
case 'create':
808808
if (!command.data) {
809-
throw new Error('Create command requires data');
809+
throw new ObjectQLError({ code: 'DRIVER_QUERY_FAILED', message: 'Create command requires data' });
810810
}
811811
const created = await this.create(command.object, command.data, cmdOptions);
812812
return {
@@ -817,7 +817,7 @@ export class RedisDriver implements Driver {
817817

818818
case 'update':
819819
if (!command.id || !command.data) {
820-
throw new Error('Update command requires id and data');
820+
throw new ObjectQLError({ code: 'DRIVER_QUERY_FAILED', message: 'Update command requires id and data' });
821821
}
822822
const updated = await this.update(command.object, command.id, command.data, cmdOptions);
823823
return {
@@ -828,7 +828,7 @@ export class RedisDriver implements Driver {
828828

829829
case 'delete':
830830
if (!command.id) {
831-
throw new Error('Delete command requires id');
831+
throw new ObjectQLError({ code: 'DRIVER_QUERY_FAILED', message: 'Delete command requires id' });
832832
}
833833
await this.delete(command.object, command.id, cmdOptions);
834834
return {
@@ -838,7 +838,7 @@ export class RedisDriver implements Driver {
838838

839839
case 'bulkCreate':
840840
if (!command.records || !Array.isArray(command.records)) {
841-
throw new Error('BulkCreate command requires records array');
841+
throw new ObjectQLError({ code: 'DRIVER_QUERY_FAILED', message: 'BulkCreate command requires records array' });
842842
}
843843
// Use Redis PIPELINE for batch operations
844844
const pipeline = this.client.multi();
@@ -868,7 +868,7 @@ export class RedisDriver implements Driver {
868868

869869
case 'bulkUpdate':
870870
if (!command.updates || !Array.isArray(command.updates)) {
871-
throw new Error('BulkUpdate command requires updates array');
871+
throw new ObjectQLError({ code: 'DRIVER_QUERY_FAILED', message: 'BulkUpdate command requires updates array' });
872872
}
873873

874874
// First, batch GET all existing records using PIPELINE
@@ -917,7 +917,7 @@ export class RedisDriver implements Driver {
917917

918918
case 'bulkDelete':
919919
if (!command.ids || !Array.isArray(command.ids)) {
920-
throw new Error('BulkDelete command requires ids array');
920+
throw new ObjectQLError({ code: 'DRIVER_QUERY_FAILED', message: 'BulkDelete command requires ids array' });
921921
}
922922
// Use Redis PIPELINE for batch operations
923923
const deletePipeline = this.client.multi();
@@ -938,7 +938,7 @@ export class RedisDriver implements Driver {
938938
};
939939

940940
default:
941-
throw new Error(`Unknown command type: ${(command as any).type}`);
941+
throw new ObjectQLError({ code: 'DRIVER_UNSUPPORTED_OPERATION', message: `Unknown command type: ${(command as any).type}` });
942942
}
943943
} catch (error: any) {
944944
return {

packages/drivers/sdk/src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ export class RemoteDriver implements Driver {
336336
try {
337337
await this.checkHealth();
338338
} catch (error) {
339-
throw new Error(`Failed to connect to remote server: ${(error as Error).message}`);
339+
throw new ObjectQLError({ code: 'DRIVER_CONNECTION_FAILED', message: `Failed to connect to remote server: ${(error as Error).message}` });
340340
}
341341
}
342342

@@ -617,7 +617,7 @@ export class RemoteDriver implements Driver {
617617
const json = await res.json();
618618

619619
if (json.error) {
620-
throw new Error(json.error.message);
620+
throw new ObjectQLError({ code: 'DRIVER_QUERY_FAILED', message: json.error.message });
621621
}
622622

623623
return json.data;

packages/drivers/sqlite-wasm/src/knex-adapter.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*/
88

99
import { Knex } from 'knex';
10+
import { ObjectQLError } from '@objectql/types';
1011

1112
/**
1213
* Custom Knex client adapter for wa-sqlite
@@ -135,7 +136,7 @@ export class KnexWaSqliteClient {
135136
}
136137

137138
_stream(connection: any, obj: any, stream: any, options: any): any {
138-
throw new Error('Streaming is not supported in wa-sqlite adapter');
139+
throw new ObjectQLError({ code: 'DRIVER_UNSUPPORTED_OPERATION', message: 'Streaming is not supported in wa-sqlite adapter' });
139140
}
140141

141142
canCancelQuery = false;

packages/drivers/sqlite-wasm/src/wasm-loader.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
* Handles lazy loading and initialization of the SQLite WASM module
1212
*/
1313

14+
import { ObjectQLError } from '@objectql/types';
15+
1416
let wasmModule: any = null;
1517
let sqlite3: any = null;
1618

@@ -38,7 +40,7 @@ export async function loadWasmModule(): Promise<any> {
3840
*/
3941
export function getSqlite3(): any {
4042
if (!sqlite3) {
41-
throw new Error('SQLite WASM module not loaded. Call loadWasmModule() first.');
43+
throw new ObjectQLError({ code: 'DRIVER_ERROR', message: 'SQLite WASM module not loaded. Call loadWasmModule() first.' });
4244
}
4345
return sqlite3;
4446
}

0 commit comments

Comments
 (0)