Skip to content

Commit bca3a28

Browse files
committed
chore: update error handling
1 parent 8f3b663 commit bca3a28

1 file changed

Lines changed: 25 additions & 5 deletions

File tree

src/admin-api.ts

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,26 @@ interface HonoEnv {
1919
};
2020
}
2121

22+
function isErrorNamed(err: unknown, name: string): err is Error {
23+
return err instanceof Error && err.name === name;
24+
}
25+
26+
function isBadRequestError(err: unknown): boolean {
27+
return err instanceof BadRequestError || isErrorNamed(err, 'BadRequestError');
28+
}
29+
30+
function isNotFoundError(err: unknown): boolean {
31+
return err instanceof NotFoundError || isErrorNamed(err, 'NotFoundError');
32+
}
33+
34+
function isForbiddenError(err: unknown): boolean {
35+
return err instanceof ForbiddenError || isErrorNamed(err, 'ForbiddenError');
36+
}
37+
38+
function getErrorMessage(err: unknown): string {
39+
return err instanceof Error ? err.message : String(err);
40+
}
41+
2242
// ===== Schema definitions =====
2343

2444
const ProjectDto = z
@@ -153,15 +173,15 @@ export function createAdminApi(engine: Engine): OpenAPIHono<HonoEnv> {
153173
return err.getResponse();
154174
}
155175

156-
if (err instanceof BadRequestError) {
157-
return c.json({error: err.message}, 400);
176+
if (isBadRequestError(err)) {
177+
return c.json({error: getErrorMessage(err)}, 400);
158178
}
159179

160-
if (err instanceof NotFoundError) {
161-
return c.json({error: err.message}, 404);
180+
if (isNotFoundError(err)) {
181+
return c.json({error: getErrorMessage(err)}, 404);
162182
}
163183

164-
if (err instanceof ForbiddenError) {
184+
if (isForbiddenError(err)) {
165185
return c.json({error: 'Forbidden'}, 403);
166186
}
167187

0 commit comments

Comments
 (0)