@optimizely-opal/opal-tool-ocp-sdk 1.0.0-beta.7 → 1.0.0-beta.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/auth/AuthUtils.d.ts +7 -0
- package/dist/auth/AuthUtils.d.ts.map +1 -1
- package/dist/auth/AuthUtils.js +27 -0
- package/dist/auth/AuthUtils.js.map +1 -1
- package/dist/auth/AuthUtils.test.js +99 -0
- package/dist/auth/AuthUtils.test.js.map +1 -1
- package/dist/function/GlobalToolFunction.d.ts +1 -1
- package/dist/function/GlobalToolFunction.d.ts.map +1 -1
- package/dist/function/GlobalToolFunction.js +4 -1
- package/dist/function/GlobalToolFunction.js.map +1 -1
- package/dist/function/ToolFunction.d.ts +1 -1
- package/dist/function/ToolFunction.d.ts.map +1 -1
- package/dist/function/ToolFunction.js +12 -2
- package/dist/function/ToolFunction.js.map +1 -1
- package/dist/function/ToolFunction.test.js +206 -0
- package/dist/function/ToolFunction.test.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -1
- package/dist/index.js.map +1 -1
- package/dist/logging/ToolLogger.d.ts.map +1 -1
- package/dist/logging/ToolLogger.js +2 -1
- package/dist/logging/ToolLogger.js.map +1 -1
- package/dist/logging/ToolLogger.test.js +114 -2
- package/dist/logging/ToolLogger.test.js.map +1 -1
- package/dist/service/Service.d.ts +73 -0
- package/dist/service/Service.d.ts.map +1 -1
- package/dist/service/Service.js +188 -42
- package/dist/service/Service.js.map +1 -1
- package/dist/service/Service.test.js +380 -34
- package/dist/service/Service.test.js.map +1 -1
- package/dist/types/Models.d.ts +3 -1
- package/dist/types/Models.d.ts.map +1 -1
- package/dist/types/Models.js +5 -1
- package/dist/types/Models.js.map +1 -1
- package/dist/types/ToolError.d.ts +13 -0
- package/dist/types/ToolError.d.ts.map +1 -1
- package/dist/types/ToolError.js +30 -2
- package/dist/types/ToolError.js.map +1 -1
- package/dist/types/ToolError.test.js +27 -3
- package/dist/types/ToolError.test.js.map +1 -1
- package/dist/validation/ParameterValidator.test.js +2 -1
- package/dist/validation/ParameterValidator.test.js.map +1 -1
- package/package.json +1 -1
- package/src/auth/AuthUtils.test.ts +115 -1
- package/src/auth/AuthUtils.ts +30 -1
- package/src/function/GlobalToolFunction.ts +5 -2
- package/src/function/ToolFunction.test.ts +220 -0
- package/src/function/ToolFunction.ts +13 -4
- package/src/index.ts +1 -1
- package/src/logging/ToolLogger.test.ts +118 -2
- package/src/logging/ToolLogger.ts +2 -1
- package/src/service/Service.test.ts +474 -32
- package/src/service/Service.ts +245 -41
- package/src/types/Models.ts +3 -1
- package/src/types/ToolError.test.ts +33 -3
- package/src/types/ToolError.ts +32 -2
- package/src/validation/ParameterValidator.test.ts +4 -1
package/src/service/Service.ts
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
import { AuthRequirement, IslandResponse, Parameter } from '../types/Models';
|
|
3
3
|
import { ToolError } from '../types/ToolError';
|
|
4
4
|
import * as App from '@zaiusinc/app-sdk';
|
|
5
|
-
import { logger, Headers } from '@zaiusinc/app-sdk';
|
|
5
|
+
import { logger, Headers, getAppContext } from '@zaiusinc/app-sdk';
|
|
6
6
|
import { ToolFunction } from '../function/ToolFunction';
|
|
7
7
|
import { GlobalToolFunction } from '../function/GlobalToolFunction';
|
|
8
8
|
import { ParameterValidator } from '../validation/ParameterValidator';
|
|
@@ -17,6 +17,27 @@ export class NestedInteractions {
|
|
|
17
17
|
}
|
|
18
18
|
}
|
|
19
19
|
|
|
20
|
+
/**
|
|
21
|
+
* Interface for tool description override data (compatible with KV store)
|
|
22
|
+
*/
|
|
23
|
+
interface ToolOverride extends App.ValueHash {
|
|
24
|
+
name: string;
|
|
25
|
+
description: string;
|
|
26
|
+
parameters: ParameterOverride[];
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
interface ParameterOverride extends App.ValueHash {
|
|
30
|
+
name: string;
|
|
31
|
+
description: string;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Interface for stored override data in KV store
|
|
36
|
+
*/
|
|
37
|
+
interface StoredOverrides extends App.ValueHash {
|
|
38
|
+
[tool_name: string]: ToolOverride;
|
|
39
|
+
}
|
|
40
|
+
|
|
20
41
|
/**
|
|
21
42
|
* Result type for interaction handlers
|
|
22
43
|
*/
|
|
@@ -96,6 +117,107 @@ export class ToolsService {
|
|
|
96
117
|
private functions: Map<string, Tool<any>> = new Map();
|
|
97
118
|
private interactions: Map<string, Interaction<any>> = new Map();
|
|
98
119
|
|
|
120
|
+
/**
|
|
121
|
+
* Generate KV store key for tool overrides
|
|
122
|
+
* @param appVersionId App version ID
|
|
123
|
+
* @param functionName Function name
|
|
124
|
+
* @returns KV store key
|
|
125
|
+
*/
|
|
126
|
+
private getOverrideKey(appVersionId: string, functionName: string): string {
|
|
127
|
+
return `${appVersionId}:${functionName}:opal-tools-overrides`;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Get tool overrides from KV store
|
|
132
|
+
* @param appVersionId App version ID
|
|
133
|
+
* @param functionName Function name
|
|
134
|
+
* @returns Stored overrides or null if not found
|
|
135
|
+
*/
|
|
136
|
+
private async getOverrides(appVersionId: string, functionName: string): Promise<StoredOverrides> {
|
|
137
|
+
const key = this.getOverrideKey(appVersionId, functionName);
|
|
138
|
+
return await App.storage.kvStore.get<StoredOverrides>(key);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Apply overrides to tool definitions
|
|
143
|
+
* @param tools Original tool definitions
|
|
144
|
+
* @param overrides Override data
|
|
145
|
+
* @returns Tools with overrides applied
|
|
146
|
+
*/
|
|
147
|
+
private applyOverrides(tools: Array<Tool<any>>, overrides: StoredOverrides): Array<Tool<any>> {
|
|
148
|
+
return tools.map((tool) => {
|
|
149
|
+
const override = overrides[tool.name];
|
|
150
|
+
if (!override) {
|
|
151
|
+
return tool;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
// Clone the tool and apply overrides
|
|
155
|
+
const overriddenTool = new Tool(
|
|
156
|
+
tool.name,
|
|
157
|
+
override.description,
|
|
158
|
+
tool.parameters.map((param) => {
|
|
159
|
+
const paramOverride = override.parameters?.find((p) => p.name === param.name);
|
|
160
|
+
if (paramOverride) {
|
|
161
|
+
return new Parameter(
|
|
162
|
+
param.name,
|
|
163
|
+
param.type,
|
|
164
|
+
paramOverride.description,
|
|
165
|
+
param.required
|
|
166
|
+
);
|
|
167
|
+
}
|
|
168
|
+
return param;
|
|
169
|
+
}),
|
|
170
|
+
tool.endpoint,
|
|
171
|
+
tool.handler,
|
|
172
|
+
tool.authRequirements
|
|
173
|
+
);
|
|
174
|
+
overriddenTool.httpMethod = tool.httpMethod;
|
|
175
|
+
return overriddenTool;
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* Save tool description overrides
|
|
181
|
+
* @param appVersionId App version ID
|
|
182
|
+
* @param functionName Function name
|
|
183
|
+
* @param overrideData Override data from request
|
|
184
|
+
*/
|
|
185
|
+
public async saveToolOverrides(
|
|
186
|
+
appVersionId: string,
|
|
187
|
+
functionName: string,
|
|
188
|
+
overrides: StoredOverrides
|
|
189
|
+
): Promise<void> {
|
|
190
|
+
const key = this.getOverrideKey(appVersionId, functionName);
|
|
191
|
+
await App.storage.kvStore.patch(key, overrides);
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* Delete tool description overrides
|
|
196
|
+
* @param appVersionId App version ID
|
|
197
|
+
* @param functionName Function name
|
|
198
|
+
*/
|
|
199
|
+
public async deleteToolOverrides(appVersionId: string, functionName: string): Promise<void> {
|
|
200
|
+
const key = this.getOverrideKey(appVersionId, functionName);
|
|
201
|
+
await App.storage.kvStore.delete(key);
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* Get tool definitions with overrides applied
|
|
206
|
+
* @param appVersionId App version ID
|
|
207
|
+
* @param functionName Function name
|
|
208
|
+
* @returns Tool definitions with overrides applied
|
|
209
|
+
*/
|
|
210
|
+
public async getToolsWithOverrides(appVersionId: string, functionName: string): Promise<Array<Tool<any>>> {
|
|
211
|
+
const tools = Array.from(this.functions.values());
|
|
212
|
+
const overrides = await this.getOverrides(appVersionId, functionName);
|
|
213
|
+
|
|
214
|
+
if (overrides) {
|
|
215
|
+
return this.applyOverrides(tools, overrides);
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
return tools;
|
|
219
|
+
}
|
|
220
|
+
|
|
99
221
|
/**
|
|
100
222
|
* Enforce OptiID authentication for tools by ensuring OptiID auth requirement is present
|
|
101
223
|
* @param authRequirements Original authentication requirements
|
|
@@ -200,59 +322,141 @@ export class ToolsService {
|
|
|
200
322
|
req: App.Request,
|
|
201
323
|
functionContext: ToolFunction | GlobalToolFunction
|
|
202
324
|
): Promise<App.Response> {
|
|
325
|
+
// Handle discovery endpoint with overrides
|
|
203
326
|
if (req.path === '/discovery') {
|
|
204
|
-
return
|
|
205
|
-
}
|
|
206
|
-
const func = this.functions.get(req.path);
|
|
207
|
-
if (func) {
|
|
208
|
-
try {
|
|
209
|
-
let params;
|
|
210
|
-
if (req.bodyJSON && req.bodyJSON.parameters) {
|
|
211
|
-
params = req.bodyJSON.parameters;
|
|
212
|
-
} else {
|
|
213
|
-
params = req.bodyJSON;
|
|
214
|
-
}
|
|
327
|
+
return await this.handleDiscoveryRequest(functionContext);
|
|
328
|
+
}
|
|
215
329
|
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
}
|
|
330
|
+
// Handle overrides endpoint
|
|
331
|
+
if (req.path === '/overrides') {
|
|
332
|
+
return await this.handleOverridesRequest(req, functionContext);
|
|
333
|
+
}
|
|
221
334
|
|
|
222
|
-
|
|
223
|
-
|
|
335
|
+
// Handle regular tool functions
|
|
336
|
+
const func = this.functions.get(req.path);
|
|
337
|
+
if (func) {
|
|
338
|
+
try {
|
|
339
|
+
let params;
|
|
340
|
+
if (req.bodyJSON && req.bodyJSON.parameters) {
|
|
341
|
+
params = req.bodyJSON.parameters;
|
|
342
|
+
} else {
|
|
343
|
+
params = req.bodyJSON;
|
|
344
|
+
}
|
|
224
345
|
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
return this.formatErrorResponse(error, func.endpoint);
|
|
346
|
+
// Validate parameters before calling the handler (only if tool has parameter definitions)
|
|
347
|
+
// ParameterValidator.validate() throws ToolError if validation fails
|
|
348
|
+
if (func.parameters && func.parameters.length > 0) {
|
|
349
|
+
ParameterValidator.validate(params, func.parameters, func.endpoint);
|
|
230
350
|
}
|
|
351
|
+
|
|
352
|
+
// Extract auth data from body JSON
|
|
353
|
+
const authData = req.bodyJSON ? req.bodyJSON.auth : undefined;
|
|
354
|
+
const result = await func.handler(functionContext, params, authData);
|
|
355
|
+
return new App.Response(200, result);
|
|
356
|
+
} catch (error: any) {
|
|
357
|
+
logger.error(`Error in function ${func.name}:`, error);
|
|
358
|
+
return this.formatErrorResponse(error, func.endpoint);
|
|
231
359
|
}
|
|
360
|
+
}
|
|
232
361
|
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
362
|
+
// Handle interactions
|
|
363
|
+
const interaction = this.interactions.get(req.path);
|
|
364
|
+
if (interaction) {
|
|
365
|
+
try {
|
|
366
|
+
let params;
|
|
367
|
+
if (req.bodyJSON && req.bodyJSON.data) {
|
|
368
|
+
params = req.bodyJSON.data;
|
|
369
|
+
} else {
|
|
370
|
+
params = req.bodyJSON;
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
// Extract auth data from body JSON
|
|
374
|
+
const authData = req.bodyJSON ? req.bodyJSON.auth : undefined;
|
|
375
|
+
|
|
376
|
+
const result = await interaction.handler(functionContext, params, authData);
|
|
377
|
+
return new App.Response(200, result);
|
|
378
|
+
} catch (error: any) {
|
|
379
|
+
logger.error(`Error in function ${interaction.name}:`, error);
|
|
380
|
+
return this.formatErrorResponse(error, interaction.endpoint);
|
|
381
|
+
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
return new App.Response(404, 'Function not found');
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
/**
|
|
388
|
+
* Handle discovery endpoint with overrides applied
|
|
389
|
+
* @param functionContext The function context to get function name from
|
|
390
|
+
* @returns Response with tool definitions
|
|
391
|
+
*/
|
|
392
|
+
private async handleDiscoveryRequest(functionContext: ToolFunction | GlobalToolFunction): Promise<App.Response> {
|
|
393
|
+
try {
|
|
394
|
+
// Get app version from app context
|
|
395
|
+
const appVersionId = getAppContext().manifest.meta.version;
|
|
396
|
+
// Get function name from function context
|
|
397
|
+
const functionName = functionContext.constructor.name;
|
|
398
|
+
|
|
399
|
+
const toolsWithOverrides = await this.getToolsWithOverrides(appVersionId, functionName);
|
|
400
|
+
return new App.Response(200, { functions: toolsWithOverrides.map((f) => f.toJSON()) });
|
|
401
|
+
} catch (error: any) {
|
|
402
|
+
logger.error('Error getting tools with overrides:', error);
|
|
403
|
+
// Fallback to original tools if override fetch fails
|
|
404
|
+
return new App.Response(200, { functions: Array.from(this.functions.values()).map((f) => f.toJSON()) });
|
|
405
|
+
}
|
|
406
|
+
}
|
|
242
407
|
|
|
243
|
-
|
|
244
|
-
|
|
408
|
+
/**
|
|
409
|
+
* Handle overrides endpoint for saving and deleting tool description overrides
|
|
410
|
+
* @param req The request object
|
|
411
|
+
* @param functionContext The function context to get function name from
|
|
412
|
+
* @returns Response indicating success or failure
|
|
413
|
+
*/
|
|
414
|
+
private async handleOverridesRequest(
|
|
415
|
+
req: App.Request,
|
|
416
|
+
functionContext: ToolFunction | GlobalToolFunction
|
|
417
|
+
): Promise<App.Response> {
|
|
418
|
+
if (req.method === 'PATCH') {
|
|
419
|
+
try {
|
|
420
|
+
// Get app version from app context
|
|
421
|
+
const appVersionId = getAppContext().manifest.meta.version;
|
|
422
|
+
// Get function name from function context
|
|
423
|
+
const functionName = functionContext.constructor.name;
|
|
245
424
|
|
|
246
|
-
|
|
247
|
-
return new App.Response(
|
|
248
|
-
} catch (error: any) {
|
|
249
|
-
logger.error(`Error in function ${interaction.name}:`, error);
|
|
250
|
-
return this.formatErrorResponse(error, interaction.endpoint);
|
|
425
|
+
if (!req.bodyJSON?.functions || !Array.isArray(req.bodyJSON.functions)) {
|
|
426
|
+
return new App.Response(400, { error: 'Invalid request body. Expected functions array.' });
|
|
251
427
|
}
|
|
428
|
+
|
|
429
|
+
// Convert array format to map format for storage
|
|
430
|
+
const overrideData: StoredOverrides = (req.bodyJSON.functions as ToolOverride[]).reduce(
|
|
431
|
+
(map: StoredOverrides, tool: ToolOverride) => {
|
|
432
|
+
map[tool.name] = tool;
|
|
433
|
+
return map;
|
|
434
|
+
},
|
|
435
|
+
{}
|
|
436
|
+
);
|
|
437
|
+
|
|
438
|
+
await this.saveToolOverrides(appVersionId, functionName, overrideData);
|
|
439
|
+
return new App.Response(200, { success: true });
|
|
440
|
+
} catch (error: any) {
|
|
441
|
+
logger.error('Error saving tool overrides:', error);
|
|
442
|
+
return new App.Response(500, { error: error.message || 'Unknown error' });
|
|
252
443
|
}
|
|
444
|
+
} else if (req.method === 'DELETE') {
|
|
445
|
+
try {
|
|
446
|
+
// Get app version from app context
|
|
447
|
+
const appVersionId = getAppContext().manifest.meta.version;
|
|
448
|
+
// Get function name from function context
|
|
449
|
+
const functionName = functionContext.constructor.name;
|
|
253
450
|
|
|
254
|
-
|
|
451
|
+
await this.deleteToolOverrides(appVersionId, functionName);
|
|
452
|
+
return new App.Response(200, { success: true });
|
|
453
|
+
} catch (error: any) {
|
|
454
|
+
logger.error('Error deleting tool overrides:', error);
|
|
455
|
+
return new App.Response(500, { error: error.message || 'Unknown error' });
|
|
456
|
+
}
|
|
255
457
|
}
|
|
458
|
+
|
|
459
|
+
return new App.Response(405, { error: 'Method not allowed' });
|
|
256
460
|
}
|
|
257
461
|
}
|
|
258
462
|
|
package/src/types/Models.ts
CHANGED
|
@@ -5,7 +5,8 @@ describe('ToolError', () => {
|
|
|
5
5
|
it('should create error with message and default status 500', () => {
|
|
6
6
|
const error = new ToolError('Something went wrong');
|
|
7
7
|
|
|
8
|
-
expect(error.message).toBe('Something went wrong');
|
|
8
|
+
expect(error.message).toBe('[500] Something went wrong');
|
|
9
|
+
expect(error.title).toBe('Something went wrong');
|
|
9
10
|
expect(error.status).toBe(500);
|
|
10
11
|
expect(error.detail).toBeUndefined();
|
|
11
12
|
expect(error.name).toBe('ToolError');
|
|
@@ -14,7 +15,8 @@ describe('ToolError', () => {
|
|
|
14
15
|
it('should create error with custom status code', () => {
|
|
15
16
|
const error = new ToolError('Not found', 404);
|
|
16
17
|
|
|
17
|
-
expect(error.message).toBe('Not found');
|
|
18
|
+
expect(error.message).toBe('[404] Not found');
|
|
19
|
+
expect(error.title).toBe('Not found');
|
|
18
20
|
expect(error.status).toBe(404);
|
|
19
21
|
expect(error.detail).toBeUndefined();
|
|
20
22
|
});
|
|
@@ -22,11 +24,39 @@ describe('ToolError', () => {
|
|
|
22
24
|
it('should create error with message, status, and detail', () => {
|
|
23
25
|
const error = new ToolError('Validation failed', 400, 'Email format is invalid');
|
|
24
26
|
|
|
25
|
-
expect(error.message).toBe('Validation failed');
|
|
27
|
+
expect(error.message).toBe('[400] Validation failed: Email format is invalid');
|
|
28
|
+
expect(error.title).toBe('Validation failed');
|
|
26
29
|
expect(error.status).toBe(400);
|
|
27
30
|
expect(error.detail).toBe('Email format is invalid');
|
|
28
31
|
});
|
|
29
32
|
|
|
33
|
+
it('should create error with errors array in message', () => {
|
|
34
|
+
const errors = [
|
|
35
|
+
{ field: 'email', message: 'Invalid email format' },
|
|
36
|
+
{ field: 'age', message: 'Must be a positive number' }
|
|
37
|
+
];
|
|
38
|
+
const error = new ToolError('Validation failed', 400, "See 'errors' field for details.", errors);
|
|
39
|
+
|
|
40
|
+
expect(error.message).toBe(
|
|
41
|
+
'[400] Validation failed: email (Invalid email format); age (Must be a positive number)'
|
|
42
|
+
);
|
|
43
|
+
expect(error.title).toBe('Validation failed');
|
|
44
|
+
expect(error.status).toBe(400);
|
|
45
|
+
expect(error.detail).toBe("See 'errors' field for details.");
|
|
46
|
+
expect(error.errors).toEqual(errors);
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it('should create error with single error in message', () => {
|
|
50
|
+
const errors = [{ field: 'username', message: 'Required field is missing' }];
|
|
51
|
+
const error = new ToolError('Validation failed', 400, undefined, errors);
|
|
52
|
+
|
|
53
|
+
expect(error.message).toBe('[400] Validation failed: username (Required field is missing)');
|
|
54
|
+
expect(error.title).toBe('Validation failed');
|
|
55
|
+
expect(error.status).toBe(400);
|
|
56
|
+
expect(error.detail).toBeUndefined();
|
|
57
|
+
expect(error.errors).toEqual(errors);
|
|
58
|
+
});
|
|
59
|
+
|
|
30
60
|
it('should be an instance of Error', () => {
|
|
31
61
|
const error = new ToolError('Test error');
|
|
32
62
|
|
package/src/types/ToolError.ts
CHANGED
|
@@ -9,18 +9,27 @@ export interface ValidationError {
|
|
|
9
9
|
/**
|
|
10
10
|
* Custom error class for tool functions that supports RFC 9457 Problem Details format
|
|
11
11
|
*
|
|
12
|
+
* The error message includes status code and details for better logging:
|
|
13
|
+
* - No detail: "[status] message"
|
|
14
|
+
* - With detail: "[status] message: detail"
|
|
15
|
+
* - With errors: "[status] message: field1 (error1); field2 (error2)"
|
|
16
|
+
*
|
|
12
17
|
* @example
|
|
13
18
|
* ```typescript
|
|
14
19
|
* // Throw a 404 error
|
|
20
|
+
* // Message: "[404] Resource not found: The requested task does not exist"
|
|
15
21
|
* throw new ToolError('Resource not found', 404, 'The requested task does not exist');
|
|
16
22
|
*
|
|
17
23
|
* // Throw a 400 error
|
|
24
|
+
* // Message: "[400] Invalid input: The priority must be "low", "medium", or "high""
|
|
18
25
|
* throw new ToolError('Invalid input', 400, 'The priority must be "low", "medium", or "high"');
|
|
19
26
|
*
|
|
20
27
|
* // Throw a 500 error (default)
|
|
28
|
+
* // Message: "[500] Database connection failed"
|
|
21
29
|
* throw new ToolError('Database connection failed');
|
|
22
30
|
*
|
|
23
31
|
* // Throw a validation error with multiple field errors
|
|
32
|
+
* // Message: "[400] Validation failed: email (Invalid email format); age (Must be a positive number)"
|
|
24
33
|
* throw new ToolError('Validation failed', 400, "See 'errors' field for details.", [
|
|
25
34
|
* { field: 'email', message: 'Invalid email format' },
|
|
26
35
|
* { field: 'age', message: 'Must be a positive number' }
|
|
@@ -43,6 +52,11 @@ export class ToolError extends Error {
|
|
|
43
52
|
*/
|
|
44
53
|
public readonly errors?: ValidationError[];
|
|
45
54
|
|
|
55
|
+
/**
|
|
56
|
+
* The title field for RFC 9457 format (same as message when no detail is provided)
|
|
57
|
+
*/
|
|
58
|
+
public readonly title: string;
|
|
59
|
+
|
|
46
60
|
/**
|
|
47
61
|
* Create a new ToolError
|
|
48
62
|
*
|
|
@@ -57,8 +71,24 @@ export class ToolError extends Error {
|
|
|
57
71
|
detail?: string,
|
|
58
72
|
errors?: ValidationError[]
|
|
59
73
|
) {
|
|
60
|
-
|
|
74
|
+
// Build comprehensive error message for logging/debugging
|
|
75
|
+
// Format: [status] message: details
|
|
76
|
+
let fullMessage = `[${status}] ${message}`;
|
|
77
|
+
|
|
78
|
+
if (errors && errors.length > 0) {
|
|
79
|
+
// Errors take precedence: field1 (message1); field2 (message2)
|
|
80
|
+
const errorDetails = errors
|
|
81
|
+
.map((err) => `${err.field} (${err.message})`)
|
|
82
|
+
.join('; ');
|
|
83
|
+
fullMessage += `: ${errorDetails}`;
|
|
84
|
+
} else if (detail) {
|
|
85
|
+
// Include detail if no errors
|
|
86
|
+
fullMessage += `: ${detail}`;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
super(fullMessage);
|
|
61
90
|
this.name = 'ToolError';
|
|
91
|
+
this.title = message;
|
|
62
92
|
this.status = status;
|
|
63
93
|
this.detail = detail;
|
|
64
94
|
this.errors = errors;
|
|
@@ -77,7 +107,7 @@ export class ToolError extends Error {
|
|
|
77
107
|
*/
|
|
78
108
|
public toProblemDetails(instance: string): Record<string, unknown> {
|
|
79
109
|
const problemDetails: Record<string, unknown> = {
|
|
80
|
-
title: this.
|
|
110
|
+
title: this.title,
|
|
81
111
|
status: this.status,
|
|
82
112
|
instance
|
|
83
113
|
};
|
|
@@ -49,7 +49,10 @@ describe('ParameterValidator', () => {
|
|
|
49
49
|
expect(error).toBeInstanceOf(ToolError);
|
|
50
50
|
const toolError = error as ToolError;
|
|
51
51
|
expect(toolError.status).toBe(400);
|
|
52
|
-
expect(toolError.message).toBe(
|
|
52
|
+
expect(toolError.message).toBe(
|
|
53
|
+
"[400] One or more validation errors occurred.: email (Required parameter 'email' is missing)"
|
|
54
|
+
);
|
|
55
|
+
expect(toolError.title).toBe('One or more validation errors occurred.');
|
|
53
56
|
expect(toolError.detail).toBe("See 'errors' field for details.");
|
|
54
57
|
expect(toolError.errors).toHaveLength(1);
|
|
55
58
|
expect(toolError.errors).toEqual([{
|