-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror-handling.test.ts
More file actions
205 lines (171 loc) · 7.64 KB
/
error-handling.test.ts
File metadata and controls
205 lines (171 loc) · 7.64 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
import { describe, expect, it } from "bun:test";
import {
getDomainErrorStatusCode,
isBracketDomainError,
isDomainError,
isHeatDomainError,
withErrorHandling,
} from "../../../src/api/middleware/error-handling.js";
import {
BracketAlreadyExistsError,
DivisionNotFoundError,
InsufficientParticipantsError,
} from "../../../src/domain/bracket/bracket-service.js";
import {
HeatAlreadyExistsError,
HeatDoesNotExistError,
InvalidHeatRulesError,
NonUniqueRiderIdsError,
RiderAlreadyInHeatError,
RiderNotInHeatError,
ScoreMustBeInValidRangeError,
ScoreNotFoundError,
ScoreUUIDAlreadyExistsError,
} from "../../../src/domain/heat/errors.js";
describe("error-handling middleware", () => {
describe("isHeatDomainError", () => {
it("should return true for heat domain errors", () => {
expect(isHeatDomainError(new HeatAlreadyExistsError("test"))).toBe(true);
expect(isHeatDomainError(new HeatDoesNotExistError("test"))).toBe(true);
expect(isHeatDomainError(new NonUniqueRiderIdsError())).toBe(true);
expect(isHeatDomainError(new RiderNotInHeatError("test", "test"))).toBe(true);
expect(isHeatDomainError(new ScoreMustBeInValidRangeError(15))).toBe(true);
expect(isHeatDomainError(new ScoreUUIDAlreadyExistsError("test"))).toBe(true);
expect(isHeatDomainError(new InvalidHeatRulesError())).toBe(true);
expect(isHeatDomainError(new RiderAlreadyInHeatError("test", "test"))).toBe(true);
});
it("should return false for non-heat errors", () => {
expect(isHeatDomainError(new Error("generic error"))).toBe(false);
expect(isHeatDomainError(new DivisionNotFoundError("test"))).toBe(false);
expect(isHeatDomainError("not an error")).toBe(false);
expect(isHeatDomainError(null)).toBe(false);
expect(isHeatDomainError(undefined)).toBe(false);
});
});
describe("isBracketDomainError", () => {
it("should return true for bracket domain errors", () => {
expect(isBracketDomainError(new BracketAlreadyExistsError("test"))).toBe(true);
expect(isBracketDomainError(new DivisionNotFoundError("test"))).toBe(true);
expect(isBracketDomainError(new InsufficientParticipantsError(1))).toBe(true);
});
it("should return false for non-bracket errors", () => {
expect(isBracketDomainError(new Error("generic error"))).toBe(false);
expect(isBracketDomainError(new HeatAlreadyExistsError("test"))).toBe(false);
expect(isBracketDomainError("not an error")).toBe(false);
expect(isBracketDomainError(null)).toBe(false);
});
});
describe("isDomainError", () => {
it("should return true for any domain error", () => {
expect(isDomainError(new HeatAlreadyExistsError("test"))).toBe(true);
expect(isDomainError(new DivisionNotFoundError("test"))).toBe(true);
expect(isDomainError(new BracketAlreadyExistsError("test"))).toBe(true);
});
it("should return false for non-domain errors", () => {
expect(isDomainError(new Error("generic error"))).toBe(false);
expect(isDomainError("not an error")).toBe(false);
});
});
describe("getDomainErrorStatusCode", () => {
it("should return 404 for DivisionNotFoundError", () => {
const error = new DivisionNotFoundError("test-division");
expect(getDomainErrorStatusCode(error)).toBe(404);
});
it("should return 404 for HeatDoesNotExistError", () => {
const error = new HeatDoesNotExistError("test-heat");
expect(getDomainErrorStatusCode(error)).toBe(404);
});
it("should return 404 for ScoreNotFoundError", () => {
const error = new ScoreNotFoundError("test-score");
expect(getDomainErrorStatusCode(error)).toBe(404);
});
it("should return 400 for heat domain errors", () => {
expect(getDomainErrorStatusCode(new HeatAlreadyExistsError("test"))).toBe(400);
expect(getDomainErrorStatusCode(new NonUniqueRiderIdsError())).toBe(400);
expect(getDomainErrorStatusCode(new RiderNotInHeatError("test", "test"))).toBe(400);
expect(getDomainErrorStatusCode(new ScoreMustBeInValidRangeError(15))).toBe(400);
expect(getDomainErrorStatusCode(new ScoreUUIDAlreadyExistsError("test"))).toBe(400);
expect(getDomainErrorStatusCode(new InvalidHeatRulesError())).toBe(400);
expect(getDomainErrorStatusCode(new RiderAlreadyInHeatError("test", "test"))).toBe(400);
});
it("should return 400 for bracket domain errors (except DivisionNotFoundError)", () => {
expect(getDomainErrorStatusCode(new BracketAlreadyExistsError("test"))).toBe(400);
expect(getDomainErrorStatusCode(new InsufficientParticipantsError(1))).toBe(400);
});
});
describe("withErrorHandling", () => {
it("should return response from successful handler", async () => {
const handler = async () => {
return new Response(JSON.stringify({ success: true }), { status: 200 });
};
const response = await withErrorHandling(handler);
expect(response.status).toBe(200);
const data = (await response.json()) as { success: boolean };
expect(data.success).toBe(true);
});
it("should return 400 for heat domain errors", async () => {
const handler = async () => {
throw new HeatAlreadyExistsError("heat-123");
};
const response = await withErrorHandling(handler);
expect(response.status).toBe(400);
const data = (await response.json()) as { error: string };
expect(data.error).toContain("heat-123");
});
it("should return 404 for DivisionNotFoundError", async () => {
const handler = async () => {
throw new DivisionNotFoundError("division-456");
};
const response = await withErrorHandling(handler);
expect(response.status).toBe(404);
const data = (await response.json()) as { error: string };
expect(data.error).toContain("division-456");
});
it("should return 400 for bracket domain errors", async () => {
const handler = async () => {
throw new BracketAlreadyExistsError("bracket-789");
};
const response = await withErrorHandling(handler);
expect(response.status).toBe(400);
const data = (await response.json()) as { error: string };
expect(data.error).toContain("bracket-789");
});
it("should return 500 for generic Error instances", async () => {
const handler = async () => {
throw new Error("Something went wrong");
};
const response = await withErrorHandling(handler);
expect(response.status).toBe(500);
const data = (await response.json()) as { error: string };
expect(data.error).toBe("Something went wrong");
});
it("should return 500 for unknown error types", async () => {
const handler = async () => {
throw "string error" as any;
};
const response = await withErrorHandling(handler);
expect(response.status).toBe(500);
const data = (await response.json()) as { error: string };
expect(data.error).toBe("Internal server error");
});
it("should log errors with context when provided", async () => {
const consoleSpy = {
errors: [] as unknown[],
log: (...args: unknown[]) => consoleSpy.errors.push(args),
};
const originalConsoleError = console.error;
console.error = consoleSpy.log;
try {
const handler = async () => {
throw new Error("Test error");
};
await withErrorHandling(handler, "testHandler");
expect(consoleSpy.errors.length).toBeGreaterThan(0);
const errorMessage = String(consoleSpy.errors[0]);
expect(errorMessage).toContain("testHandler");
} finally {
console.error = originalConsoleError;
}
});
});
});