diff options
Diffstat (limited to 'src/background/ollama-client.js')
| -rw-r--r-- | src/background/ollama-client.js | 26 |
1 files changed, 24 insertions, 2 deletions
diff --git a/src/background/ollama-client.js b/src/background/ollama-client.js index 0286230..e53d997 100644 --- a/src/background/ollama-client.js +++ b/src/background/ollama-client.js @@ -16,6 +16,24 @@ import { isValidOllamaHost } from '../shared/settings-schema.js'; import { ERROR_CODES } from '../shared/constants.js'; /** + * Extract a short error message from an Ollama JSON error body. + * @param {string} raw + * @returns {string} message or '' when not parseable + */ +function extractOllamaError(raw) { + if (!raw) return ''; + try { + const parsed = JSON.parse(raw); + if (parsed && typeof parsed.error === 'string') { + return parsed.error.slice(0, 200); + } + } catch { + // not JSON + } + return ''; +} + +/** * Translate a fetch/HTTP failure into a stable error code. * @param {Response|undefined} response * @returns {string} error code @@ -73,7 +91,9 @@ export async function listModels(host, signal) { throw toExtensionError(error); } if (!response.ok) { - const e = new Error(`Ollama /api/tags failed: ${response.status}`); + const detail = await response.text().catch(() => ''); + const serverMessage = extractOllamaError(detail); + const e = new Error(`Ollama /api/tags failed: ${response.status}${serverMessage ? ` - ${serverMessage}` : ''}`); e.code = response.status === 404 ? ERROR_CODES.MODEL_NOT_FOUND : ERROR_CODES.STREAM_ERROR; throw e; } @@ -119,7 +139,9 @@ export async function chatStream({ host, model, messages, think, options, onChun } if (!response.ok) { - const e = new Error(`Ollama /api/chat failed: ${response.status}`); + const detail = await response.text().catch(() => ''); + const serverMessage = extractOllamaError(detail); + const e = new Error(`Ollama /api/chat failed: ${response.status}${serverMessage ? ` - ${serverMessage}` : ''}`); e.code = response.status === 404 ? ERROR_CODES.MODEL_NOT_FOUND |
