Skip to content

Commit 22ad9d7

Browse files
committed
refactor: extract handleValidationError helper and include error details in validation failures
1 parent 064a3a1 commit 22ad9d7

7 files changed

Lines changed: 24 additions & 60 deletions

File tree

src/tools/MapboxApiBasedTool.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,18 @@ export abstract class MapboxApiBasedTool<
217217
};
218218
}
219219

220+
protected handleValidationError(error: unknown): CallToolResult {
221+
return {
222+
isError: true,
223+
content: [
224+
{
225+
type: 'text',
226+
text: `Unexpected API response format from Mapbox API: ${error instanceof Error ? error.message : 'Unknown validation error'}`
227+
}
228+
]
229+
};
230+
}
231+
220232
/**
221233
* Tool logic to be implemented by subclasses.
222234
* Must return a complete OutputSchema with content and optional structured content.

src/tools/create-token-tool/CreateTokenTool.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -95,16 +95,8 @@ export class CreateTokenTool extends MapboxApiBasedTool<
9595
let data;
9696
try {
9797
data = CreateTokenOutputSchema.parse(rawData);
98-
} catch {
99-
return {
100-
isError: true,
101-
content: [
102-
{
103-
type: 'text',
104-
text: 'Unexpected API response format from Mapbox API'
105-
}
106-
]
107-
};
98+
} catch (validationError) {
99+
return this.handleValidationError(validationError);
108100
}
109101

110102
this.log('info', `CreateTokenTool: Successfully created token`);

src/tools/list-styles-tool/ListStylesTool.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -73,16 +73,8 @@ export class ListStylesTool extends MapboxApiBasedTool<
7373
let validatedData;
7474
try {
7575
validatedData = StylesArraySchema.parse(rawData);
76-
} catch {
77-
return {
78-
isError: true,
79-
content: [
80-
{
81-
type: 'text',
82-
text: 'Unexpected API response format from Mapbox API'
83-
}
84-
]
85-
};
76+
} catch (validationError) {
77+
return this.handleValidationError(validationError);
8678
}
8779

8880
this.log('info', `ListStylesTool: Successfully listed styles`);

src/tools/list-tokens-tool/ListTokensTool.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -133,16 +133,8 @@ export class ListTokensTool extends MapboxApiBasedTool<
133133
let validatedTokens;
134134
try {
135135
validatedTokens = TokenObjectSchema.array().parse(tokens);
136-
} catch {
137-
return {
138-
isError: true,
139-
content: [
140-
{
141-
type: 'text',
142-
text: 'Unexpected API response format from Mapbox API'
143-
}
144-
]
145-
};
136+
} catch (validationError) {
137+
return this.handleValidationError(validationError);
146138
}
147139

148140
allTokens.push(...validatedTokens);

src/tools/retrieve-style-tool/RetrieveStyleTool.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,8 @@ export class RetrieveStyleTool extends MapboxApiBasedTool<
5757
let data: MapboxStyleOutput;
5858
try {
5959
data = MapboxStyleOutputSchema.parse(rawData);
60-
} catch {
61-
return {
62-
isError: true,
63-
content: [
64-
{
65-
type: 'text',
66-
text: 'Unexpected API response format from Mapbox API'
67-
}
68-
]
69-
};
60+
} catch (validationError) {
61+
return this.handleValidationError(validationError);
7062
}
7163

7264
this.log(

src/tools/tilequery-tool/TilequeryTool.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -84,16 +84,8 @@ export class TilequeryTool extends MapboxApiBasedTool<
8484
let data: TilequeryResponse;
8585
try {
8686
data = TilequeryResponseSchema.parse(rawData);
87-
} catch {
88-
return {
89-
isError: true,
90-
content: [
91-
{
92-
type: 'text',
93-
text: 'Unexpected API response format from Mapbox API'
94-
}
95-
]
96-
};
87+
} catch (validationError) {
88+
return this.handleValidationError(validationError);
9789
}
9890

9991
this.log(

src/tools/update-style-tool/UpdateStyleTool.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -67,16 +67,8 @@ export class UpdateStyleTool extends MapboxApiBasedTool<
6767
let data: MapboxStyleOutput;
6868
try {
6969
data = MapboxStyleOutputSchema.parse(rawData);
70-
} catch {
71-
return {
72-
isError: true,
73-
content: [
74-
{
75-
type: 'text',
76-
text: 'Unexpected API response format from Mapbox API'
77-
}
78-
]
79-
};
70+
} catch (validationError) {
71+
return this.handleValidationError(validationError);
8072
}
8173

8274
this.log('info', `UpdateStyleTool: Successfully updated style ${data.id}`);

0 commit comments

Comments
 (0)