Elffuss BraidHub

kikocis/continue public

80eede9a6f83 · 4.6 KiBRawPermalink
1import { formatError, formatAnthropicError } from "./formatError.js";
2
3describe("formatError", () => {
4 it("should format Error objects correctly", () => {
5 const error = new Error("Test error message");
6 expect(formatError(error)).toBe("Test error message");
7 });
8
9 it("should return string errors as-is", () => {
10 const error = "String error message";
11 expect(formatError(error)).toBe("String error message");
12 });
13
14 it("should extract message from error objects", () => {
15 const error = { message: "Object error message" };
16 expect(formatError(error)).toBe("Object error message");
17 });
18
19 it("should recursively format nested error objects", () => {
20 const error = { error: { message: "Nested error message" } };
21 expect(formatError(error)).toBe("Nested error message");
22 });
23
24 it("should extract details from error objects", () => {
25 const error = { details: "Error details" };
26 expect(formatError(error)).toBe("Error details");
27 });
28
29 it("should extract description from error objects", () => {
30 const error = { description: "Error description" };
31 expect(formatError(error)).toBe("Error description");
32 });
33
34 it("should format API errors with status and message", () => {
35 const error = {
36 status: 404,
37 error: { message: "Not found" },
38 };
39 // The actual implementation prioritizes nested error.message over status formatting
40 expect(formatError(error)).toBe("Not found");
41 });
42
43 it("should format network errors with code and syscall", () => {
44 const error = {
45 code: "ECONNREFUSED",
46 syscall: "connect",
47 };
48 expect(formatError(error)).toBe("Network error: ECONNREFUSED in connect");
49 });
50
51 it("should join error arrays", () => {
52 const error = {
53 errors: ["First error", "Second error", "Third error"],
54 };
55 expect(formatError(error)).toBe("First error, Second error, Third error");
56 });
57
58 it("should JSON stringify complex objects", () => {
59 const error = {
60 code: "CUSTOM_ERROR",
61 data: { id: 123, name: "test" },
62 };
63 expect(formatError(error)).toBe(
64 '{"code":"CUSTOM_ERROR","data":{"id":123,"name":"test"}}',
65 );
66 });
67
68 it("should handle objects that cannot be JSON stringified", () => {
69 const circular: any = { name: "circular" };
70 circular.self = circular;
71
72 const result = formatError(circular);
73 expect(result).toBe("An error occurred: [object Object]");
74 });
75
76 it("should handle null values", () => {
77 expect(formatError(null)).toBe("null");
78 });
79
80 it("should handle undefined values", () => {
81 expect(formatError(undefined)).toBe("undefined");
82 });
83
84 it("should handle numbers", () => {
85 expect(formatError(42)).toBe("42");
86 });
87
88 it("should handle boolean values", () => {
89 expect(formatError(true)).toBe("true");
90 expect(formatError(false)).toBe("false");
91 });
92
93 it("should handle empty objects", () => {
94 expect(formatError({})).toBe("{}");
95 });
96
97 it("should handle empty arrays", () => {
98 expect(formatError([])).toBe("[]");
99 });
100
101 it("should prioritize message over other properties", () => {
102 const error = {
103 message: "Primary message",
104 description: "Secondary description",
105 details: "Tertiary details",
106 };
107 expect(formatError(error)).toBe("Primary message");
108 });
109
110 it("should prioritize error property over details", () => {
111 const error = {
112 error: "Nested error",
113 details: "Error details",
114 };
115 expect(formatError(error)).toBe("Nested error");
116 });
117
118 it("should handle deeply nested error structures", () => {
119 const error = {
120 error: {
121 error: {
122 message: "Deep nested error",
123 },
124 },
125 };
126 expect(formatError(error)).toBe("Deep nested error");
127 });
128
129 it("should format API errors with status only when no nested error message", () => {
130 const error = {
131 status: 404,
132 error: { code: "NOT_FOUND" },
133 };
134 expect(formatError(error)).toBe('{"code":"NOT_FOUND"}');
135 });
136});
137
138describe("formatAnthropicError", () => {
139 it("should format invalid API key authentication errors", () => {
140 const error = new Error(
141 '{"type":"error","error":{"type":"authentication_error","message":"invalid x-api-key"}}',
142 );
143 expect(formatAnthropicError(error)).toBe("Anthropic: Invalid API key");
144 });
145
146 it("should handle error objects in general", () => {
147 const error = new Error("some error"); // some error we haven't catalogued yet
148 console.log(formatAnthropicError(error));
149 expect(formatAnthropicError(error)).toBe("Anthropic: some error");
150 });
151
152 it("should handle undefined values", () => {
153 expect(formatAnthropicError(undefined)).toBe("Anthropic: undefined");
154 });
155});