@@ -0,0 +1,120 @@
1+import { expect, test } from "bun:test";
2+import {
3+ APICallError,
4+ LoadAPIKeyError,
5+ NoContentGeneratedError,
6+ NoSuchModelError,
7+ RetryError,
8+} from "ai";
9+import { parseAppError } from "./error-parse.ts";
10+
11+test("parseAppError - string", () => {
12+ expect(parseAppError("hello world")).toEqual({ headline: "hello world" });
13+});
14+
15+test("parseAppError - APICallError 429", () => {
16+ const err = new APICallError({
17+ message: "Too many",
18+ statusCode: 429,
19+ url: "https://api.example.com",
20+ requestBodyValues: {},
21+ });
22+ expect(parseAppError(err)).toEqual({
23+ headline: "Rate limit hit",
24+ hint: "Wait a moment and retry, or switch model with /model",
25+ });
26+});
27+
28+test("parseAppError - APICallError 401", () => {
29+ const err = new APICallError({
30+ message: "Unauthorized",
31+ statusCode: 401,
32+ url: "https://api.example.com",
33+ requestBodyValues: {},
34+ });
35+ expect(parseAppError(err)).toEqual({
36+ headline: "Auth failed",
37+ hint: "Check the relevant provider API key env var",
38+ });
39+});
40+
41+test("parseAppError - APICallError generic", () => {
42+ const err = new APICallError({
43+ message: "Bad Gateway",
44+ statusCode: 502,
45+ url: "https://api.example.com",
46+ requestBodyValues: {},
47+ });
48+ expect(parseAppError(err)).toEqual({
49+ headline: "API error 502",
50+ hint: "https://api.example.com",
51+ });
52+});
53+
54+test("parseAppError - RetryError", () => {
55+ const inner = new APICallError({
56+ message: "Rate limit",
57+ statusCode: 429,
58+ url: "https://api.example.com",
59+ requestBodyValues: {},
60+ });
61+ const err = new RetryError({
62+ message: "Retries failed",
63+ reason: "maxRetriesExceeded",
64+ errors: [inner],
65+ });
66+ expect(parseAppError(err)).toEqual({
67+ headline: "Retries exhausted: Rate limit hit",
68+ hint: "Wait a moment and retry, or switch model with /model",
69+ });
70+});
71+
72+test("parseAppError - NoContentGeneratedError", () => {
73+ const err = new NoContentGeneratedError({ message: "Empty" });
74+ expect(parseAppError(err)).toEqual({
75+ headline: "Model returned empty response",
76+ hint: "Try rephrasing or switching model with /model",
77+ });
78+});
79+
80+test("parseAppError - LoadAPIKeyError", () => {
81+ const err = new LoadAPIKeyError({ message: "Missing key" });
82+ expect(parseAppError(err)).toEqual({
83+ headline: "API key not found",
84+ hint: "Set the relevant provider env var",
85+ });
86+});
87+
88+test("parseAppError - NoSuchModelError", () => {
89+ const err = new NoSuchModelError({
90+ modelId: "gpt-4",
91+ modelType: "languageModel",
92+ });
93+ expect(parseAppError(err)).toEqual({
94+ headline: "Model not found",
95+ hint: "Use /model to pick a valid model",
96+ });
97+});
98+
99+test("parseAppError - ECONNREFUSED code", () => {
100+ const err = Object.assign(new Error("Boom"), { code: "ECONNREFUSED" });
101+ expect(parseAppError(err)).toEqual({
102+ headline: "Connection failed",
103+ hint: "Check network or local server",
104+ });
105+});
106+
107+test("parseAppError - ECONNREFUSED message", () => {
108+ const err = new Error("fetch failed: ECONNREFUSED 127.0.0.1:11434");
109+ expect(parseAppError(err)).toEqual({
110+ headline: "Connection failed",
111+ hint: "Check network or local server",
112+ });
113+});
114+
115+test("parseAppError - fallback", () => {
116+ const err = new Error("Something went wrong\nSome more details");
117+ expect(parseAppError(err)).toEqual({
118+ headline: "Something went wrong",
119+ });
120+});