Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
| 1 | import { ComposioRequestCancelledError, isRequestAbortError } from '../errors/SDKErrors'; |
| 2 | |
| 3 | /** @internal */ |
| 4 | export async function withCancellation<T>( |
| 5 | call: () => Promise<T>, |
| 6 | signal?: AbortSignal |
| 7 | ): Promise<T> { |
| 8 | try { |
| 9 | return await call(); |
| 10 | } catch (error) { |
| 11 | // A raw `fetch` rejects with the signal's reason, so an `AbortSignal.timeout()` |
| 12 | // surfaces as a `TimeoutError` (or a custom reason passed to `abort()`) that is |
| 13 | // not abort-shaped. Matching the reason itself still excludes an unrelated |
| 14 | // failure that merely coincides with a late abort. |
| 15 | if (signal?.aborted && (isRequestAbortError(error) || error === signal.reason)) { |
| 16 | const underlyingMessage = error instanceof Error ? error.message : ''; |
| 17 | const message = underlyingMessage |
| 18 | ? `Request was cancelled by the caller: ${underlyingMessage}` |
| 19 | : 'Request was cancelled by the caller'; |
| 20 | throw new ComposioRequestCancelledError(message, { |
| 21 | cause: error instanceof Error ? error : undefined, |
| 22 | }); |
| 23 | } |
| 24 | throw error; |
| 25 | } |
| 26 | } |