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 { |
| 2 | AuthenticationError as ClientAuthenticationError, |
| 3 | APIConnectionError as ClientAPIConnectionError, |
| 4 | APIConnectionTimeoutError as ClientAPIConnectionTimeoutError, |
| 5 | BadRequestError as ClientBadRequestError, |
| 6 | ConfigurationError as ClientConfigurationError, |
| 7 | DecodeError as ClientDecodeError, |
| 8 | InternalServerError as ClientInternalServerError, |
| 9 | RateLimitError as ClientRateLimitError, |
| 10 | type ComposioError, |
| 11 | } from '@composio/client/effect'; |
| 12 | import { describe, expect, it } from '@effect/vitest'; |
| 13 | import { ConfigProvider, Effect } from 'effect'; |
| 14 | import { handleClientApiError } from 'src/effects/handle-http-error'; |
| 15 | import { ReportedFailure } from 'src/effects/report-failure'; |
| 16 | import { retryHint, TEMPORARY_FAILURE_EXIT_CODE } from 'src/effects/retry-advice'; |
| 17 | import { CLIENT_API_ERROR_TAGS } from 'src/services/composio-errors'; |
| 18 | import { TerminalUI } from 'src/services/terminal-ui'; |
| 19 | import { exitCodeOf } from 'test/__utils__/exit-code'; |
| 20 | import { terminalUITestImpl } from 'test/__utils__/services/terminal-ui-test'; |
| 21 | |
| 22 | const envelope = { |
| 23 | error: { |
| 24 | message: 'Auth config "ac_missing" was not found', |
| 25 | code: 1803, |
| 26 | slug: 'AuthConfig_NotFound', |
| 27 | status: 400, |
| 28 | request_id: 'req_body', |
| 29 | suggested_fix: 'Check the auth config id', |
| 30 | }, |
| 31 | }; |
| 32 | |
| 33 | const url = 'https://backend.composio.dev/api/v3.1/auth_configs/{nanoid}'; |
| 34 | |
| 35 | const clientStatusError = new ClientBadRequestError({ |
| 36 | message: `400 ${JSON.stringify(envelope)}`, |
| 37 | method: 'GET', |
| 38 | url, |
| 39 | status: 400, |
| 40 | requestId: 'req_header', |
| 41 | details: envelope.error, |
| 42 | body: envelope, |
| 43 | }); |
| 44 | |
| 45 | const clientDecodeError = new ClientDecodeError({ |
| 46 | message: "The response body does not match the schema of 'authConfigs.retrieve'.", |
| 47 | method: 'GET', |
| 48 | url, |
| 49 | status: 200, |
| 50 | requestId: 'req_decode', |
| 51 | operation: 'authConfigs.retrieve', |
| 52 | issues: [{ path: ['toolkit', 'slug'], kind: 'InvalidType' }], |
| 53 | }); |
| 54 | |
| 55 | const options = { |
| 56 | fallbackMessage: 'Failed to fetch auth config "ac_missing".', |
| 57 | hint: 'Browse available auth configs:\n> composio dev auth-configs list', |
| 58 | }; |
| 59 | |
| 60 | // A TerminalUI whose stderr is not a TTY, recording every line the handler |
| 61 | // writes together with the channel it went to. |
| 62 | const recordingUI = () => { |
| 63 | const lines: Array<string> = []; |
| 64 | const record = (channel: string) => (message: string) => |
| 65 | Effect.sync(() => { |
| 66 | lines.push(`${channel}: ${message}`); |
| 67 | }); |
| 68 | const ui = TerminalUI.of({ |
| 69 | ...terminalUITestImpl, |
| 70 | error: record('stderr'), |
| 71 | log: { ...terminalUITestImpl.log, error: record('log.error'), step: record('log.step') }, |
| 72 | }); |
| 73 | return { ui, lines }; |
| 74 | }; |
| 75 | |
| 76 | describe('handleClientApiError', () => { |
| 77 | it.effect('prints the envelope message and the suggested fix, and fails the process', () => |
| 78 | Effect.gen(function* () { |
| 79 | const { ui, lines } = recordingUI(); |
| 80 | |
| 81 | const exit = yield* Effect.exit(handleClientApiError(ui, options)(clientStatusError)); |
| 82 | |
| 83 | expect(lines).toStrictEqual([ |
| 84 | 'stderr: Auth config "ac_missing" was not found', |
| 85 | 'stderr: Check the auth config id\n\nBrowse available auth configs:\n> composio dev auth-configs list', |
| 86 | ]); |
| 87 | expect(exitCodeOf(exit)).toBe(1); |
| 88 | }) |
| 89 | ); |
| 90 | |
| 91 | it.effect.each([ |
| 92 | new ClientAPIConnectionError({ |
| 93 | message: 'Connection error.', |
| 94 | method: 'POST', |
| 95 | url, |
| 96 | reason: 'TransportError', |
| 97 | description: 'fetch failed', |
| 98 | }), |
| 99 | new ClientAPIConnectionTimeoutError({ message: 'Request timed out.', method: 'POST', url }), |
| 100 | ])('prints the fallback message for $_tag of a request that may have taken effect', error => |
| 101 | Effect.gen(function* () { |
| 102 | const { ui, lines } = recordingUI(); |
| 103 | |
| 104 | const exit = yield* Effect.exit(handleClientApiError(ui, options)(error)); |
| 105 | |
| 106 | expect(lines).toStrictEqual([ |
| 107 | 'stderr: Failed to fetch auth config "ac_missing".', |
| 108 | 'stderr: Browse available auth configs:\n> composio dev auth-configs list', |
| 109 | ]); |
| 110 | expect(exitCodeOf(exit)).toBe(1); |
| 111 | }) |
| 112 | ); |
| 113 | |
| 114 | it.effect.each([ |
| 115 | new ClientAPIConnectionError({ |
| 116 | message: 'Connection error.', |
| 117 | method: 'GET', |
| 118 | url, |
| 119 | reason: 'TransportError', |
| 120 | description: 'fetch failed', |
| 121 | }), |
| 122 | new ClientAPIConnectionTimeoutError({ message: 'Request timed out.', method: 'GET', url }), |
| 123 | new ClientInternalServerError({ |
| 124 | message: '503 Service Unavailable', |
| 125 | method: 'GET', |
| 126 | url, |
| 127 | status: 503, |
| 128 | requestId: undefined, |
| 129 | details: undefined, |
| 130 | body: undefined, |
| 131 | }), |
| 132 | ])('says a $_tag of a GET is safe to run again and exits 75', error => |
| 133 | Effect.gen(function* () { |
| 134 | const { ui, lines } = recordingUI(); |
| 135 | |
| 136 | const exit = yield* Effect.exit(handleClientApiError(ui, options)(error)); |
| 137 | |
| 138 | expect(lines).toStrictEqual([ |
| 139 | 'stderr: Failed to fetch auth config "ac_missing".', |
| 140 | `stderr: ${retryHint('safe')}`, |
| 141 | ]); |
| 142 | expect(exitCodeOf(exit)).toBe(TEMPORARY_FAILURE_EXIT_CODE); |
| 143 | }) |
| 144 | ); |
| 145 | |
| 146 | it.effect('says a rate-limited POST is safe to run again, since it did not run', () => |
| 147 | Effect.gen(function* () { |
| 148 | const { ui, lines } = recordingUI(); |
| 149 | const rateLimited = new ClientRateLimitError({ |
| 150 | message: '429 Too many requests', |
| 151 | method: 'POST', |
| 152 | url, |
| 153 | status: 429, |
| 154 | requestId: undefined, |
| 155 | details: { message: 'Too many requests', code: 429, slug: 'RateLimited', status: 429 }, |
| 156 | body: undefined, |
| 157 | }); |
| 158 | |
| 159 | const exit = yield* Effect.exit(handleClientApiError(ui, options)(rateLimited)); |
| 160 | |
| 161 | expect(lines).toStrictEqual(['stderr: Too many requests', `stderr: ${retryHint('safe')}`]); |
| 162 | expect(exitCodeOf(exit)).toBe(TEMPORARY_FAILURE_EXIT_CODE); |
| 163 | }) |
| 164 | ); |
| 165 | |
| 166 | it.effect('names the operation and request id of a decode failure and suggests an upgrade', () => |
| 167 | Effect.gen(function* () { |
| 168 | const { ui, lines } = recordingUI(); |
| 169 | |
| 170 | const exit = yield* Effect.exit( |
| 171 | handleClientApiError(ui, { |
| 172 | ...options, |
| 173 | searchForSuggestions: () => |
| 174 | Effect.succeed([{ label: 'ac_similar', command: '> composio dev auth-configs info' }]), |
| 175 | })(clientDecodeError) |
| 176 | ); |
| 177 | |
| 178 | expect(lines).toStrictEqual([ |
| 179 | "stderr: The API response for 'authConfigs.retrieve' is not in the shape this version of the CLI expects (request id: req_decode).", |
| 180 | 'stderr: Upgrade the CLI, then retry:\n> composio upgrade', |
| 181 | ]); |
| 182 | expect(exitCodeOf(exit)).toBe(1); |
| 183 | }) |
| 184 | ); |
| 185 | |
| 186 | it.effect('names the key to replace for a 401 when it comes from COMPOSIO_USER_API_KEY', () => |
| 187 | Effect.gen(function* () { |
| 188 | const { ui, lines } = recordingUI(); |
| 189 | const unauthorized = new ClientAuthenticationError({ |
| 190 | message: '401 Invalid API key', |
| 191 | method: 'GET', |
| 192 | url, |
| 193 | status: 401, |
| 194 | requestId: undefined, |
| 195 | details: { message: 'Invalid API key', code: 401, slug: 'Auth_InvalidApiKey', status: 401 }, |
| 196 | body: undefined, |
| 197 | }); |
| 198 | |
| 199 | const exit = yield* Effect.exit(handleClientApiError(ui, options)(unauthorized)).pipe( |
| 200 | Effect.provideService( |
| 201 | ConfigProvider.ConfigProvider, |
| 202 | ConfigProvider.fromEnv({ env: { COMPOSIO_USER_API_KEY: 'uak_env' } }) |
| 203 | ) |
| 204 | ); |
| 205 | |
| 206 | expect(lines).toStrictEqual([ |
| 207 | 'stderr: Invalid API key', |
| 208 | 'stderr: If the key in COMPOSIO_USER_API_KEY is no longer valid, replace it, then retry.', |
| 209 | ]); |
| 210 | expect(exitCodeOf(exit)).toBe(1); |
| 211 | }) |
| 212 | ); |
| 213 | |
| 214 | it.effect('replaces the hint with suggestions when the search finds some', () => |
| 215 | Effect.gen(function* () { |
| 216 | const { ui, lines } = recordingUI(); |
| 217 | |
| 218 | yield* handleClientApiError(ui, { |
| 219 | ...options, |
| 220 | searchForSuggestions: () => |
| 221 | Effect.succeed([ |
| 222 | { label: 'ac_similar', command: '> composio dev auth-configs info "ac_similar"' }, |
| 223 | ]), |
| 224 | })(clientStatusError).pipe(Effect.flip); |
| 225 | |
| 226 | expect(lines.at(-1)).toBe( |
| 227 | 'stderr: Check the auth config id\n\nDid you mean?\n ac_similar\n\n> composio dev auth-configs info "ac_similar"' |
| 228 | ); |
| 229 | }) |
| 230 | ); |
| 231 | }); |
| 232 | |
| 233 | describe('Effect.catchTag(CLIENT_API_ERROR_TAGS, handleClientApiError(...))', () => { |
| 234 | it.effect.each([ |
| 235 | clientStatusError, |
| 236 | clientDecodeError, |
| 237 | new ClientAPIConnectionError({ |
| 238 | message: 'Connection error.', |
| 239 | method: 'POST', |
| 240 | url, |
| 241 | reason: 'TransportError', |
| 242 | description: 'fetch failed', |
| 243 | }), |
| 244 | new ClientAPIConnectionTimeoutError({ message: 'Request timed out.', method: 'POST', url }), |
| 245 | ])('reports $_tag and fails already reported', error => |
| 246 | Effect.gen(function* () { |
| 247 | const { ui, lines } = recordingUI(); |
| 248 | |
| 249 | // The tag list catches the client's whole error union. |
| 250 | const exit = yield* Effect.exit( |
| 251 | Effect.fail<ComposioError>(error).pipe( |
| 252 | Effect.catchTag(CLIENT_API_ERROR_TAGS, handleClientApiError(ui, options)) |
| 253 | ) |
| 254 | ); |
| 255 | const failure = yield* Effect.flip(exit); |
| 256 | |
| 257 | expect(failure).toBeInstanceOf(ReportedFailure); |
| 258 | expect(lines).not.toStrictEqual([]); |
| 259 | expect(exitCodeOf(exit)).toBe(1); |
| 260 | }) |
| 261 | ); |
| 262 | |
| 263 | it.effect('leaves other failures alone', () => |
| 264 | Effect.gen(function* () { |
| 265 | const { ui, lines } = recordingUI(); |
| 266 | const error = new ClientConfigurationError({ message: 'No credential.' }); |
| 267 | |
| 268 | const failure = yield* Effect.fail<ComposioError>(error).pipe( |
| 269 | Effect.catchTag(CLIENT_API_ERROR_TAGS, handleClientApiError(ui, options)), |
| 270 | Effect.flip |
| 271 | ); |
| 272 | |
| 273 | expect(failure).toBe(error); |
| 274 | expect(lines).toStrictEqual([]); |
| 275 | }) |
| 276 | ); |
| 277 | }); |