Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
TypeScript agentic-aiagentsaiai-agentsaiagentsdeveloper-toolsfunction-callinggpt-4javascriptjsllmllmopsmcppythonremote-mcp-serverssetypescript
fix: throw error instead of calling process.exit() when API key is missing (#2206)
devin-ai-integration[bot]73a2fb10c236
diff --git a/ts/docs/advanced/error-handling.md b/ts/docs/advanced/error-handling.mdindex 3a926d925..a7b1efe55 100644--- a/ts/docs/advanced/error-handling.md+++ b/ts/docs/advanced/error-handling.md@@ -277,13 +277,6 @@ try {// Include stack traceComposioError.handle(error, { includeStack: true });-- // Exit process with code 1 after displaying the error- ComposioError.handle(error, {- includeStack: true,- exitProcess: true,- exitCode: 1,- });}```@@ -292,7 +285,29 @@ This method:- Automatically detects Composio errors and uses `prettyPrint` for them- Formats standard errors with a similar style- Handles unknown errors gracefully-- Optionally exits the process with a specified code++### Using handleAndThrow for Fatal Errors++For fatal errors that should stop execution, use the `handleAndThrow` method which displays the error and then throws it:++```typescript+try {+ // Some operation that might fail+} catch (error) {+ // Display the error and then throw it (for fatal errors)+ ComposioError.handleAndThrow(error);++ // Include stack trace before throwing+ ComposioError.handleAndThrow(error, true);+}+```++This method:++- Displays the error using the same formatting as `handle()`+- Always throws the error after displaying it+- Returns `never` type, indicating it always throws+- Is compatible with serverless environments (unlike `process.exit()`)### Creating and Printing Errorsdiff --git a/ts/packages/core/src/errors/ComposioError.ts b/ts/packages/core/src/errors/ComposioError.tsindex 603269a47..89d81c963 100644--- a/ts/packages/core/src/errors/ComposioError.ts+++ b/ts/packages/core/src/errors/ComposioError.ts@@ -19,9 +19,9 @@ import logger from '../utils/logger';export interface ErrorHandleOptions {/** Whether to include the stack trace in the output */includeStack?: boolean;- /** Whether to exit the process after handling */+ /** @deprecated Use ComposioError.handleAndThrow() instead. This option will be removed in a future version. */exitProcess?: boolean;- /** The exit code to use if exiting the process */+ /** @deprecated This option is no longer used. */exitCode?: number;}@@ -272,12 +272,12 @@ export class ComposioError extends Error {/*** Utility function to handle errors in a consistent way- * This properly displays the error and exits the process if needed+ * This properly displays the error without throwing* @param error The error to handle* @param options Options for error handling*/static handle(error: unknown, options: ErrorHandleOptions = {}): void {- const { includeStack = false, exitProcess = false, exitCode = 1 } = options;+ const { includeStack = false, exitProcess = false } = options;if (error instanceof ComposioError) {// For Composio errors, use pretty printing@@ -293,9 +293,37 @@ export class ComposioError extends Error {this.handleUnknownError(error);}- // Exit the process if requested- if (exitProcess && typeof process !== 'undefined') {- process.exit(exitCode);+ // @deprecated - exitProcess is deprecated, use handleAndThrow() instead+ if (exitProcess) {+ this.throwError(error);+ }+ }++ /**+ * Utility function to handle errors and then throw them+ * This properly displays the error and then throws it, allowing callers to catch it.+ * Use this for fatal errors that should stop execution.+ * @param error The error to handle and throw+ * @param includeStack Whether to include the stack trace in the output+ * @throws ComposioError - Always throws after displaying the error+ */+ static handleAndThrow(error: unknown, includeStack = false): never {+ this.handle(error, { includeStack });+ this.throwError(error);+ }++ /**+ * Helper method to throw an error as a ComposioError+ * @param error The error to throw+ * @private+ */+ private static throwError(error: unknown): never {+ if (error instanceof ComposioError) {+ throw error;+ } else if (error instanceof Error) {+ throw new ComposioError(error.message, { cause: error });+ } else {+ throw new ComposioError(String(error));}}diff --git a/ts/packages/core/src/utils/sdk.ts b/ts/packages/core/src/utils/sdk.tsindex cf8134bba..e940f2454 100644--- a/ts/packages/core/src/utils/sdk.ts+++ b/ts/packages/core/src/utils/sdk.ts@@ -46,9 +46,7 @@ export function getSDKConfig(baseUrl?: string | null, apiKey?: string | null) {const apiKeyParsed = apiKey || getEnvVariable('COMPOSIO_API_KEY') || apiKeyFromUserConfig || '';if (!apiKeyParsed) {- ComposioError.handle(new ComposioNoAPIKeyError(), {- exitProcess: true,- });+ ComposioError.handleAndThrow(new ComposioNoAPIKeyError());}logger.debug('Environment', `API Key: ${apiKeyParsed}`);