Repository navigation
Expand file tree
/
Copy patherrorMapping.test.ts
More file actions
268 lines (237 loc) · 8.94 KB
/
Copy patherrorMapping.test.ts
File metadata and controls
268 lines (237 loc) · 8.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/**
* Unit tests for structured MCP error mapping (#407).
*
* Covers the four failure classes called out in the issue — network failure,
* 402 payment, contract NotFound, and validation — plus the guarantee that the
* mapping is deterministic and always carries a next step.
*/
import { describe, it, expect } from "vitest";
import {
categorizeStatus,
extractDetail,
formatMappedError,
isTimeoutError,
mapHttpError,
mapRegistryError,
mapTransportError,
mcpError,
throwHttpError,
type ErrorCategory,
} from "./errorMapping.js";
describe("categorizeStatus", () => {
it("maps the statuses the MindVault surface actually returns", () => {
expect(categorizeStatus(400)).toBe("validation");
expect(categorizeStatus(401)).toBe("auth");
expect(categorizeStatus(402)).toBe("payment");
expect(categorizeStatus(403)).toBe("auth");
expect(categorizeStatus(404)).toBe("not_found");
expect(categorizeStatus(408)).toBe("timeout");
expect(categorizeStatus(409)).toBe("conflict");
expect(categorizeStatus(422)).toBe("validation");
expect(categorizeStatus(429)).toBe("rate_limit");
expect(categorizeStatus(500)).toBe("server");
expect(categorizeStatus(503)).toBe("server");
expect(categorizeStatus(504)).toBe("timeout");
});
it("falls back deterministically outside the known ranges", () => {
expect(categorizeStatus(418)).toBe("validation");
expect(categorizeStatus(200)).toBe("unknown");
});
});
describe("extractDetail", () => {
it("prefers the conventional error fields in order", () => {
expect(extractDetail({ error: "boom" })).toBe("boom");
expect(extractDetail({ message: "nope" })).toBe("nope");
expect(extractDetail({ detail: "why" })).toBe("why");
expect(extractDetail({ reason: "because" })).toBe("because");
expect(extractDetail({ error: "first", message: "second" })).toBe("first");
});
it("falls back to the raw payload rather than dropping server detail", () => {
expect(extractDetail({ unexpected: 1 })).toBe('{"unexpected":1}');
expect(extractDetail("plain text")).toBe("plain text");
});
it("describes empty and missing bodies explicitly", () => {
expect(extractDetail(null)).toBe("no response body");
expect(extractDetail(undefined)).toBe("no response body");
expect(extractDetail(" ")).toBe("empty response body");
});
});
describe("isTimeoutError", () => {
it("recognises aborts and timeouts", () => {
const abort = new Error("aborted");
abort.name = "AbortError";
const timeout = new Error("slow");
timeout.name = "TimeoutError";
expect(isTimeoutError(abort)).toBe(true);
expect(isTimeoutError(timeout)).toBe(true);
expect(isTimeoutError(new Error("request timed out after 15000ms"))).toBe(true);
});
it("does not treat ordinary network errors as timeouts", () => {
expect(isTimeoutError(new Error("fetch failed"))).toBe(false);
expect(isTimeoutError(null)).toBe(false);
expect(isTimeoutError("string")).toBe(false);
});
});
describe("mapTransportError — network failure", () => {
it("classifies an unreachable service as a network error", () => {
const mapped = mapTransportError({
operation: "MindVault API request failed",
source: "api",
error: new Error("fetch failed"),
});
expect(mapped.category).toBe("network");
expect(mapped.source).toBe("api");
expect(mapped.status).toBeUndefined();
expect(mapped.summary).toContain("fetch failed");
expect(mapped.action).toContain("connectivity");
});
it("separates a timeout from a plain network failure", () => {
const abort = new Error("The operation was aborted");
abort.name = "AbortError";
const mapped = mapTransportError({
operation: "Horizon request failed",
source: "horizon",
error: abort,
});
expect(mapped.category).toBe("timeout");
expect(mapped.action).toContain("MINDVAULT_HTTP_TIMEOUT_MS");
});
it("preserves the underlying cause in the summary", () => {
const mapped = mapTransportError({
operation: "Browse failed",
source: "api",
error: new Error("ECONNREFUSED 127.0.0.1:4021"),
});
expect(mapped.summary).toBe("Browse failed: ECONNREFUSED 127.0.0.1:4021");
});
});
describe("mapHttpError — 402 payment", () => {
it("classifies a 402 as payment and points at the wallet", () => {
const mapped = mapHttpError({
operation: "Buy failed [402]",
source: "x402",
status: 402,
data: { error: "payment rejected" },
});
expect(mapped.category).toBe("payment");
expect(mapped.source).toBe("x402");
expect(mapped.status).toBe(402);
expect(mapped.summary).toBe("Buy failed [402]: payment rejected");
expect(mapped.action).toContain("mindvault_wallet_info");
expect(mapped.action).toContain("USDC");
});
});
describe("mapHttpError — validation", () => {
it("classifies a 400 as validation and tells the agent to fix arguments", () => {
const mapped = mapHttpError({
operation: "Publish failed",
source: "api",
status: 400,
data: { error: "price must be a positive decimal string" },
});
expect(mapped.category).toBe("validation");
expect(mapped.summary).toContain("price must be a positive decimal string");
expect(mapped.action).toContain("Correct the invalid arguments");
});
it("classifies a 422 the same way", () => {
expect(
mapHttpError({ operation: "Publish failed", source: "api", status: 422, data: {} }).category,
).toBe("validation");
});
});
describe("mapRegistryError — contract NotFound", () => {
it("classifies a missing registry entry as not_found with a registration path", () => {
const mapped = mapRegistryError({
operation: "Registry lookup",
message: "Resource not found",
notFound: true,
});
expect(mapped.category).toBe("not_found");
expect(mapped.source).toBe("registry");
expect(mapped.action).toContain("mindvault_register_onchain");
expect(mapped.action).toContain("not registered on-chain");
});
it("classifies any other contract failure as a contract error", () => {
const mapped = mapRegistryError({
operation: 'Contract error for resource "res-1"',
message: "Unauthorized",
});
expect(mapped.category).toBe("contract");
expect(mapped.action).toContain("mindvault_registry_info");
});
it("can attribute JSON-RPC level failures to Soroban instead of the contract", () => {
const mapped = mapRegistryError({
operation: "RPC error",
message: '{"code":-32602}',
source: "soroban",
});
expect(mapped.source).toBe("soroban");
expect(formatMappedError(mapped)).toContain("Source: Soroban RPC");
});
});
describe("formatMappedError", () => {
it("renders summary, machine-readable classification, and next step", () => {
const text = formatMappedError(
mapHttpError({
operation: "Browse failed",
source: "api",
status: 500,
data: { error: "Internal server error" },
}),
);
expect(text.split("\n")).toHaveLength(3);
expect(text).toContain("Browse failed: Internal server error");
expect(text).toContain("Source: MindVault API · Category: server · HTTP 500");
expect(text).toMatch(/^Next: /m);
});
it("omits the HTTP segment when there is no status", () => {
const text = formatMappedError(
mapTransportError({ operation: "Browse failed", source: "api", error: new Error("boom") }),
);
expect(text).toContain("Source: MindVault API · Category: network");
expect(text).not.toContain("HTTP");
});
it("is deterministic for the same input", () => {
const build = () =>
formatMappedError(
mapHttpError({ operation: "Preview failed", source: "api", status: 404, data: {} }),
);
expect(build()).toBe(build());
});
it("always supplies a non-empty next step for every category", () => {
const categories: ErrorCategory[] = [
"network",
"timeout",
"payment",
"validation",
"auth",
"not_found",
"conflict",
"rate_limit",
"server",
"contract",
"unknown",
];
const statuses = [0, 408, 402, 400, 401, 404, 409, 429, 500];
for (const status of statuses) {
const mapped = mapHttpError({ operation: "op", source: "api", status, data: {} });
expect(categories).toContain(mapped.category);
expect(mapped.action.length).toBeGreaterThan(0);
}
});
});
describe("mcpError / throwHttpError", () => {
it("produces a throwable carrying the formatted text", () => {
const err = mcpError(
mapHttpError({ operation: "Search failed", source: "api", status: 503, data: {} }),
);
expect(err).toBeInstanceOf(Error);
expect(err.message).toContain("Search failed");
expect(err.message).toContain("Category: server");
});
it("throwHttpError throws rather than returning", () => {
expect(() =>
throwHttpError({ operation: "Register failed", source: "api", status: 401, data: {} }),
).toThrow("Category: auth");
});
});