xonecas.io
64875
Mc

mini-coder / commit fc9653d

refactor: share error message helper

xonecas2026-04-10 fc9653df1396173955c033bf05e1f7318754f696 parent 5424197

7 files changed

src/errors.test.ts+10−0
@@ -0,0 +1,10 @@
1+import { describe, expect, test } from "bun:test";
2+import { getErrorMessage } from "./errors.ts";
3+
4+describe("errors", () => {
5+ test("getErrorMessage returns an Error message and stringifies non-Errors", () => {
6+ expect(getErrorMessage(new Error("boom"))).toBe("boom");
7+ expect(getErrorMessage("plain failure")).toBe("plain failure");
8+ expect(getErrorMessage(42)).toBe("42");
9+ });
10+});
src/errors.ts+15−0
@@ -0,0 +1,15 @@
1+/**
2+ * Shared error-formatting helpers.
3+ *
4+ * @module
5+ */
6+
7+/**
8+ * Convert an unknown thrown value into a readable message string.
9+ *
10+ * @param error - The thrown value to format.
11+ * @returns The error message when available, otherwise the stringified value.
12+ */
13+export function getErrorMessage(error: unknown): string {
14+ return error instanceof Error ? error.message : String(error);
15+}
src/index.ts+1−4
@@ -28,6 +28,7 @@ import {
2828 resolveHeadlessPrompt,
2929 shouldUseHeadlessMode,
3030 } from "./cli.ts";
31+import { getErrorMessage } from "./errors.ts";
3132 import { type GitState, getGitState } from "./git.ts";
3233 import { canonicalizePath } from "./paths.ts";
3334 import {
@@ -98,10 +99,6 @@ export const MAX_SESSIONS_PER_CWD = 20;
9899 /** Maximum raw prompt-history entries to retain globally. */
99100 export const MAX_PROMPT_HISTORY = 1_000;
100101
101-function getErrorMessage(error: unknown): string {
102- return error instanceof Error ? error.message : String(error);
103-}
104-
105102 // ---------------------------------------------------------------------------
106103 // OAuth credential persistence
107104 // ---------------------------------------------------------------------------
src/settings.ts+1−4
@@ -10,6 +10,7 @@
1010 import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
1111 import { dirname } from "node:path";
1212 import type { ThinkingLevel } from "@mariozechner/pi-ai";
13+import { getErrorMessage } from "./errors.ts";
1314
1415 /** Default reasoning effort when no saved setting exists. */
1516 const DEFAULT_EFFORT: ThinkingLevel = "medium";
@@ -51,10 +52,6 @@ const THINKING_LEVELS = new Set<ThinkingLevel>([
5152 "xhigh",
5253 ]);
5354
54-function getErrorMessage(error: unknown): string {
55- return error instanceof Error ? error.message : String(error);
56-}
57-
5855 /**
5956 * Load and validate user settings from disk.
6057 *
src/submit.ts+1−4
@@ -8,6 +8,7 @@ import { readFileSync } from "node:fs";
88 import type { UserMessage } from "@mariozechner/pi-ai";
99 import type { AgentEvent } from "./agent.ts";
1010 import { runAgentLoop } from "./agent.ts";
11+import { getErrorMessage } from "./errors.ts";
1112 import { getGitState } from "./git.ts";
1213 import {
1314 type AppState,
@@ -103,10 +104,6 @@ export function isEmptyUserContent(content: UserMessage["content"]): boolean {
103104 );
104105 }
105106
106-function getErrorMessage(error: unknown): string {
107- return error instanceof Error ? error.message : String(error);
108-}
109-
110107 function buildSkillMessageContent(
111108 skillName: string,
112109 userText: string,
src/ui/agent.ts+1−4
@@ -11,6 +11,7 @@
1111
1212 import type { AssistantMessage } from "@mariozechner/pi-ai";
1313 import type { AgentEvent } from "../agent.ts";
14+import { getErrorMessage } from "../errors.ts";
1415 import type { AppState } from "../index.ts";
1516 import { resolveRawInput, submitResolvedInput } from "../submit.ts";
1617 import type {
@@ -75,10 +76,6 @@ export function getStreamingConversationState(): StreamingConversationState {
7576 };
7677 }
7778
78-function getErrorMessage(error: unknown): string {
79- return error instanceof Error ? error.message : String(error);
80-}
81-
8279 /**
8380 * Create the UI agent controller bound to runtime hooks supplied by `ui.ts`.
8481 *
src/ui/commands.ts+1−4
@@ -14,6 +14,7 @@ import { Select } from "@cel-tui/components";
1414 import type { Model, ThinkingLevel } from "@mariozechner/pi-ai";
1515 import type { OAuthProviderInterface } from "@mariozechner/pi-ai/oauth";
1616 import { getOAuthProviders } from "@mariozechner/pi-ai/oauth";
17+import { getErrorMessage } from "../errors.ts";
1718 import type { AppState } from "../index.ts";
1819 import { getAvailableModels, saveOAuthCredentials } from "../index.ts";
1920 import { COMMANDS } from "../input.ts";
@@ -39,10 +40,6 @@ const EFFORT_LEVELS: { label: string; value: ThinkingLevel }[] = [
3940 { label: "xhigh", value: "xhigh" },
4041 ];
4142
42-function getErrorMessage(error: unknown): string {
43- return error instanceof Error ? error.message : String(error);
44-}
45-
4643 /** Runtime hooks injected from the stateful UI module. */
4744 interface UiCommandRuntime {
4845 /** Open an overlay and trigger a re-render. */