|
| 1 | +import test from 'ava'; |
| 2 | + |
| 3 | +// Note: parseAPIError is an internal function in ai-sdk-client.ts that is not exported. |
| 4 | +// For testing purposes, we replicate the logic here to verify it works correctly. |
| 5 | + |
| 6 | +/** |
| 7 | + * Parses API errors into user-friendly messages |
| 8 | + * This is a copy of the internal parseAPIError function for testing |
| 9 | + */ |
| 10 | +function parseAPIErrorForTest(error: unknown): string { |
| 11 | + if (!(error instanceof Error)) { |
| 12 | + return 'An unknown error occurred while communicating with the model'; |
| 13 | + } |
| 14 | + |
| 15 | + const errorMessage = error.message; |
| 16 | + |
| 17 | + // Handle Ollama-specific unmarshal/JSON parsing errors |
| 18 | + if ( |
| 19 | + errorMessage.includes('unmarshal') || |
| 20 | + (errorMessage.includes('invalid character') && |
| 21 | + errorMessage.includes('after top-level value')) |
| 22 | + ) { |
| 23 | + return ( |
| 24 | + 'Ollama server error: The model returned malformed JSON. ' + |
| 25 | + 'This usually indicates an issue with the Ollama server or model. ' + |
| 26 | + 'Try:\n' + |
| 27 | + ' 1. Restart Ollama: systemctl restart ollama (Linux) or restart the Ollama app\n' + |
| 28 | + ' 2. Re-pull the model: ollama pull <model-name>\n' + |
| 29 | + ' 3. Check Ollama logs for more details\n' + |
| 30 | + ' 4. Try a different model to see if the issue is model-specific\n' + |
| 31 | + `Original error: ${errorMessage}` |
| 32 | + ); |
| 33 | + } |
| 34 | + |
| 35 | + // Extract status code and clean message from common error patterns |
| 36 | + const statusMatch = errorMessage.match( |
| 37 | + /(?:Error: )?(\d{3})\s+(?:\d{3}\s+)?(?:Bad Request|[^:]+):\s*(.+)/i, |
| 38 | + ); |
| 39 | + if (statusMatch) { |
| 40 | + const [, statusCode, message] = statusMatch; |
| 41 | + const cleanMessage = message.trim(); |
| 42 | + |
| 43 | + switch (statusCode) { |
| 44 | + case '400': |
| 45 | + return `Bad request: ${cleanMessage}`; |
| 46 | + case '401': |
| 47 | + return 'Authentication failed: Invalid API key or credentials'; |
| 48 | + case '403': |
| 49 | + return 'Access forbidden: Check your API permissions'; |
| 50 | + case '404': |
| 51 | + return 'Model not found: The requested model may not exist or is unavailable'; |
| 52 | + case '429': |
| 53 | + return 'Rate limit exceeded: Too many requests. Please wait and try again'; |
| 54 | + case '500': |
| 55 | + case '502': |
| 56 | + case '503': |
| 57 | + return `Server error: ${cleanMessage}`; |
| 58 | + default: |
| 59 | + return `Request failed (${statusCode}): ${cleanMessage}`; |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + // Handle timeout errors |
| 64 | + if (errorMessage.includes('timeout') || errorMessage.includes('ETIMEDOUT')) { |
| 65 | + return 'Request timed out: The model took too long to respond'; |
| 66 | + } |
| 67 | + |
| 68 | + // Handle network errors |
| 69 | + if ( |
| 70 | + errorMessage.includes('ECONNREFUSED') || |
| 71 | + errorMessage.includes('connect') |
| 72 | + ) { |
| 73 | + return 'Connection failed: Unable to reach the model server'; |
| 74 | + } |
| 75 | + |
| 76 | + // Handle context length errors |
| 77 | + if ( |
| 78 | + errorMessage.includes('context length') || |
| 79 | + errorMessage.includes('too many tokens') |
| 80 | + ) { |
| 81 | + return 'Context too large: Please reduce the conversation length or message size'; |
| 82 | + } |
| 83 | + |
| 84 | + // Handle token limit errors |
| 85 | + if (errorMessage.includes('reduce the number of tokens')) { |
| 86 | + return 'Too many tokens: Please shorten your message or clear conversation history'; |
| 87 | + } |
| 88 | + |
| 89 | + // If we can't parse it, return a cleaned up version |
| 90 | + return errorMessage.replace(/^Error:\s*/i, '').split('\n')[0]; |
| 91 | +} |
| 92 | + |
| 93 | +test('parseAPIError - handles Ollama unmarshal error from issue #87', t => { |
| 94 | + const error = new Error( |
| 95 | + "RetryError [AI_RetryError]: Failed after 3 attempts. Last error: unmarshal: invalid character '{' after top-level value", |
| 96 | + ); |
| 97 | + |
| 98 | + const result = parseAPIErrorForTest(error); |
| 99 | + |
| 100 | + t.true(result.includes('Ollama server error')); |
| 101 | + t.true(result.includes('malformed JSON')); |
| 102 | + t.true(result.includes('Restart Ollama')); |
| 103 | + t.true(result.includes('Re-pull the model')); |
| 104 | + t.true(result.includes('Check Ollama logs')); |
| 105 | + t.true(result.includes('Try a different model')); |
| 106 | + t.true(result.includes('Original error:')); |
| 107 | +}); |
| 108 | + |
| 109 | +test('parseAPIError - handles unmarshal error without retry wrapper', t => { |
| 110 | + const error = new Error("unmarshal: invalid character '{' after top-level value"); |
| 111 | + |
| 112 | + const result = parseAPIErrorForTest(error); |
| 113 | + |
| 114 | + t.true(result.includes('Ollama server error')); |
| 115 | + t.true(result.includes('malformed JSON')); |
| 116 | +}); |
| 117 | + |
| 118 | +test('parseAPIError - handles invalid character error', t => { |
| 119 | + const error = new Error( |
| 120 | + "500 Internal Server Error: invalid character 'x' after top-level value", |
| 121 | + ); |
| 122 | + |
| 123 | + const result = parseAPIErrorForTest(error); |
| 124 | + |
| 125 | + t.true(result.includes('Ollama server error')); |
| 126 | + t.true(result.includes('malformed JSON')); |
| 127 | +}); |
| 128 | + |
| 129 | +test('parseAPIError - handles 500 error without JSON parsing issue', t => { |
| 130 | + const error = new Error('500 Internal Server Error: database connection failed'); |
| 131 | + |
| 132 | + const result = parseAPIErrorForTest(error); |
| 133 | + |
| 134 | + t.is(result, 'Server error: database connection failed'); |
| 135 | +}); |
| 136 | + |
| 137 | +test('parseAPIError - handles 404 error', t => { |
| 138 | + const error = new Error('404 Not Found: model not available'); |
| 139 | + |
| 140 | + const result = parseAPIErrorForTest(error); |
| 141 | + |
| 142 | + t.is( |
| 143 | + result, |
| 144 | + 'Model not found: The requested model may not exist or is unavailable', |
| 145 | + ); |
| 146 | +}); |
| 147 | + |
| 148 | +test('parseAPIError - handles connection refused', t => { |
| 149 | + const error = new Error('ECONNREFUSED: Connection refused'); |
| 150 | + |
| 151 | + const result = parseAPIErrorForTest(error); |
| 152 | + |
| 153 | + t.is(result, 'Connection failed: Unable to reach the model server'); |
| 154 | +}); |
| 155 | + |
| 156 | +test('parseAPIError - handles timeout error', t => { |
| 157 | + const error = new Error('Request timeout: ETIMEDOUT'); |
| 158 | + |
| 159 | + const result = parseAPIErrorForTest(error); |
| 160 | + |
| 161 | + t.is(result, 'Request timed out: The model took too long to respond'); |
| 162 | +}); |
| 163 | + |
| 164 | +test('parseAPIError - handles non-Error objects', t => { |
| 165 | + const result = parseAPIErrorForTest('string error'); |
| 166 | + |
| 167 | + t.is(result, 'An unknown error occurred while communicating with the model'); |
| 168 | +}); |
| 169 | + |
| 170 | +test('parseAPIError - handles context length errors', t => { |
| 171 | + const error = new Error( |
| 172 | + 'context length exceeded, please reduce the number of tokens', |
| 173 | + ); |
| 174 | + |
| 175 | + const result = parseAPIErrorForTest(error); |
| 176 | + |
| 177 | + t.true( |
| 178 | + result.includes('Context too large') || |
| 179 | + result.includes('Too many tokens'), |
| 180 | + ); |
| 181 | +}); |
| 182 | + |
| 183 | +test('parseAPIError - handles 400 with context length in message', t => { |
| 184 | + const error = new Error( |
| 185 | + '400 Bad Request: context length exceeded', |
| 186 | + ); |
| 187 | + |
| 188 | + const result = parseAPIErrorForTest(error); |
| 189 | + |
| 190 | + // The 400 status code pattern matches first, so we get the full message |
| 191 | + t.is(result, 'Bad request: context length exceeded'); |
| 192 | +}); |
0 commit comments