@devries-cameron20 / composio-1 / commits / 73a2fb10c
Fork 0

Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

fix: throw error instead of calling process.exit() when API key is missing (#2206)

Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: rahul@composio.dev <rahultarak12345@gmail.com>

devin-ai-integration[bot] committed Nov 25, 2025 at 10:49 UTC 73a2fb10c236fe7d477cec5ce27d78c6c12b210e
3 files changed +59 -18
ts/docs/advanced/error-handling.md
+23 -8
@@ -277,13 +277,6 @@ try {
277
278 // Include stack trace
279 ComposioError.handle(error, { includeStack: true });
280 -
281 - // Exit process with code 1 after displaying the error
282 - ComposioError.handle(error, {
283 - includeStack: true,
284 - exitProcess: true,
285 - exitCode: 1,
286 - });
280 }
281 ```
282
@@ -292,7 +285,29 @@ This method:
285 - Automatically detects Composio errors and uses `prettyPrint` for them
286 - Formats standard errors with a similar style
287 - Handles unknown errors gracefully
295 -- Optionally exits the process with a specified code
288 +
289 +### Using handleAndThrow for Fatal Errors
290 +
291 +For fatal errors that should stop execution, use the `handleAndThrow` method which displays the error and then throws it:
292 +
293 +```typescript
294 +try {
295 + // Some operation that might fail
296 +} catch (error) {
297 + // Display the error and then throw it (for fatal errors)
298 + ComposioError.handleAndThrow(error);
299 +
300 + // Include stack trace before throwing
301 + ComposioError.handleAndThrow(error, true);
302 +}
303 +```
304 +
305 +This method:
306 +
307 +- Displays the error using the same formatting as `handle()`
308 +- Always throws the error after displaying it
309 +- Returns `never` type, indicating it always throws
310 +- Is compatible with serverless environments (unlike `process.exit()`)
311
312 ### Creating and Printing Errors
313
ts/packages/core/src/errors/ComposioError.ts
+35 -7
@@ -19,9 +19,9 @@ import logger from '../utils/logger';
19 export interface ErrorHandleOptions {
20 /** Whether to include the stack trace in the output */
21 includeStack?: boolean;
22 - /** Whether to exit the process after handling */
22 + /** @deprecated Use ComposioError.handleAndThrow() instead. This option will be removed in a future version. */
23 exitProcess?: boolean;
24 - /** The exit code to use if exiting the process */
24 + /** @deprecated This option is no longer used. */
25 exitCode?: number;
26 }
27
@@ -272,12 +272,12 @@ export class ComposioError extends Error {
272
273 /**
274 * Utility function to handle errors in a consistent way
275 - * This properly displays the error and exits the process if needed
275 + * This properly displays the error without throwing
276 * @param error The error to handle
277 * @param options Options for error handling
278 */
279 static handle(error: unknown, options: ErrorHandleOptions = {}): void {
280 - const { includeStack = false, exitProcess = false, exitCode = 1 } = options;
280 + const { includeStack = false, exitProcess = false } = options;
281
282 if (error instanceof ComposioError) {
283 // For Composio errors, use pretty printing
@@ -293,9 +293,37 @@ export class ComposioError extends Error {
293 this.handleUnknownError(error);
294 }
295
296 - // Exit the process if requested
297 - if (exitProcess && typeof process !== 'undefined') {
298 - process.exit(exitCode);
296 + // @deprecated - exitProcess is deprecated, use handleAndThrow() instead
297 + if (exitProcess) {
298 + this.throwError(error);
299 + }
300 + }
301 +
302 + /**
303 + * Utility function to handle errors and then throw them
304 + * This properly displays the error and then throws it, allowing callers to catch it.
305 + * Use this for fatal errors that should stop execution.
306 + * @param error The error to handle and throw
307 + * @param includeStack Whether to include the stack trace in the output
308 + * @throws ComposioError - Always throws after displaying the error
309 + */
310 + static handleAndThrow(error: unknown, includeStack = false): never {
311 + this.handle(error, { includeStack });
312 + this.throwError(error);
313 + }
314 +
315 + /**
316 + * Helper method to throw an error as a ComposioError
317 + * @param error The error to throw
318 + * @private
319 + */
320 + private static throwError(error: unknown): never {
321 + if (error instanceof ComposioError) {
322 + throw error;
323 + } else if (error instanceof Error) {
324 + throw new ComposioError(error.message, { cause: error });
325 + } else {
326 + throw new ComposioError(String(error));
327 }
328 }
329
ts/packages/core/src/utils/sdk.ts
+1 -3
@@ -46,9 +46,7 @@ export function getSDKConfig(baseUrl?: string | null, apiKey?: string | null) {
46 const apiKeyParsed = apiKey || getEnvVariable('COMPOSIO_API_KEY') || apiKeyFromUserConfig || '';
47
48 if (!apiKeyParsed) {
49 - ComposioError.handle(new ComposioNoAPIKeyError(), {
50 - exitProcess: true,
51 - });
49 + ComposioError.handleAndThrow(new ComposioNoAPIKeyError());
50 }
51
52 logger.debug('Environment', `API Key: ${apiKeyParsed}`);