|
| 1 | +import test from 'ava'; |
| 2 | +import {parseAPIError} from './ai-sdk-client.js'; |
| 3 | + |
| 4 | +// Tests for parseAPIError function |
| 5 | +// Now using the actual exported function instead of a duplicated copy |
| 6 | + |
| 7 | +test('parseAPIError - handles Ollama unmarshal error from issue #87', t => { |
| 8 | + const error = new Error( |
| 9 | + "RetryError [AI_RetryError]: Failed after 3 attempts. Last error: unmarshal: invalid character '{' after top-level value", |
| 10 | + ); |
| 11 | + |
| 12 | + const result = parseAPIError(error); |
| 13 | + |
| 14 | + t.true(result.includes('Ollama server error')); |
| 15 | + t.true(result.includes('malformed JSON')); |
| 16 | + t.true(result.includes('Restart Ollama')); |
| 17 | + t.true(result.includes('Re-pull the model')); |
| 18 | + t.true(result.includes('Check Ollama logs')); |
| 19 | + t.true(result.includes('Try a different model')); |
| 20 | + t.true(result.includes('Original error:')); |
| 21 | +}); |
| 22 | + |
| 23 | +test('parseAPIError - handles unmarshal error without retry wrapper', t => { |
| 24 | + const error = new Error("unmarshal: invalid character '{' after top-level value"); |
| 25 | + |
| 26 | + const result = parseAPIError(error); |
| 27 | + |
| 28 | + t.true(result.includes('Ollama server error')); |
| 29 | + t.true(result.includes('malformed JSON')); |
| 30 | +}); |
| 31 | + |
| 32 | +test('parseAPIError - handles 500 error with invalid character (status code takes precedence)', t => { |
| 33 | + // This test verifies that HTTP status codes are parsed FIRST, |
| 34 | + // so a 500 error with "invalid character" in the message is treated |
| 35 | + // as a server error, not an Ollama-specific error |
| 36 | + const error = new Error( |
| 37 | + "500 Internal Server Error: invalid character 'x' after top-level value", |
| 38 | + ); |
| 39 | + |
| 40 | + const result = parseAPIError(error); |
| 41 | + |
| 42 | + // Status code parsing takes precedence over Ollama-specific pattern matching |
| 43 | + t.is(result, "Server error: invalid character 'x' after top-level value"); |
| 44 | +}); |
| 45 | + |
| 46 | +test('parseAPIError - handles 500 error without JSON parsing issue', t => { |
| 47 | + const error = new Error('500 Internal Server Error: database connection failed'); |
| 48 | + |
| 49 | + const result = parseAPIError(error); |
| 50 | + |
| 51 | + t.is(result, 'Server error: database connection failed'); |
| 52 | +}); |
| 53 | + |
| 54 | +test('parseAPIError - handles 404 error', t => { |
| 55 | + const error = new Error('404 Not Found: model not available'); |
| 56 | + |
| 57 | + const result = parseAPIError(error); |
| 58 | + |
| 59 | + t.is( |
| 60 | + result, |
| 61 | + 'Model not found: The requested model may not exist or is unavailable', |
| 62 | + ); |
| 63 | +}); |
| 64 | + |
| 65 | +test('parseAPIError - handles connection refused', t => { |
| 66 | + const error = new Error('ECONNREFUSED: Connection refused'); |
| 67 | + |
| 68 | + const result = parseAPIError(error); |
| 69 | + |
| 70 | + t.is(result, 'Connection failed: Unable to reach the model server'); |
| 71 | +}); |
| 72 | + |
| 73 | +test('parseAPIError - handles timeout error', t => { |
| 74 | + const error = new Error('Request timeout: ETIMEDOUT'); |
| 75 | + |
| 76 | + const result = parseAPIError(error); |
| 77 | + |
| 78 | + t.is(result, 'Request timed out: The model took too long to respond'); |
| 79 | +}); |
| 80 | + |
| 81 | +test('parseAPIError - handles non-Error objects', t => { |
| 82 | + const result = parseAPIError('string error'); |
| 83 | + |
| 84 | + t.is(result, 'An unknown error occurred while communicating with the model'); |
| 85 | +}); |
| 86 | + |
| 87 | +test('parseAPIError - handles context length errors', t => { |
| 88 | + const error = new Error( |
| 89 | + 'context length exceeded', |
| 90 | + ); |
| 91 | + |
| 92 | + const result = parseAPIError(error); |
| 93 | + |
| 94 | + // Use exact assertion instead of OR condition |
| 95 | + t.is(result, 'Context too large: Please reduce the conversation length or message size'); |
| 96 | +}); |
| 97 | + |
| 98 | +test('parseAPIError - handles too many tokens errors', t => { |
| 99 | + const error = new Error( |
| 100 | + 'too many tokens in the request', |
| 101 | + ); |
| 102 | + |
| 103 | + const result = parseAPIError(error); |
| 104 | + |
| 105 | + t.is(result, 'Context too large: Please reduce the conversation length or message size'); |
| 106 | +}); |
| 107 | + |
| 108 | +test('parseAPIError - handles 400 with context length in message', t => { |
| 109 | + const error = new Error( |
| 110 | + '400 Bad Request: context length exceeded', |
| 111 | + ); |
| 112 | + |
| 113 | + const result = parseAPIError(error); |
| 114 | + |
| 115 | + // The 400 status code pattern matches first, so we get the full message |
| 116 | + t.is(result, 'Bad request: context length exceeded'); |
| 117 | +}); |
| 118 | + |
| 119 | +test('parseAPIError - handles 401 authentication error', t => { |
| 120 | + const error = new Error('401 Unauthorized: Invalid API key'); |
| 121 | + |
| 122 | + const result = parseAPIError(error); |
| 123 | + |
| 124 | + t.is(result, 'Authentication failed: Invalid API key or credentials'); |
| 125 | +}); |
| 126 | + |
| 127 | +test('parseAPIError - handles 403 forbidden error', t => { |
| 128 | + const error = new Error('403 Forbidden: Access denied'); |
| 129 | + |
| 130 | + const result = parseAPIError(error); |
| 131 | + |
| 132 | + t.is(result, 'Access forbidden: Check your API permissions'); |
| 133 | +}); |
| 134 | + |
| 135 | +test('parseAPIError - handles 429 rate limit error', t => { |
| 136 | + const error = new Error('429 Too Many Requests: Rate limit exceeded'); |
| 137 | + |
| 138 | + const result = parseAPIError(error); |
| 139 | + |
| 140 | + t.is(result, 'Rate limit exceeded: Too many requests. Please wait and try again'); |
| 141 | +}); |
| 142 | + |
| 143 | +test('parseAPIError - handles 502 bad gateway error', t => { |
| 144 | + const error = new Error('502 Bad Gateway: upstream error'); |
| 145 | + |
| 146 | + const result = parseAPIError(error); |
| 147 | + |
| 148 | + t.is(result, 'Server error: upstream error'); |
| 149 | +}); |
| 150 | + |
| 151 | +test('parseAPIError - handles 503 service unavailable error', t => { |
| 152 | + const error = new Error('503 Service Unavailable: server overloaded'); |
| 153 | + |
| 154 | + const result = parseAPIError(error); |
| 155 | + |
| 156 | + t.is(result, 'Server error: server overloaded'); |
| 157 | +}); |
| 158 | + |
| 159 | +test('parseAPIError - handles reduce tokens message', t => { |
| 160 | + const error = new Error('Please reduce the number of tokens in your request'); |
| 161 | + |
| 162 | + const result = parseAPIError(error); |
| 163 | + |
| 164 | + t.is(result, 'Too many tokens: Please shorten your message or clear conversation history'); |
| 165 | +}); |
| 166 | + |
| 167 | +test('parseAPIError - cleans up unknown errors', t => { |
| 168 | + const error = new Error('Error: Something unexpected happened\nWith more details'); |
| 169 | + |
| 170 | + const result = parseAPIError(error); |
| 171 | + |
| 172 | + // Should strip "Error: " prefix and only return first line |
| 173 | + t.is(result, 'Something unexpected happened'); |
| 174 | +}); |
0 commit comments