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
|
import { Hono } from "hono";
import { describe, expect, it } from "vitest";
import { AppError, Errors, errorHandler } from "./error-handler.js";
function createTestApp() {
const app = new Hono();
app.onError(errorHandler);
return app;
}
describe("errorHandler", () => {
describe("AppError handling", () => {
it("returns correct status and message for AppError", async () => {
const app = createTestApp();
app.get("/test", () => {
throw new AppError("Custom error", 400, "CUSTOM_ERROR");
});
const res = await app.request("/test");
expect(res.status).toBe(400);
expect(await res.json()).toEqual({
error: {
message: "Custom error",
code: "CUSTOM_ERROR",
},
});
});
it("uses default values for AppError", async () => {
const app = createTestApp();
app.get("/test", () => {
throw new AppError("Something went wrong");
});
const res = await app.request("/test");
expect(res.status).toBe(500);
expect(await res.json()).toEqual({
error: {
message: "Something went wrong",
code: "INTERNAL_ERROR",
},
});
});
});
describe("Errors factory functions", () => {
it("handles badRequest error", async () => {
const app = createTestApp();
app.get("/test", () => {
throw Errors.badRequest("Invalid input");
});
const res = await app.request("/test");
expect(res.status).toBe(400);
expect(await res.json()).toEqual({
error: {
message: "Invalid input",
code: "BAD_REQUEST",
},
});
});
it("handles unauthorized error", async () => {
const app = createTestApp();
app.get("/test", () => {
throw Errors.unauthorized();
});
const res = await app.request("/test");
expect(res.status).toBe(401);
expect(await res.json()).toEqual({
error: {
message: "Unauthorized",
code: "UNAUTHORIZED",
},
});
});
it("handles forbidden error", async () => {
const app = createTestApp();
app.get("/test", () => {
throw Errors.forbidden("Access denied");
});
const res = await app.request("/test");
expect(res.status).toBe(403);
expect(await res.json()).toEqual({
error: {
message: "Access denied",
code: "FORBIDDEN",
},
});
});
it("handles notFound error", async () => {
const app = createTestApp();
app.get("/test", () => {
throw Errors.notFound("Resource not found");
});
const res = await app.request("/test");
expect(res.status).toBe(404);
expect(await res.json()).toEqual({
error: {
message: "Resource not found",
code: "NOT_FOUND",
},
});
});
it("handles conflict error", async () => {
const app = createTestApp();
app.get("/test", () => {
throw Errors.conflict("Already exists");
});
const res = await app.request("/test");
expect(res.status).toBe(409);
expect(await res.json()).toEqual({
error: {
message: "Already exists",
code: "CONFLICT",
},
});
});
it("handles validationError", async () => {
const app = createTestApp();
app.get("/test", () => {
throw Errors.validationError("Invalid data");
});
const res = await app.request("/test");
expect(res.status).toBe(422);
expect(await res.json()).toEqual({
error: {
message: "Invalid data",
code: "VALIDATION_ERROR",
},
});
});
it("handles internal error", async () => {
const app = createTestApp();
app.get("/test", () => {
throw Errors.internal("Database connection failed");
});
const res = await app.request("/test");
expect(res.status).toBe(500);
expect(await res.json()).toEqual({
error: {
message: "Database connection failed",
code: "INTERNAL_ERROR",
},
});
});
});
describe("unknown error handling", () => {
it("handles generic Error with 500 status", async () => {
const app = createTestApp();
app.get("/test", () => {
throw new Error("Unexpected error");
});
const res = await app.request("/test");
expect(res.status).toBe(500);
const body = (await res.json()) as { error: { code: string } };
expect(body.error.code).toBe("INTERNAL_ERROR");
});
});
});
|