Skip to content

Commit 04ae4ab

Browse files
Copilothotlong
andcommitted
refactor: migrate throw new Error to throw new ObjectQLError across 8 source files
Co-authored-by: hotlong <50353452+hotlong@users.noreply.github.com>
1 parent 2dd651f commit 04ae4ab

8 files changed

Lines changed: 31 additions & 23 deletions

File tree

packages/foundation/edge-adapter/src/plugin.ts

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

9+
import { ObjectQLError } from '@objectql/types';
910
import type { RuntimePlugin, RuntimeContext, EdgeAdapterConfig } from '@objectql/types';
1011
import { detectRuntime } from './detector.js';
1112
import { validateCapabilities, getCapabilities, type CapabilityRequirement } from './capabilities.js';
@@ -52,9 +53,10 @@ export class EdgeAdapterPlugin implements RuntimePlugin {
5253
if (this.config.requirements) {
5354
const validation = validateCapabilities(runtime, this.config.requirements);
5455
if (!validation.valid) {
55-
throw new Error(
56-
`[${this.name}] Runtime '${runtime}' missing capabilities: ${validation.missing.join(', ')}`,
57-
);
56+
throw new ObjectQLError({
57+
code: 'CONFIG_ERROR',
58+
message: `[${this.name}] Runtime '${runtime}' missing capabilities: ${validation.missing.join(', ')}`,
59+
});
5860
}
5961
}
6062

packages/foundation/platform-node/src/driver.ts

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

9-
import { Driver } from '@objectql/types';
9+
import { Driver, ObjectQLError } from '@objectql/types';
1010

1111
export function createDriverFromConnection(connection: string): Driver {
1212
let driverPackage = '';
@@ -45,18 +45,18 @@ export function createDriverFromConnection(connection: string): Driver {
4545
};
4646
}
4747
else {
48-
throw new Error(`Unsupported connection protocol: ${connection}`);
48+
throw new ObjectQLError({ code: 'DRIVER_UNSUPPORTED_OPERATION', message: `Unsupported connection protocol: ${connection}` });
4949
}
5050

5151
try {
5252

5353
const pkg = require(driverPackage);
5454
const DriverClass = pkg[driverClass];
5555
if (!DriverClass) {
56-
throw new Error(`${driverClass} not found in ${driverPackage}`);
56+
throw new ObjectQLError({ code: 'DRIVER_ERROR', message: `${driverClass} not found in ${driverPackage}` });
5757
}
5858
return new DriverClass(driverConfig);
5959
} catch (e: any) {
60-
throw new Error(`Failed to load driver ${driverPackage}. Please install it: npm install ${driverPackage}. Error: ${e.message}`);
60+
throw new ObjectQLError({ code: 'DRIVER_ERROR', message: `Failed to load driver ${driverPackage}. Please install it: npm install ${driverPackage}. Error: ${e.message}` });
6161
}
6262
}

packages/foundation/platform-node/src/plugin.ts

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

99
import { Kernel } from '@objectstack/spec';
10+
import { ObjectQLError } from '@objectql/types';
1011
type PluginDefinition = Kernel.PluginDefinition;
1112

1213
export function loadPlugin(packageName: string): PluginDefinition {
@@ -15,7 +16,7 @@ export function loadPlugin(packageName: string): PluginDefinition {
1516
const modulePath = require.resolve(packageName, { paths: [process.cwd()] });
1617
mod = require(modulePath);
1718
} catch (e) {
18-
throw new Error(`Failed to resolve plugin '${packageName}': ${e}`);
19+
throw new ObjectQLError({ code: 'CONFIG_ERROR', message: `Failed to resolve plugin '${packageName}': ${e}` });
1920
}
2021

2122
// Helper to check if candidate is a PluginDefinition
@@ -74,6 +75,6 @@ export function loadPlugin(packageName: string): PluginDefinition {
7475
return instance;
7576
} else {
7677
console.error(`[PluginLoader] Failed to find plugin in '${packageName}'. Exports:`, Object.keys(mod));
77-
throw new Error(`Plugin '${packageName}' must export a PluginDefinition with lifecycle hooks (onEnable, onDisable, etc.).`);
78+
throw new ObjectQLError({ code: 'CONFIG_ERROR', message: `Plugin '${packageName}' must export a PluginDefinition with lifecycle hooks (onEnable, onDisable, etc.).` });
7879
}
7980
}

packages/foundation/plugin-security/src/query-trimmer.ts

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

9+
import { ObjectQLError } from '@objectql/types';
910
import type { PermissionConfig, RecordRuleCondition } from '@objectql/types';
1011
import type { SecurityContext } from './types';
1112
import { PermissionLoader } from './permission-loader';
@@ -279,7 +280,7 @@ export class QueryTrimmer {
279280
}
280281

281282
// Unable to convert formula to filter
282-
throw new Error(`Unsupported formula pattern: ${formula}`);
283+
throw new ObjectQLError({ code: 'INTERNAL_ERROR', message: `Unsupported formula pattern: ${formula}` });
283284
}
284285

285286
/**
@@ -353,7 +354,7 @@ export class QueryTrimmer {
353354
const nestedFilter = this.conditionToFilter(nestedCondition, user);
354355

355356
if (Object.keys(nestedFilter).length === 0) {
356-
throw new Error('Unable to convert nested lookup condition');
357+
throw new ObjectQLError({ code: 'INTERNAL_ERROR', message: 'Unable to convert nested lookup condition' });
357358
}
358359

359360
// Create a lookup filter

packages/foundation/plugin-workflow/src/engine/action-executor.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
* Actions are side effects that occur during state transitions.
1414
*/
1515

