Skip to content

Commit 2dd651f

Browse files
Copilothotlong
andcommitted
refactor: migrate throw new Error to ObjectQLError in protocol packages
Co-authored-by: hotlong <50353452+hotlong@users.noreply.github.com>
1 parent 203ffc6 commit 2dd651f

3 files changed

Lines changed: 32 additions & 25 deletions

File tree

packages/protocols/graphql/src/index.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
*/
1010

1111
import type { RuntimePlugin, RuntimeContext } from '@objectql/types';
12+
import { ObjectQLError } from '@objectql/types';
1213
import { ApolloServer, HeaderMap } from '@apollo/server';
1314
import { expressMiddleware } from '@as-integrations/express4';
1415
import { ApolloServerPluginDrainHttpServer } from '@apollo/server/plugin/drainHttpServer';
@@ -180,7 +181,7 @@ export class GraphQLPlugin implements RuntimePlugin {
180181
*/
181182
async onStart(ctx: RuntimeContext): Promise<void> {
182183
if (!this.engine) {
183-
throw new Error('Protocol not initialized. Install hook must be called first.');
184+
throw new ObjectQLError({ code: 'PROTOCOL_ERROR', message: 'Protocol not initialized. Install hook must be called first.' });
184185
}
185186

186187
// Check for Hono server service
@@ -1253,7 +1254,7 @@ export class GraphQLPlugin implements RuntimePlugin {
12531254
resolvers.Subscription[`${camelCaseName}Created`] = {
12541255
subscribe: (_: any, args: { where?: any }, context: any) => {
12551256
if (!context?.pubsub) {
1256-
throw new Error('PubSub not available');
1257+
throw new ObjectQLError({ code: 'INTERNAL_ERROR', message: 'PubSub not available' });
12571258
}
12581259
return context.pubsub.asyncIterator([`${pascalCaseName}_CREATED`]);
12591260
},
@@ -1270,7 +1271,7 @@ export class GraphQLPlugin implements RuntimePlugin {
12701271
resolvers.Subscription[`${camelCaseName}Updated`] = {
12711272
subscribe: (_: any, args: { where?: any }, context: any) => {
12721273
if (!context?.pubsub) {
1273-
throw new Error('PubSub not available');
1274+
throw new ObjectQLError({ code: 'INTERNAL_ERROR', message: 'PubSub not available' });
12741275
}
12751276
return context.pubsub.asyncIterator([`${pascalCaseName}_UPDATED`]);
12761277
},
@@ -1286,7 +1287,7 @@ export class GraphQLPlugin implements RuntimePlugin {
12861287
resolvers.Subscription[`${camelCaseName}Deleted`] = {
12871288
subscribe: (_: any, __: any, context: any) => {
12881289
if (!context?.pubsub) {
1289-
throw new Error('PubSub not available');
1290+
throw new ObjectQLError({ code: 'INTERNAL_ERROR', message: 'PubSub not available' });
12901291
}
12911292
return context.pubsub.asyncIterator([`${pascalCaseName}_DELETED`]);
12921293
}

packages/protocols/json-rpc/src/index.ts

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

99
import type { RuntimePlugin, RuntimeContext } from '@objectql/types';
10+
import { ObjectQLError } from '@objectql/types';
1011
import { IncomingMessage, ServerResponse, createServer, Server } from 'http';
1112
import {
1213
validateRequest,
@@ -209,7 +210,7 @@ export class JSONRPCPlugin implements RuntimePlugin {
209210
*/
210211
async onStart(ctx: RuntimeContext): Promise<void> {
211212
if (!this.engine) {
212-
throw new Error('Protocol not initialized. Install hook must be called first.');
213+
throw new ObjectQLError({ code: 'PROTOCOL_ERROR', message: 'Protocol not initialized. Install hook must be called first.' });
213214
}
214215

215216
// Check if Hono server is available via service injection
@@ -664,14 +665,14 @@ export class JSONRPCPlugin implements RuntimePlugin {
664665
*/
665666
private async executeAction(actionName: string, params?: any): Promise<any> {
666667
if (!this.engine) {
667-
throw new Error('Engine not initialized');
668+
throw new ObjectQLError({ code: 'INTERNAL_ERROR', message: 'Engine not initialized' });
668669
}
669670

670671
if (typeof this.engine.executeAction === 'function') {
671672
return await this.engine.executeAction(actionName, params);
672673
}
673674

674-
throw new Error('Action execution not supported by engine');
675+
throw new ObjectQLError({ code: 'PROTOCOL_METHOD_NOT_FOUND', message: 'Action execution not supported by engine' });
675676
}
676677

677678
/**
@@ -765,7 +766,7 @@ export class JSONRPCPlugin implements RuntimePlugin {
765766
this.methods.set('metadata.getAll', async (metaType: string) => {
766767
// Validate metaType parameter
767768
if (!metaType || typeof metaType !== 'string') {
768-
throw new Error('Invalid metaType parameter: must be a non-empty string');
769+
throw new ObjectQLError({ code: 'PROTOCOL_INVALID_REQUEST', message: 'Invalid metaType parameter: must be a non-empty string' });
769770
}
770771

771772
return this.getMetaItems(metaType);
@@ -807,7 +808,7 @@ export class JSONRPCPlugin implements RuntimePlugin {
807808
this.methods.set('session.get', async (sessionId: string, key: string) => {
808809
const session = this.sessions.get(sessionId);
809810
if (!session) {
810-
throw new Error('Session not found or expired');
811+
throw new ObjectQLError({ code: 'NOT_FOUND', message: 'Session not found or expired' });
811812
}
812813
this.updateSessionAccess(sessionId);
813814
return session.data[key];
@@ -820,7 +821,7 @@ export class JSONRPCPlugin implements RuntimePlugin {
820821
this.methods.set('session.set', async (sessionId: string, key: string, value: any) => {
821822
const session = this.sessions.get(sessionId);
822823
if (!session) {
823-
throw new Error('Session not found or expired');
824+
throw new ObjectQLError({ code: 'NOT_FOUND', message: 'Session not found or expired' });
824825
}
825826
this.updateSessionAccess(sessionId);
826827
session.data[key] = value;
@@ -854,7 +855,7 @@ export class JSONRPCPlugin implements RuntimePlugin {
854855
this.methods.set('system.describe', async (methodName: string) => {
855856
const signature = this.methodSignatures.get(methodName);
856857
if (!signature) {
857-
throw new Error(`Method not found: ${methodName}`);
858+
throw new ObjectQLError({ code: 'PROTOCOL_METHOD_NOT_FOUND', message: `Method not found: ${methodName}` });
858859
}
859860
return signature;
860861
});

packages/protocols/odata-v4/src/index.ts

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

99
import type { RuntimePlugin, RuntimeContext } from '@objectql/types';
10+
import { ObjectQLError } from '@objectql/types';
1011
import { IncomingMessage, ServerResponse, createServer, Server } from 'http';
1112
import { mapErrorToODataError } from './validation.js';
1213

@@ -130,7 +131,7 @@ export class ODataV4Plugin implements RuntimePlugin {
130131
*/
131132
async onStart(ctx: RuntimeContext): Promise<void> {
132133
if (!this.engine) {
133-
throw new Error('Protocol not initialized. Install hook must be called first.');
134+
throw new ObjectQLError({ code: 'PROTOCOL_ERROR', message: 'Protocol not initialized. Install hook must be called first.' });
134135
}
135136

136137
// Check if Hono server is available via service injection
@@ -1060,11 +1061,12 @@ export class ODataV4Plugin implements RuntimePlugin {
10601061
}
10611062

10621063
// Unsupported filter expression
1063-
throw new Error(
1064-
`Unsupported $filter expression: "${filter}". ` +
1064+
throw new ObjectQLError({
1065+
code: 'PROTOCOL_INVALID_REQUEST',
1066+
message: `Unsupported $filter expression: "${filter}". ` +
10651067
`Supported operators: eq, ne, gt, ge, lt, le, and, or, not. ` +
10661068
`Supported functions: contains, startswith, endswith, substringof.`
1067-
);
1069+
});
10681070
}
10691071

10701072
/**
@@ -1092,28 +1094,31 @@ export class ODataV4Plugin implements RuntimePlugin {
10921094
depth--;
10931095
// Check for negative depth (closing before opening)
10941096
if (depth < 0) {
1095-
throw new Error(
1096-
`Invalid $filter expression: Mismatched parentheses. ` +
1097+
throw new ObjectQLError({
1098+
code: 'PROTOCOL_INVALID_REQUEST',
1099+
message: `Invalid $filter expression: Mismatched parentheses. ` +
10971100
`Found closing ')' without matching opening '(' at position ${i}.`
1098-
);
1101+
});
10991102
}
11001103
}
11011104
}
11021105
}
11031106

11041107
// Check final depth is zero
11051108
if (depth !== 0) {
1106-
throw new Error(
1107-
`Invalid $filter expression: Mismatched parentheses. ` +
1109+
throw new ObjectQLError({
1110+
code: 'PROTOCOL_INVALID_REQUEST',
1111+
message: `Invalid $filter expression: Mismatched parentheses. ` +
11081112
`${depth} unclosed opening parenthesis(es).`
1109-
);
1113+
});
11101114
}
11111115

11121116
// Check quotes are balanced
11131117
if (inQuotes) {
1114-
throw new Error(
1115-
`Invalid $filter expression: Unclosed quoted string.`
1116-
);
1118+
throw new ObjectQLError({
1119+
code: 'PROTOCOL_INVALID_REQUEST',
1120+
message: `Invalid $filter expression: Unclosed quoted string.`
1121+
});
11171122
}
11181123
}
11191124

@@ -1464,7 +1469,7 @@ export class ODataV4Plugin implements RuntimePlugin {
14641469
const errorMatch = response.match(/{"error":\s*({[^}]+}|"[^"]+")}/);
14651470
const errorDetail = errorMatch ? errorMatch[0] : 'Unknown error';
14661471

1467-
throw new Error(`Changeset operation ${i + 1}/${requests.length} failed: ${errorDetail}`);
1472+
throw new ObjectQLError({ code: 'PROTOCOL_BATCH_ERROR', message: `Changeset operation ${i + 1}/${requests.length} failed: ${errorDetail}` });
14681473
}
14691474

14701475
tempResults.push(response);

0 commit comments

Comments
 (0)