@devries-cameron20 / composio Mirror · read-only
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.

Mirrored from https://github.com/Camo67/composio.git

Synced Nov 21, 04:45 UTC

next
ts 241 lines 8.16 KB
Raw
1 import {
2 APIConnectionError as ClientAPIConnectionError,
3 APIConnectionTimeoutError as ClientAPIConnectionTimeoutError,
4 DecodeError as ClientDecodeError,
5 NotFoundError as ClientNotFoundError,
6 } from '@composio/client/effect';
7 import { describe, expect, it } from 'vitest';
8 import { Data } from 'effect';
9 import { extractErrorDetails } from 'src/effect-errors/logic/errors/extract-error-details';
10 import { HttpDecodingError } from 'src/services/composio-errors';
11
12 class RequestError extends Data.TaggedError('RequestError')<{
13 readonly message: string;
14 readonly cause: unknown;
15 }> {}
16
17 describe('extractErrorDetails', () => {
18 it('preserves a tagged error message and appends the cause detail', () => {
19 const error = new RequestError({
20 message: 'The request failed for /api/signup.',
21 cause: new TypeError('fetch failed'),
22 });
23
24 expect(extractErrorDetails(error)).toStrictEqual({
25 isPlainString: false,
26 type: 'RequestError',
27 message: 'The request failed for /api/signup.\nCaused by: fetch failed',
28 });
29 });
30
31 it('surfaces the deepest cause message in a nested chain', () => {
32 const deepest = new Error('expected NonEmptyString, got null');
33 const middle = new Error('Failed to parse GitHub release JSON response', { cause: deepest });
34 const error = new RequestError({
35 message: 'Upgrade failed.',
36 cause: middle,
37 });
38
39 expect(extractErrorDetails(error)).toStrictEqual({
40 isPlainString: false,
41 type: 'RequestError',
42 message: 'Upgrade failed.\nCaused by: expected NonEmptyString, got null',
43 });
44 });
45
46 it('leaves the message untouched when the cause carries no detail', () => {
47 const error = new RequestError({
48 message: 'The request failed.',
49 cause: undefined,
50 });
51
52 expect(extractErrorDetails(error)).toStrictEqual({
53 isPlainString: false,
54 type: 'RequestError',
55 message: 'The request failed.',
56 });
57 });
58
59 it('does not repeat a cause message identical to the wrapper message', () => {
60 const error = new RequestError({
61 message: 'boom',
62 cause: new Error('boom'),
63 });
64
65 expect(extractErrorDetails(error)).toStrictEqual({
66 isPlainString: false,
67 type: 'RequestError',
68 message: 'boom',
69 });
70 });
71 });
72
73 const notFoundEnvelope = {
74 error: {
75 message: 'Toolkit not found',
76 code: 1803,
77 slug: 'Toolkit_NotFound',
78 status: 404,
79 request_id: 'req_body',
80 suggested_fix: 'Check the toolkit slug',
81 },
82 };
83
84 const notFoundDetail =
85 '404 {"error":{"message":"Toolkit not found","code":1803,"slug":"Toolkit_NotFound","status":404,"request_id":"req_body","suggested_fix":"Check the toolkit slug"}}';
86
87 describe('extractErrorDetails for an uncaught API failure', () => {
88 describe('raised as a client error', () => {
89 const url = 'https://backend.composio.dev/api/v3.1/toolkits/{slug}';
90
91 it('shows the same cause detail for a 404, under the client tag', () => {
92 const error = new ClientNotFoundError({
93 message: notFoundDetail,
94 method: 'GET',
95 url,
96 status: 404,
97 requestId: 'req_header',
98 details: notFoundEnvelope.error,
99 body: notFoundEnvelope,
100 });
101
102 expect(extractErrorDetails(error)).toStrictEqual({
103 isPlainString: false,
104 type: '@composio/client/NotFoundError',
105 message: `\nCaused by: ${notFoundDetail}`,
106 });
107 });
108
109 it('shows the transport reason of a connection failure', () => {
110 const error = new ClientAPIConnectionError({
111 message: 'Connection error.',
112 method: 'GET',
113 url,
114 reason: 'TransportError',
115 description: 'fetch failed: connect ECONNREFUSED 127.0.0.1:1',
116 });
117
118 expect(extractErrorDetails(error).message).toBe(
119 '\nCaused by: fetch failed: connect ECONNREFUSED 127.0.0.1:1'
120 );
121 });
122
123 it('shows the message of a connection failure the transport did not describe', () => {
124 const error = new ClientAPIConnectionError({
125 message: 'Too many redirects.',
126 method: 'GET',
127 url,
128 reason: 'TooManyRedirects',
129 description: undefined,
130 });
131
132 expect(extractErrorDetails(error).message).toBe('\nCaused by: Too many redirects.');
133 });
134
135 it('shows a timeout', () => {
136 const error = new ClientAPIConnectionTimeoutError({
137 message: 'Request timed out.',
138 method: 'GET',
139 url,
140 });
141
142 expect(extractErrorDetails(error).message).toBe('\nCaused by: Request timed out.');
143 });
144
145 it('names the operation and request id of a decode failure and suggests an upgrade', () => {
146 const error = new ClientDecodeError({
147 message: "The response body does not match the schema of 'toolkits.retrieve'.",
148 method: 'GET',
149 url,
150 status: 200,
151 requestId: 'req_decode',
152 operation: 'toolkits.retrieve',
153 issues: [{ path: ['meta', 'categories'], kind: 'InvalidType' }],
154 });
155
156 expect(extractErrorDetails(error)).toStrictEqual({
157 isPlainString: false,
158 type: '@composio/client/DecodeError',
159 message:
160 "The API response for 'toolkits.retrieve' is not in the shape this version of the CLI expects (request id: req_decode).\nUpgrade the CLI, then retry:\n> composio upgrade",
161 });
162 });
163 });
164
165 describe('raised as a command error wrapping a client error', () => {
166 const url = 'https://backend.composio.dev/api/v3.1/internal/action_execution/logs';
167
168 it('shows the transport reason of a connection failure', () => {
169 const error = new RequestError({
170 message: 'Failed to fetch tool logs.',
171 cause: new ClientAPIConnectionError({
172 message: 'Connection error.',
173 method: 'POST',
174 url,
175 reason: 'TransportError',
176 description: 'fetch failed: connect ECONNREFUSED 127.0.0.1:1',
177 }),
178 });
179
180 expect(extractErrorDetails(error).message).toBe(
181 'Failed to fetch tool logs.\nCaused by: fetch failed: connect ECONNREFUSED 127.0.0.1:1'
182 );
183 });
184
185 it('names the operation and request id of a decode failure and suggests an upgrade', () => {
186 const error = new RequestError({
187 message: 'Failed to fetch tool logs.',
188 cause: new ClientDecodeError({
189 message: "The response body does not match the schema of 'logs.tools.list'.",
190 method: 'POST',
191 url,
192 status: 200,
193 requestId: 'req_logs',
194 operation: 'logs.tools.list',
195 issues: [{ path: ['data', 0, 'status'], kind: 'InvalidValue' }],
196 }),
197 });
198
199 expect(extractErrorDetails(error).message).toBe(
200 "Failed to fetch tool logs.\nCaused by: The API response for 'logs.tools.list' is not in the shape this version of the CLI expects (request id: req_logs).\nUpgrade the CLI, then retry:\n> composio upgrade"
201 );
202 });
203 });
204
205 describe("raised for a response that failed the CLI's schema", () => {
206 // The schema issues may quote the response, so they are never shown.
207 const shapeError = new HttpDecodingError({
208 cause: new Error('Expected array, got {"secret":"value"}'),
209 operation: 'toolRouter.session.search',
210 });
211 const described =
212 "The API response for 'toolRouter.session.search' is not in the shape this version of the CLI expects.\nUpgrade the CLI, then retry:\n> composio upgrade";
213
214 it('names the operation and suggests an upgrade', () => {
215 expect(extractErrorDetails(shapeError)).toStrictEqual({
216 isPlainString: false,
217 type: 'services/HttpDecodingError',
218 message: described,
219 });
220 });
221
222 it('does so as the cause of a command error', () => {
223 const error = new RequestError({ message: 'Failed to search tools.', cause: shapeError });
224
225 expect(extractErrorDetails(error).message).toBe(
226 `Failed to search tools.\nCaused by: ${described}`
227 );
228 });
229
230 it('keeps the schema issues of a failure that names no operation', () => {
231 const error = new RequestError({
232 message: 'Failed to fetch toolkits.',
233 cause: new HttpDecodingError({ cause: new Error('Expected array') }),
234 });
235
236 expect(extractErrorDetails(error).message).toBe(
237 'Failed to fetch toolkits.\nCaused by: Expected array'
238 );
239 });
240 });
241 });