diff options
| author | Scott Densmore <[email protected]> | 2025-06-07 22:04:57 -0700 |
|---|---|---|
| committer | GitHub <[email protected]> | 2025-06-07 22:04:57 -0700 |
| commit | b46f22093145760eaf8cefafd0050597bfb44249 (patch) | |
| tree | eeb3eb26e2289c1d5feef272745c105a2dbfca93 /packages/cli/src/ui/utils/errorParsing.test.ts | |
| parent | 6e4b84a60d0f94ba772b9872385d1c4021090786 (diff) | |
feat(cli): improve API error parsing and display (#829)
Diffstat (limited to 'packages/cli/src/ui/utils/errorParsing.test.ts')
| -rw-r--r-- | packages/cli/src/ui/utils/errorParsing.test.ts | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/packages/cli/src/ui/utils/errorParsing.test.ts b/packages/cli/src/ui/utils/errorParsing.test.ts new file mode 100644 index 00000000..afee5793 --- /dev/null +++ b/packages/cli/src/ui/utils/errorParsing.test.ts @@ -0,0 +1,56 @@ +/** + * @license + * Copyright 2025 Google LLC + * SPDX-License-Identifier: Apache-2.0 + */ + +import { describe, it, expect } from 'vitest'; +import { parseAndFormatApiError } from './errorParsing.js'; + +describe('parseAndFormatApiError', () => { + it('should format a valid API error JSON', () => { + const errorMessage = + 'got status: 400 Bad Request. {"error":{"code":400,"message":"API key not valid. Please pass a valid API key.","status":"INVALID_ARGUMENT"}}'; + const expected = + 'API Error: API key not valid. Please pass a valid API key. (Status: INVALID_ARGUMENT)'; + expect(parseAndFormatApiError(errorMessage)).toBe(expected); + }); + + it('should return the original message if it is not a JSON error', () => { + const errorMessage = 'This is a plain old error message'; + expect(parseAndFormatApiError(errorMessage)).toBe(errorMessage); + }); + + it('should return the original message for malformed JSON', () => { + const errorMessage = '[Stream Error: {"error": "malformed}'; + expect(parseAndFormatApiError(errorMessage)).toBe(errorMessage); + }); + + it('should handle JSON that does not match the ApiError structure', () => { + const errorMessage = '[Stream Error: {"not_an_error": "some other json"}]'; + expect(parseAndFormatApiError(errorMessage)).toBe(errorMessage); + }); + + it('should format a nested API error', () => { + const nestedErrorMessage = JSON.stringify({ + error: { + code: 429, + message: + "Gemini 2.5 Pro Preview doesn't have a free quota tier. For more information on this error, head to: https://ai.google.dev/gemini-api/docs/rate-limits.", + status: 'RESOURCE_EXHAUSTED', + }, + }); + + const errorMessage = JSON.stringify({ + error: { + code: 429, + message: nestedErrorMessage, + status: 'Too Many Requests', + }, + }); + + expect(parseAndFormatApiError(errorMessage)).toBe( + "API Error: Gemini 2.5 Pro Preview doesn't have a free quota tier. For more information on this error, head to: https://ai.google.dev/gemini-api/docs/rate-limits. (Status: Too Many Requests)", + ); + }); +}); |