16+
import { ObjectQLError } from '@objectql/types';
1617
import type { ExecutionContext, ActionResult } from '../types';
1718

1819
/**
@@ -167,7 +168,7 @@ export class ActionExecutor {
167168
private executeFieldUpdate(expr: string, record: Record<string, any>): void {
168169
const equalIndex = expr.indexOf('=');
169170
if (equalIndex === -1) {
170-
throw new Error(`Invalid field update expression: ${expr}`);
171+
throw new ObjectQLError({ code: 'WORKFLOW_TRANSITION_DENIED', message: `Invalid field update expression: ${expr}` });
171172
}
172173

173174
const fieldName = expr.substring(0, equalIndex).trim();

packages/tools/cli/src/commands/migrate.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import * as fs from 'fs';
1010
import * as path from 'path';
1111
import chalk from 'chalk';
12+
import { ObjectQLError } from '@objectql/types';
1213
import { resolveConfigFile } from '../utils/config-loader';
1314

1415
interface MigrateOptions {
@@ -274,7 +275,7 @@ async function loadObjectQLInstance(configPath?: string): Promise<any> {
274275
const app = configModule.default || configModule.app || configModule.objectql || configModule.db;
275276

276277
if (!app) {
277-
throw new Error('Config file must export an ObjectQL instance');
278+
throw new ObjectQLError({ code: 'CONFIG_ERROR', message: 'Config file must export an ObjectQL instance' });
278279
}
279280

280281
await app.init();

packages/tools/cli/src/commands/sync.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import * as fs from 'fs';
1010
import * as path from 'path';
1111
import chalk from 'chalk';
1212
import * as yaml from 'js-yaml';
13-
import { IntrospectedSchema, IntrospectedTable, IntrospectedColumn, ObjectConfig, IObjectQL, FieldConfig, FieldType } from '@objectql/types';
13+
import { IntrospectedSchema, IntrospectedTable, IntrospectedColumn, ObjectConfig, IObjectQL, FieldConfig, FieldType, ObjectQLError } from '@objectql/types';
1414
import { resolveConfigFile } from '../utils/config-loader';
1515

1616
interface SyncOptions {
@@ -45,7 +45,7 @@ export async function syncDatabase(options: SyncOptions) {
4545
if (!driver || !driver.introspectSchema) {
4646
const errorMsg = 'The configured driver does not support schema introspection. Only SQL drivers (PostgreSQL, MySQL, SQLite) support this feature.';
4747
console.error(chalk.red(`❌ ${errorMsg}`));
48-
throw new Error(errorMsg);
48+
throw new ObjectQLError({ code: 'DRIVER_UNSUPPORTED_OPERATION', message: errorMsg });
4949
}
5050

5151
// Introspect database schema
@@ -291,7 +291,7 @@ async function loadObjectQLInstance(configPath?: string): Promise<IObjectQL> {
291291
}
292292
});
293293
} catch (err) {
294-
throw new Error('TypeScript config file detected but ts-node is not installed. Please run: npm install --save-dev ts-node');
294+
throw new ObjectQLError({ code: 'CONFIG_ERROR', message: 'TypeScript config file detected but ts-node is not installed. Please run: npm install --save-dev ts-node' });
295295
}
296296
}
297297

@@ -309,7 +309,7 @@ async function loadObjectQLInstance(configPath?: string): Promise<IObjectQL> {
309309
const app = configModule.default || configModule.app || configModule.objectql || configModule.db;
310310

311311
if (!app) {
312-
throw new Error('Config file must export an ObjectQL instance as default export or named export (app/objectql/db)');
312+
throw new ObjectQLError({ code: 'CONFIG_ERROR', message: 'Config file must export an ObjectQL instance as default export or named export (app/objectql/db)' });
313313
}
314314

315315
// Initialize app (but don't sync schema - we're reading it)

packages/tools/cli/src/utils/config-loader.ts

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

99
import * as path from 'path';
1010
import * as fs from 'fs';
11+
import { ObjectQLError } from '@objectql/types';
1112

1213
/**
1314
* Config file search order:
@@ -36,7 +37,7 @@ export function resolveConfigFile(configPath?: string, cwd: string = process.cwd
3637
if (configPath) {
3738
const resolved = path.isAbsolute(configPath) ? configPath : path.join(cwd, configPath);
3839
if (!fs.existsSync(resolved)) {
39-
throw new Error(`Configuration file not found: ${resolved}`);
40+
throw new ObjectQLError({ code: 'CONFIG_ERROR', message: `Configuration file not found: ${resolved}` });
4041
}
4142
return resolved;
4243
}
@@ -48,11 +49,12 @@ export function resolveConfigFile(configPath?: string, cwd: string = process.cwd
4849
}
4950
}
5051

51-
throw new Error(
52-
'No configuration file found. Expected one of:\n' +
53-
CONFIG_FILE_NAMES.map(f => ` - ${f}`).join('\n') +
54-
'\n\nRun `objectstack create` to scaffold a new project.'
55-
);
52+
throw new ObjectQLError({
53+
code: 'CONFIG_ERROR',
54+
message: 'No configuration file found. Expected one of:\n' +
55+
CONFIG_FILE_NAMES.map(f => ` - ${f}`).join('\n') +
56+
'\n\nRun `objectstack create` to scaffold a new project.',
57+
});
5658
}
5759

5860
/**

0 commit comments

Comments
 (0)