diff --git a/app/src/lib/ai/butlerClient.ts b/app/src/lib/ai/butlerClient.ts index b917fb41cf..32aee18c92 100644 --- a/app/src/lib/ai/butlerClient.ts +++ b/app/src/lib/ai/butlerClient.ts @@ -1,5 +1,5 @@ import { SHORT_DEFAULT_BRANCH_TEMPLATE, SHORT_DEFAULT_COMMIT_TEMPLATE } from '$lib/ai/prompts'; -import { err, ok, type Result } from '$lib/result'; +import { ok, stringErrorFromAny, type Result } from '$lib/result'; import type { AIClient, ModelKind, Prompt } from '$lib/ai/types'; import type { HttpClient } from '$lib/backend/httpClient'; @@ -26,11 +26,7 @@ export class ButlerAIClient implements AIClient { return ok(response.message); } catch (e) { - if (e instanceof Error) { - return err(e.message); - } else { - return err('Failed to contant GitButler API'); - } + return stringErrorFromAny(e); } } } diff --git a/app/src/lib/ai/ollamaClient.ts b/app/src/lib/ai/ollamaClient.ts index 4950d60d20..86000ceed4 100644 --- a/app/src/lib/ai/ollamaClient.ts +++ b/app/src/lib/ai/ollamaClient.ts @@ -1,6 +1,6 @@ import { LONG_DEFAULT_BRANCH_TEMPLATE, LONG_DEFAULT_COMMIT_TEMPLATE } from '$lib/ai/prompts'; import { MessageRole, type PromptMessage, type AIClient, type Prompt } from '$lib/ai/types'; -import { err, isError, ok, type Result } from '$lib/result'; +import { err, isError, ok, stringErrorFromAny, type Result } from '$lib/result'; import { isNonEmptyObject } from '$lib/utils/typeguards'; import { fetch, Body, Response } from '@tauri-apps/api/http'; @@ -89,12 +89,17 @@ export class OllamaClient implements AIClient { if (isError(responseResult)) return responseResult; const response = responseResult.value; - const rawResponse = JSON.parse(response.message.content); - if (!isOllamaChatMessageFormat(rawResponse)) { - err('Invalid response: ' + response.message.content); - } + try { + const rawResponse = JSON.parse(response.message.content); + if (!isOllamaChatMessageFormat(rawResponse)) { + return err('Invalid response: ' + response.message.content); + } - return ok(rawResponse.result); + return ok(rawResponse.result); + } catch (e) { + // Catch JSON.parse error + return stringErrorFromAny(e); + } } /** diff --git a/app/src/lib/result.ts b/app/src/lib/result.ts index 103ab07fd8..1599ccc517 100644 --- a/app/src/lib/result.ts +++ b/app/src/lib/result.ts @@ -41,3 +41,11 @@ export function ok(value: Ok): Result { export function err(value: Error): Result { return { ok: false, error: value }; } + +export function stringErrorFromAny(value: any): Result { + if (value instanceof Error) { + return err(value.message); + } else { + return err(String(value)); + } +}