Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
Mirrored from https://github.com/Camo67/composio.git
Synced Nov 22, 14:30 UTC
fix(ts/cli): address cursorbot
jkomyno committed
Jan 15, 2026 at 01:29 UTC
964831bc9f3f7ae4dd630a9ef985e4a8a59fd812
1 file changed
+72
-47
ts/packages/cli/src/services/composio-clients.ts
+72
-47
@@ -133,6 +133,55 @@ type Metrics = {
133
readonly requests: number;
134
};
135
136
+/**
137
+ * Handles HTTP error responses by reading the body and formatting a proper error message.
138
+ * Attempts to decode the response as HttpErrorResponse for structured errors,
139
+ * otherwise falls back to a generic error with status code.
140
+ *
141
+ * @param response - The Fetch API Response object with a non-OK status
142
+ * @returns An Effect that always fails with HttpServerError containing formatted error details
143
+ */
144
+const handleHttpErrorResponse = (response: Response): Effect.Effect<never, HttpServerError> =>
145
+ Effect.gen(function* () {
146
+ const status = response.status;
147
+ const statusText = response.statusText;
148
+
149
+ // Try to read the error body as JSON
150
+ const errorBodyOpt = yield* Effect.tryPromise({
151
+ try: () => response.json() as Promise<unknown>,
152
+ catch: () => new HttpServerError({ cause: 'Failed to parse error response body' }),
153
+ }).pipe(Effect.option);
154
+
155
+ // Try to decode as structured error response
156
+ if (Option.isSome(errorBodyOpt)) {
157
+ const decodedOpt = Schema.decodeUnknownOption(HttpErrorResponse)(errorBodyOpt.value);
158
+
159
+ if (Option.isSome(decodedOpt)) {
160
+ const {
161
+ error: { error },
162
+ } = decodedOpt.value;
163
+ const pretty = renderPrettyError([
164
+ ['code', error.code],
165
+ ['message', error.message],
166
+ ['suggested fix', error.suggested_fix],
167
+ ]);
168
+
169
+ return yield* Effect.fail(
170
+ new HttpServerError({
171
+ cause: `HTTP ${status}\n${pretty}`,
172
+ })
173
+ );
174
+ }
175
+ }
176
+
177
+ // Fallback to generic error message
178
+ return yield* Effect.fail(
179
+ new HttpServerError({
180
+ cause: `HTTP ${status} ${statusText}`,
181
+ })
182
+ );
183
+ });
184
+
185
/**
186
* Streams a Fetch Response body, counting bytes precisely and parsing JSON in a single pass.
187
* Uses streaming to avoid loading the entire response into memory at once.
@@ -209,31 +258,17 @@ const callClient = <T, S extends Schema.Schema<any, any>>(
258
const client = yield* clientSingleton.get();
259
const response = yield* Effect.tryPromise({
260
try: () => apiCall(client).asResponse(),
212
- catch: e => {
213
- const decodedOpt = Schema.decodeUnknownOption(HttpErrorResponse)(e);
214
-
215
- if (Option.isNone(decodedOpt)) {
216
- return new HttpServerError({
217
- cause: e,
218
- });
219
- }
220
-
221
- const {
222
- status,
223
- error: { error },
224
- } = decodedOpt.value;
225
- const pretty = renderPrettyError([
226
- ['code', error.code],
227
- ['message', error.message],
228
- ['suggested fix', error.suggested_fix],
229
- ]);
230
-
231
- return new HttpServerError({
232
- cause: `HTTP ${status}\n${pretty}`,
233
- });
234
- },
261
+ catch: e =>
262
+ new HttpServerError({
263
+ cause: e,
264
+ }),
265
});
266
267
+ // Check HTTP status before streaming - .asResponse() doesn't throw on HTTP errors
268
+ if (!response.ok) {
269
+ return yield* handleHttpErrorResponse(response);
270
+ }
271
+
272
// Stream the response body with byte counting
273
const { json, byteSize } = yield* streamResponseWithByteCount(response);
274
const metrics = { byteSize, requests: 1 };
@@ -289,31 +324,17 @@ const callClientWithPagination = <T, S extends PaginatedSchema>(
324
Effect.gen(function* () {
325
const response = yield* Effect.tryPromise({
326
try: () => apiCall(client, cursor, MAX_PAGE_SIZE).asResponse(),
292
- catch: e => {
293
- const decodedOpt = Schema.decodeUnknownOption(HttpErrorResponse)(e);
294
-
295
- if (Option.isNone(decodedOpt)) {
296
- return new HttpServerError({
297
- cause: e,
298
- });
299
- }
300
-
301
- const {
302
- status,
303
- error: { error },
304
- } = decodedOpt.value;
305
- const pretty = renderPrettyError([
306
- ['code', error.code],
307
- ['message', error.message],
308
- ['suggested fix', error.suggested_fix],
309
- ]);
310
-
311
- return new HttpServerError({
312
- cause: `HTTP ${status}\n${pretty}`,
313
- });
314
- },
327
+ catch: e =>
328
+ new HttpServerError({
329
+ cause: e,
330
+ }),
331
});
332
333
+ // Check HTTP status before streaming - .asResponse() doesn't throw on HTTP errors
334
+ if (!response.ok) {
335
+ return yield* handleHttpErrorResponse(response);
336
+ }
337
+
338
// Stream the response body with byte counting
339
return yield* streamResponseWithByteCount(response);
340
});
@@ -494,7 +515,11 @@ export class ComposioClientLive extends Effect.Service<ComposioClientLive>()(
515
clientSingleton,
516
(client, cursor, limit) =>
517
client.tools.list({
497
- cursor, toolkit_slug: toolkitSlugs?.length ? toolkitSlugs.join(',') : undefined, toolkit_versions: 'latest', limit, }),
518
+ cursor,
519
+ toolkit_slug: toolkitSlugs?.length ? toolkitSlugs.join(',') : undefined,
520
+ toolkit_versions: 'latest',
521
+ limit,
522
+ }),
523
ToolsResponse
524
)
525
),