2.8 KiB
2.8 KiB
Error Class Consistency in Composio SDK
This document outlines the standardized pattern for error classes in the Composio SDK.
Standardized Error Class Pattern
All error classes in the Composio SDK follow this standard pattern:
export class SomeSpecificError extends ComposioError {
constructor(
message: string = 'Default error message',
options: Omit<ComposioErrorOptions, 'code'> = {}
) {
super(message, {
...options,
code: ERROR_CODE_CONSTANT,
possibleFixes: options.possibleFixes || [
'Default fix suggestion 1',
'Default fix suggestion 2',
],
});
this.name = 'SomeSpecificError';
}
}
Key Standardization Points
- Message Parameter: All constructors accept a message string with a default value.
- Options Object: All constructors take an options object (rather than individual properties).
- Default Values: Default values are provided for both the message and options parameters.
- Preserving Options: All options are preserved with
...optionsand only specific properties are overridden. - Default Fixes: Default
possibleFixesare provided but can be overridden. - Name Property: Each error class sets its
nameproperty to match the class name.
Special Cases
Some error classes have specific requirements:
-
ValidationError: Accepts a
zodErrorin the options.new ValidationError('Message', { cause: someZodError }); -
ComposioToolExecutionError: Accepts an
originalErrorin the options.new ComposioToolExecutionError('Message', { originalError: someError });
Using Error Classes
// Basic usage
throw new ComposioNoAPIKeyError();
// With custom message
throw new ComposioToolNotFoundError('Could not find the specified tool');
// With additional options
throw new ComposioConnectedAccountNotFoundError('Account not found', {
meta: {
accountId: '12345',
userId: 'user123',
},
});
// Special cases
try {
// Some code that might throw
} catch (error) {
// Handle tool execution errors
throw new ComposioToolExecutionError('Tool failed', {
originalError: error,
meta: { toolId: 'some-tool' },
});
// Handle validation errors
throw new ValidationError('Validation failed', {
zodError: someZodError,
});
}
Utility Methods
All error classes inherit these helpful methods from ComposioError:
- toString(): Returns a formatted string representation
- prettyPrint(): Prints a formatted error message to the console
- toJSON(): Returns a JSON representation of the error
Static Factory Methods
- ComposioError.createAndPrint(): Creates and prints an error in one step
- ComposioError.handle(): Generic error handler for any error type