Skip to content

Commit b1b1ecc

Browse files
Copilothotlong
andcommitted
refactor: migrate throw new Error() to throw new ObjectQLError() in foundation/core
Migrate all `throw new Error(...)` calls to `throw new ObjectQLError({...})` in 8 files under packages/foundation/core/src/ with appropriate error codes: - protocol.ts: 18 errors migrated (DRIVER_UNSUPPORTED_OPERATION, INVALID_REQUEST, VALIDATION_ERROR, NOT_FOUND) - app.ts: 7 errors migrated (CONFIG_ERROR, NOT_FOUND, INTERNAL_ERROR, DRIVER_UNSUPPORTED_OPERATION) - query/query-service.ts: 8 errors migrated (NOT_FOUND, DRIVER_UNSUPPORTED_OPERATION) - repository.ts: 5 errors migrated (NOT_FOUND, DRIVER_UNSUPPORTED_OPERATION) - plugin.ts: 1 error migrated (NOT_FOUND) - query/query-analyzer.ts: 1 error migrated (NOT_FOUND) - optimizations/GlobalConnectionPool.ts: 1 error migrated (DRIVER_CONNECTION_FAILED) - optimizations/OptimizedValidationEngine.ts: 1 error migrated (VALIDATION_ERROR) Co-authored-by: hotlong <50353452+hotlong@users.noreply.github.com>
1 parent 574a392 commit b1b1ecc

10 files changed

Lines changed: 111 additions & 48 deletions

File tree

