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

next
ts 170 lines 5.45 KB
Raw
1 import {
2 APIConnectionError,
3 DecodeError,
4 InternalServerError,
5 RateLimitError,
6 } from '@composio/client/effect';
7 import { describe, expect, it } from 'vitest';
8 import { HttpDecodingError } from 'src/services/composio-errors';
9 import {
10 extractApiErrorDetails,
11 extractMessage,
12 findApiError,
13 } from 'src/utils/api-error-extraction';
14
15 const envelope = {
16 error: {
17 message: 'Rate limited',
18 code: 42901,
19 slug: 'rate_limited',
20 status: 429,
21 request_id: 'req_body',
22 suggested_fix: 'Slow down',
23 },
24 };
25
26 const url = 'https://backend.composio.dev/api/v3.1/toolkits';
27
28 // The client's error for a 429 answered with `envelope`.
29 const rateLimitError = (requestId: string | undefined) =>
30 new RateLimitError({
31 message: `429 ${JSON.stringify(envelope)}`,
32 method: 'GET',
33 url,
34 status: 429,
35 requestId: requestId ?? envelope.error.request_id,
36 details: envelope.error,
37 body: envelope,
38 });
39
40 const badGatewayError = (body: unknown, message: string) =>
41 new InternalServerError({
42 message,
43 method: 'GET',
44 url,
45 status: 502,
46 requestId: undefined,
47 details: undefined,
48 body,
49 });
50
51 const connectionError = (description: string) =>
52 new APIConnectionError({
53 message: 'Connection error.',
54 method: 'GET',
55 url,
56 reason: 'TransportError',
57 description,
58 });
59
60 describe('extractApiErrorDetails', () => {
61 it('reads the envelope, status, and header request id from a status error', () => {
62 const error = rateLimitError('req_header');
63
64 expect(findApiError(error)).toBe(error);
65 expect(extractApiErrorDetails(error)).toEqual({
66 message: 'Rate limited',
67 code: 42901,
68 slug: 'rate_limited',
69 status: 429,
70 request_id: 'req_header',
71 suggested_fix: 'Slow down',
72 });
73 });
74
75 it('follows the cause chain to the first status error', () => {
76 const error = rateLimitError(undefined);
77 const wrapper = Object.assign(new Error('generic wrapper'), {
78 cause: { _tag: 'UnknownException', cause: error },
79 });
80
81 expect(findApiError(wrapper)).toBe(error);
82 expect(extractApiErrorDetails(wrapper)?.slug).toBe('rate_limited');
83 });
84
85 it('falls back to the error message when the body is not the envelope', () => {
86 const error = badGatewayError({ message: 'Bad gateway' }, '502 {"message":"Bad gateway"}');
87
88 expect(extractApiErrorDetails(error)).toMatchObject({
89 message: error.message,
90 status: 502,
91 code: undefined,
92 slug: undefined,
93 });
94 });
95
96 it('falls back to the status-prefixed message when the body is not JSON', () => {
97 const error = badGatewayError(undefined, '502 status code (no body)');
98
99 expect(extractApiErrorDetails(error)).toEqual({
100 message: '502 status code (no body)',
101 code: undefined,
102 slug: undefined,
103 status: 502,
104 request_id: undefined,
105 suggested_fix: undefined,
106 });
107 });
108
109 it('reports no details for a connection error', () => {
110 expect(extractApiErrorDetails(connectionError('fetch failed'))).toBeUndefined();
111 });
112
113 it('returns undefined when no status error is in the chain', () => {
114 expect(extractApiErrorDetails({ error: envelope.error })).toBeUndefined();
115 expect(extractApiErrorDetails(new Error('plain'))).toBeUndefined();
116 expect(extractApiErrorDetails(undefined)).toBeUndefined();
117 expect(extractApiErrorDetails('plain string')).toBeUndefined();
118 });
119 });
120
121 describe('extractMessage', () => {
122 it('reads the envelope message', () => {
123 expect(extractMessage(rateLimitError(undefined))).toBe('Rate limited');
124 });
125
126 it('reads the message of a body that is not the envelope', () => {
127 const error = badGatewayError({ message: 'Bad gateway' }, '502 {"message":"Bad gateway"}');
128
129 expect(extractMessage(error)).toBe('Bad gateway');
130 });
131
132 it('reports a response that failed decoding by operation and request id, with the remedy', () => {
133 const clientError = new DecodeError({
134 message: "The response body does not match the schema of 'toolRouter.session.toolkits'.",
135 method: 'GET',
136 url: 'https://backend.composio.dev/api/v3.1/tool_router/session/{session_id}/toolkits',
137 status: 200,
138 requestId: 'req_toolkits',
139 operation: 'toolRouter.session.toolkits',
140 issues: [{ path: ['items', 0, 'slug'], kind: 'MissingKey' }],
141 });
142
143 expect(extractMessage({ cause: clientError })).toBe(
144 "The API response for 'toolRouter.session.toolkits' is not in the shape this version of the CLI expects (request id: req_toolkits).\nUpgrade the CLI, then retry:\n> composio upgrade"
145 );
146 });
147
148 it("reports a response that failed the CLI's schema for a named operation, with the remedy", () => {
149 const shapeError = new HttpDecodingError({
150 cause: new Error('Expected object, got undefined'),
151 operation: 'toolRouter.session.search',
152 });
153
154 expect(extractMessage({ cause: shapeError })).toBe(
155 "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"
156 );
157 });
158
159 it('reads the transport text of a connection error', () => {
160 const error = connectionError('fetch failed: connect ECONNREFUSED 127.0.0.1:1');
161
162 expect(extractMessage(error)).toBe('fetch failed: connect ECONNREFUSED 127.0.0.1:1');
163 });
164
165 it('falls back to the status-prefixed message when the body is not JSON', () => {
166 const error = badGatewayError(undefined, '502 status code (no body)');
167
168 expect(extractMessage(error)).toBe('502 status code (no body)');
169 });
170 });