1
-import { APIError } from '@composio/client';
1
+import { APIError, type APIErrorBody } from '@composio/client';
2
import { ComposioError, ComposioErrorOptions } from './ComposioError';
3
import { ComposioConnectedAccountNotFoundError } from './ConnectedAccountsErrors';
4
import { getRetryAfterSeconds, isRateLimitResponse } from '../utils/rateLimit';
146
}
147
}
148
149
+/** Options for a `ComposioToolExecutionError`. */
150
+export type ComposioToolExecutionErrorOptions = ComposioErrorOptions & {
151
+ /** The numeric `error.code` of the Composio API error response. */
152
+ apiErrorCode?: number;
153
+};
154
+
155
export class ComposioToolExecutionError extends ComposioError {
150
- constructor(message: string = 'Tool execution error', options: ComposioErrorOptions = {}) {
156
+ /**
157
+ * The numeric `error.code` the Composio API sent when it rejected the execution.
158
+ * `code` stays the SDK's `TS-SDK::` code.
159
+ */
160
+ declare public readonly apiErrorCode?: number;
161
+
162
+ constructor(
163
+ message: string = 'Tool execution error',
164
+ options: ComposioToolExecutionErrorOptions = {}
165
+ ) {
166
+ const { apiErrorCode, ...errorOptions } = options;
167
super(message, {
152
- ...options,
153
- code: options.code || ToolErrorCodes.TOOL_EXECUTION_ERROR,
154
- cause: options.cause,
155
- possibleFixes: options.possibleFixes || [
168
+ ...errorOptions,
169
+ code: errorOptions.code || ToolErrorCodes.TOOL_EXECUTION_ERROR,
170
+ cause: errorOptions.cause,
171
+ possibleFixes: errorOptions.possibleFixes || [
172
'Ensure the tool is correctly configured and the input is valid',
173
'Ensure the userId is correct and has an active connected account for the user in case of non NoAuth toolkits',
174
],
175
});
176
+ if (apiErrorCode !== undefined) {
177
+ this.apiErrorCode = apiErrorCode;
178
+ }
179
180
this.name = 'ComposioToolExecutionError';
181
}
339
this.name = 'ComposioToolVersionRequiredError';
340
}
341
}
323
-export interface ComposioAPIServerErrorBody {
324
- error: {
325
- message: string;
326
- code: number;
327
- status: number;
328
- request_id: string;
329
- suggested_fix: string;
330
- };
331
-}
342
+/** The error envelope of a Composio API error response, as `@composio/client` validates it. */
343
+export type ComposioAPIServerErrorBody = APIErrorBody;
344
345
// Map of error codes to their corresponding error constructors
334
-const ERROR_CODE_HANDLERS = new Map<number, (message: string) => ComposioError>([
335
- [1803, msg => new ComposioConnectedAccountNotFoundError(msg)],
346
+const ERROR_CODE_HANDLERS = new Map<number, (message: string, cause: APIError) => ComposioError>([
347
+ [1803, (msg, cause) => new ComposioConnectedAccountNotFoundError(msg, { cause })],
348
// Add more error code handlers here as needed
349
]);
350
351
+/** What to try when the API rejected an execution without a `suggested_fix`, by HTTP status. */
352
+const fallbackExecutionFixes = (status: number | undefined): string[] => {
353
+ if (status === 401) {
354
+ return ['Ensure you are using a valid Composio API key for this project'];
355
+ }
356
+ if (status === 402) {
357
+ return ['Check the plan and billing limits of your Composio organization'];
358
+ }
359
+ if (status === 403) {
360
+ return ['Ensure your API key and the connected account are allowed to run this tool'];
361
+ }
362
+ if (status === 410) {
363
+ return ['The tool or the resource it acts on is no longer available; see the error message'];
364
+ }
365
+ if (status !== undefined && status >= 500) {
366
+ return [
367
+ 'The Composio API failed to process the request; retry later',
368
+ 'If it keeps failing, contact Composio support with the request id',
369
+ ];
370
+ }
371
+ return ['Ensure the tool slug is correct and the input arguments for the tool is valid'];
372
+};
373
+
374
export const handleToolExecutionError = (tool: string, actualError: Error): ComposioError => {
340
- // Handle specific API error codes
341
- if (actualError instanceof APIError && actualError.error) {
342
- const errorBody = actualError.error as ComposioAPIServerErrorBody;
343
- const errorCode = errorBody?.error?.code;
344
- const errorMessage = errorBody?.error?.message;
375
+ const apiError = actualError instanceof APIError ? actualError : undefined;
376
+ const details = apiError?.details;
377
346
- if (errorCode && ERROR_CODE_HANDLERS.has(errorCode)) {
347
- return ERROR_CODE_HANDLERS.get(errorCode)!(errorMessage || 'An error occurred');
348
- }
378
+ // Handle specific API error codes
379
+ if (apiError && details && ERROR_CODE_HANDLERS.has(details.code)) {
380
+ return ERROR_CODE_HANDLERS.get(details.code)!(details.message || 'An error occurred', apiError);
381
}
382
383
if (isRateLimitResponse(actualError)) {
389
}
390
391
// Default error for all other cases
360
- return new ComposioToolExecutionError(`Error executing the tool ${tool}`, {
361
- cause: actualError,
362
- possibleFixes: [
363
- 'Ensure the tool slug is correct and the input arguments for the tool is valid',
364
- ],
365
- });
392
+ const serverMessage = details?.message;
393
+ return new ComposioToolExecutionError(
394
+ serverMessage
395
+ ? `Error executing the tool ${tool}: ${serverMessage}`
396
+ : `Error executing the tool ${tool}`,
397
+ {
398
+ cause: actualError,
399
+ statusCode: apiError?.status,
400
+ apiErrorCode: details?.code,
401
+ possibleFixes: details?.suggested_fix
402
+ ? [details.suggested_fix]
403
+ : fallbackExecutionFixes(apiError?.status),
404
+ }
405
+ );
406
};