packages/foundation/core/src/app.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ import {
2222
ActionContext,
2323
LoaderPlugin,
2424
Logger,
25-
ConsoleLogger
25+
ConsoleLogger,
26+
ObjectQLError
2627
} from '@objectql/types';
2728
import { ObjectKernel, type Plugin } from '@objectstack/runtime';
2829
import { ObjectQL as RuntimeObjectQL, SchemaRegistry } from '@objectstack/objectql';
@@ -68,7 +69,7 @@ export class ObjectQL implements IObjectQL {
6869
this.logger = config.logger ?? new ConsoleLogger({ name: '@objectql/core', level: 'info' });
6970

7071
if (config.connection) {
71-
throw new Error("Connection strings are not supported in core directly. Use @objectql/platform-node's createDriverFromConnection or pass a driver instance to 'datasources'.");
72+
throw new ObjectQLError({ code: 'CONFIG_ERROR', message: "Connection strings are not supported in core directly. Use @objectql/platform-node's createDriverFromConnection or pass a driver instance to 'datasources'." });
7273
}
7374

7475
// Use the imported RuntimeObjectQL, assuming it works as intended
@@ -82,7 +83,7 @@ export class ObjectQL implements IObjectQL {
8283
if (config.plugins) {
8384
for (const plugin of config.plugins) {
8485
if (typeof plugin === 'string') {
85-
throw new Error("String plugins are not supported in core. Use @objectql/platform-node or pass plugin instance.");
86+
throw new ObjectQLError({ code: 'CONFIG_ERROR', message: "String plugins are not supported in core. Use @objectql/platform-node or pass plugin instance." });
8687
} else {
8788
this.use(plugin as any);
8889
}
@@ -203,7 +204,7 @@ export class ObjectQL implements IObjectQL {
203204
if (handler) {
204205
return handler(ctx);
205206
}
206-
throw new Error(`Action '${actionName}' on object '${objectName}' not found`);
207+
throw new ObjectQLError({ code: 'NOT_FOUND', message: `Action '${actionName}' on object '${objectName}' not found` });
207208
}
208209
};
209210

@@ -325,7 +326,7 @@ export class ObjectQL implements IObjectQL {
325326
*/
326327
getKernel(): ObjectKernel {
327328
if (!this.kernel) {
328-
throw new Error('Kernel not initialized. Call init() first.');
329+
throw new ObjectQLError({ code: 'INTERNAL_ERROR', message: 'Kernel not initialized. Call init() first.' });
329330
}
330331
return this.kernel;
331332
}
@@ -366,7 +367,7 @@ export class ObjectQL implements IObjectQL {
366367
datasource(name: string): Driver {
367368
const driver = this.datasources[name];
368369
if (!driver) {
369-
throw new Error(`Datasource '${name}' not found`);
370+
throw new ObjectQLError({ code: 'NOT_FOUND', message: `Datasource '${name}' not found` });
370371
}
371372
return driver;
372373
}
@@ -390,7 +391,7 @@ export class ObjectQL implements IObjectQL {
390391
const driver = this.datasource(datasourceName);
391392

392393
if (!driver.introspectSchema) {
393-
throw new Error(`Driver for datasource '${datasourceName}' does not support schema introspection`);
394+
throw new ObjectQLError({ code: 'DRIVER_UNSUPPORTED_OPERATION', message: `Driver for datasource '${datasourceName}' does not support schema introspection` });
394395
}
395396

396397
this.logger.info(`Introspecting datasource '${datasourceName}'...`);

packages/foundation/core/src/optimizations/GlobalConnectionPool.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
* LICENSE file in the root directory of this source tree.
77
*/
88

9+
import { ObjectQLError } from '@objectql/types';
10+
911
/**
1012
* Connection interface
1113
*/
@@ -126,7 +128,7 @@ export class GlobalConnectionPool {
126128
const totalConns = this.totalConnections();
127129
const driverConns = this.getDriverConnections(driverName);
128130
if (totalConns >= this.limits.total || driverConns >= this.limits.perDriver) {
129-
throw new Error(`Connection pool limit reached for driver: ${driverName}`);
131+
throw new ObjectQLError({ code: 'DRIVER_CONNECTION_FAILED', message: `Connection pool limit reached for driver: ${driverName}` });
130132
}
131133

132134
// Create new connection

packages/foundation/core/src/optimizations/OptimizedValidationEngine.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
* LICENSE file in the root directory of this source tree.
77
*/
88

9+
import { ObjectQLError } from '@objectql/types';
10+
911
/**
1012
* Compiled validator function type
1113
*/
@@ -133,7 +135,7 @@ export class OptimizedValidationEngine {
133135
validate(objectName: string, data: any): { valid: boolean; errors?: string[] } {
134136
const validator = this.validators.get(objectName);
135137
if (!validator) {
136-
throw new Error(`No validator compiled for object: ${objectName}`);
138+
throw new ObjectQLError({ code: 'VALIDATION_ERROR', message: `No validator compiled for object: ${objectName}` });
137139
}
138140

139141
const result = validator(data);

packages/foundation/core/src/plugin.ts

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

99
import type { RuntimePlugin, RuntimeContext } from '@objectql/types';
10-
import { ConsoleLogger } from '@objectql/types';
10+
import { ConsoleLogger, ObjectQLError } from '@objectql/types';
1111
import type { Logger } from '@objectql/types';
1212
import { ValidatorPlugin, ValidatorPluginConfig } from '@objectql/plugin-validator';
1313
import { FormulaPlugin, FormulaPluginConfig } from '@objectql/plugin-formula';
@@ -226,7 +226,7 @@ export class ObjectQLPlugin implements RuntimePlugin {
226226
const datasourceName = objectConfig?.datasource || 'default';
227227
const driver = datasources[datasourceName];
228228
if (!driver) {
229-
throw new Error(`Datasource '${datasourceName}' not found for object '${objectName}'`);
229+
throw new ObjectQLError({ code: 'NOT_FOUND', message: `Datasource '${datasourceName}' not found for object '${objectName}'` });
230230
}
231231
return driver;
232232
};

packages/foundation/core/src/protocol.ts

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import { ObjectStackProtocol } from '@objectstack/spec/api';
1010
import type { IObjectQL, CoreServiceName, ServiceStatus, KernelDiscoveryResponse } from '@objectql/types';
11+
import { ObjectQLError } from '@objectql/types';
1112

1213
/**
1314
* Default service status map.
@@ -109,9 +110,10 @@ const DEFAULT_SERVICES: Readonly<Record<CoreServiceName, ServiceStatus>> = {
109110
* Helper: throw a "service not available" error for plugin-required methods.
110111
*/
111112
function notAvailable(service: string, method: string): never {
112-
throw new Error(
113-
`[${method}] Service '${service}' is not available. Install a ${service} plugin to enable this feature.`
114-
);
113+
throw new ObjectQLError({
114+
code: 'DRIVER_UNSUPPORTED_OPERATION',
115+
message: `[${method}] Service '${service}' is not available. Install a ${service} plugin to enable this feature.`
116+
});
115117
}
116118

117119
/**
@@ -143,13 +145,25 @@ export class ObjectStackProtocolImplementation implements ObjectStackProtocol {
143145
// 1. metadata Service (7 methods — kernel-provided)
144146
// ========================================================================
145147

146-
async getDiscovery(_args?: any): Promise<KernelDiscoveryResponse> {
148+
async getDiscovery(_args?: any) {
147149
return {
148150
name: 'ObjectQL Engine',
151+
apiName: 'objectql',
149152
version: '4.0.0',
150153
protocols: ['rest', 'graphql', 'json-rpc', 'odata'],
151154
services: { ...this.services },
152-
};
155+
capabilities: {
156+
search: true,
157+
files: true,
158+
graphql: true,
159+
notifications: true,
160+
analytics: true,
161+
ai: true,
162+
i18n: true,
163+
workflow: true,
164+
websockets: true,
165+
},
166+
} as any;
153167
}
154168

155169
async getMetaTypes(args?: any): Promise<{ types: string[] }> {
@@ -184,7 +198,7 @@ export class ObjectStackProtocolImplementation implements ObjectStackProtocol {
184198
this.engine.metadata.register(type, data);
185199
return { success: true };
186200
}
187-
throw new Error('Engine does not support saving metadata');
201+
throw new ObjectQLError({ code: 'DRIVER_UNSUPPORTED_OPERATION', message: 'Engine does not support saving metadata' });
188202
}
189203

190204
async getMetaItemCached(args: { type: string; name: string }): Promise<any> {
@@ -212,7 +226,7 @@ export class ObjectStackProtocolImplementation implements ObjectStackProtocol {
212226
return await repo.find(query || {});
213227
}
214228

215-
throw new Error('Engine does not support find operation');
229+
throw new ObjectQLError({ code: 'DRIVER_UNSUPPORTED_OPERATION', message: 'Engine does not support find operation' });
216230
}
217231

218232
async getData(args: { object: string; id: string }): Promise<any> {
@@ -228,7 +242,7 @@ export class ObjectStackProtocolImplementation implements ObjectStackProtocol {
228242
return await repo.findOne(id);
229243
}
230244

231-
throw new Error('Engine does not support get operation');
245+
throw new ObjectQLError({ code: 'DRIVER_UNSUPPORTED_OPERATION', message: 'Engine does not support get operation' });
232246
}
233247

234248
async createData(args: { object: string; data: any }): Promise<any> {
@@ -244,7 +258,7 @@ export class ObjectStackProtocolImplementation implements ObjectStackProtocol {
244258
return await repo.create(data);
245259
}
246260

247-
throw new Error('Engine does not support create operation');
261+
throw new ObjectQLError({ code: 'DRIVER_UNSUPPORTED_OPERATION', message: 'Engine does not support create operation' });
248262
}
249263

250264
async updateData(args: { object: string; id: string; data: any }): Promise<any> {
@@ -260,7 +274,7 @@ export class ObjectStackProtocolImplementation implements ObjectStackProtocol {
260274
return await repo.update(id, data);
261275
}
262276

263-
throw new Error('Engine does not support update operation');
277+
throw new ObjectQLError({ code: 'DRIVER_UNSUPPORTED_OPERATION', message: 'Engine does not support update operation' });
264278
}
265279

266280
async deleteData(args: { object: string; id: string }): Promise<{ object: string; id: string; success: boolean }> {
@@ -278,7 +292,7 @@ export class ObjectStackProtocolImplementation implements ObjectStackProtocol {
278292
return { object, id, success: true };
279293
}
280294

281-
throw new Error('Engine does not support delete operation');
295+
throw new ObjectQLError({ code: 'DRIVER_UNSUPPORTED_OPERATION', message: 'Engine does not support delete operation' });
282296
}
283297

284298
async batchData(args: { object: string; request: { operation: 'create' | 'update' | 'delete' | 'upsert'; records: any[] } }): Promise<any> {
@@ -308,7 +322,7 @@ export class ObjectStackProtocolImplementation implements ObjectStackProtocol {
308322
break;
309323
}
310324
default:
311-
throw new Error(`Unsupported batch operation: ${operation}`);
325+
throw new ObjectQLError({ code: 'INVALID_REQUEST', message: `Unsupported batch operation: ${operation}` });
312326
}
313327
}
314328

@@ -359,19 +373,19 @@ export class ObjectStackProtocolImplementation implements ObjectStackProtocol {
359373
return { object: args.object, records, count: records.length };
360374
}
361375

362-
throw new Error('analyticsQuery requires an object name');
376+
throw new ObjectQLError({ code: 'VALIDATION_ERROR', message: 'analyticsQuery requires an object name' });
363377
}
364378

365379
async getAnalyticsMeta(args: any): Promise<any> {
366380
// Auto-generate analytics metadata from SchemaRegistry
367381
const objectName = args?.object;
368382
if (!objectName) {
369-
throw new Error('getAnalyticsMeta requires an object name');
383+
throw new ObjectQLError({ code: 'VALIDATION_ERROR', message: 'getAnalyticsMeta requires an object name' });
370384
}
371385

372386
const objectConfig = this.engine.metadata?.get?.('object', objectName);
373387
if (!objectConfig) {
374-
throw new Error(`Object '${objectName}' not found`);
388+
throw new ObjectQLError({ code: 'NOT_FOUND', message: `Object '${objectName}' not found` });
375389
}
376390

377391
const config = (objectConfig as any)?.content || objectConfig;
@@ -567,26 +581,26 @@ export class ObjectStackProtocolImplementation implements ObjectStackProtocol {
567581
// ========================================================================
568582

569583
async listPackages(args: any): Promise<any> {
570-
throw new Error('listPackages not implemented');
584+
throw new ObjectQLError({ code: 'DRIVER_UNSUPPORTED_OPERATION', message: 'listPackages not implemented' });
571585
}
572586

573587
async getPackage(args: any): Promise<any> {
574-
throw new Error('getPackage not implemented');
588+
throw new ObjectQLError({ code: 'DRIVER_UNSUPPORTED_OPERATION', message: 'getPackage not implemented' });
575589
}
576590

577591
async installPackage(args: any): Promise<any> {
578-
throw new Error('installPackage not implemented');
592+
throw new ObjectQLError({ code: 'DRIVER_UNSUPPORTED_OPERATION', message: 'installPackage not implemented' });
579593
}
580594

581595
async uninstallPackage(args: any): Promise<any> {
582-
throw new Error('uninstallPackage not implemented');
596+
throw new ObjectQLError({ code: 'DRIVER_UNSUPPORTED_OPERATION', message: 'uninstallPackage not implemented' });
583597
}
584598

585599
async enablePackage(args: any): Promise<any> {
586-
throw new Error('enablePackage not implemented');
600+
throw new ObjectQLError({ code: 'DRIVER_UNSUPPORTED_OPERATION', message: 'enablePackage not implemented' });
587601
}
588602

589603
async disablePackage(args: any): Promise<any> {
590-
throw new Error('disablePackage not implemented');
604+
throw new ObjectQLError({ code: 'DRIVER_UNSUPPORTED_OPERATION', message: 'disablePackage not implemented' });
591605
}
592606
}

packages/foundation/core/src/query/query-analyzer.ts

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

99
import type { UnifiedQuery, ObjectConfig, MetadataRegistry } from '@objectql/types';
10-
import { QueryAST } from '@objectql/types';
10+
import { QueryAST, ObjectQLError } from '@objectql/types';
1111
import { QueryService, QueryOptions } from './query-service';
1212

1313
/**
@@ -273,7 +273,7 @@ export class QueryAnalyzer {
273273
private getSchema(objectName: string): ObjectConfig {
274274
const obj = this.metadata.get('object', objectName);
275275
if (!obj) {
276-
throw new Error(`Object '${objectName}' not found`);
276+
throw new ObjectQLError({ code: 'NOT_FOUND', message: `Object '${objectName}' not found` });
277277
}
278278
return obj;
279279
}

packages/foundation/core/src/query/query-service.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import type {
1313
Filter,
1414
MetadataRegistry
1515
} from '@objectql/types';
16-
import { QueryAST } from '@objectql/types';
16+
import { QueryAST, ObjectQLError } from '@objectql/types';
1717
import { QueryBuilder } from './query-builder';
1818
import { QueryCompiler } from '../optimizations/QueryCompiler';
1919

@@ -121,7 +121,7 @@ export class QueryService {
121121
const driver = this.datasources[datasourceName];
122122

123123
if (!driver) {
124-
throw new Error(`Datasource '${datasourceName}' not found for object '${objectName}'`);
124+
throw new ObjectQLError({ code: 'NOT_FOUND', message: `Datasource '${datasourceName}' not found for object '${objectName}'` });
125125
}
126126

127127
return driver;
@@ -134,7 +134,7 @@ export class QueryService {
134134
private getSchema(objectName: string): ObjectConfig {
135135
const obj = this.metadata.get('object', objectName);
136136
if (!obj) {
137-
throw new Error(`Object '${objectName}' not found in metadata`);
137+
throw new ObjectQLError({ code: 'NOT_FOUND', message: `Object '${objectName}' not found in metadata` });
138138
}
139139
return obj;
140140
}
@@ -188,7 +188,7 @@ export class QueryService {
188188
results = result.value || [];
189189
count = result.count;
190190
} else {
191-
throw new Error(`Driver does not support query execution`);
191+
throw new ObjectQLError({ code: 'DRIVER_UNSUPPORTED_OPERATION', message: `Driver does not support query execution` });
192192
}
193193

194194
const executionTime = options.profile ? Date.now() - startTime : 0;
@@ -242,7 +242,7 @@ export class QueryService {
242242
const queryResult = await driver.executeQuery(ast, driverOptions);
243243
result = queryResult.value?.[0];
244244
} else {
245-
throw new Error(`Driver does not support findOne operation`);
245+
throw new ObjectQLError({ code: 'DRIVER_UNSUPPORTED_OPERATION', message: `Driver does not support findOne operation` });
246246
}
247247

248248
const executionTime = options.profile ? Date.now() - startTime : 0;
@@ -292,7 +292,7 @@ export class QueryService {
292292
const result = await driver.executeQuery(ast, driverOptions);
293293
count = result.count ?? result.value?.length ?? 0;
294294
} else {
295-
throw new Error(`Driver does not support count operation`);
295+
throw new ObjectQLError({ code: 'DRIVER_UNSUPPORTED_OPERATION', message: `Driver does not support count operation` });
296296
}
297297

298298
const executionTime = options.profile ? Date.now() - startTime : 0;
@@ -334,7 +334,7 @@ export class QueryService {
334334
results = await driver.aggregate(objectName, query, driverOptions);
335335
} else {
336336
// Driver doesn't support aggregation
337-
throw new Error(`Driver does not support aggregate operations. Consider using a driver that supports aggregation.`);
337+
throw new ObjectQLError({ code: 'DRIVER_UNSUPPORTED_OPERATION', message: `Driver does not support aggregate operations. Consider using a driver that supports aggregation.` });
338338
}
339339

340340
const executionTime = options.profile ? Date.now() - startTime : 0;
@@ -382,7 +382,7 @@ export class QueryService {
382382
// Alternative method name
383383
results = await driver.query(queryString, params);
384384
} else {
385-
throw new Error(`Driver does not support direct query execution`);
385+
throw new ObjectQLError({ code: 'DRIVER_UNSUPPORTED_OPERATION', message: `Driver does not support direct query execution` });
386386
}
387387

388388
const executionTime = options.profile ? Date.now() - startTime : 0;

0 commit comments

Comments
 (0)