-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror.ts
More file actions
133 lines (123 loc) · 4.1 KB
/
error.ts
File metadata and controls
133 lines (123 loc) · 4.1 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
import Boom from "@hapi/boom";
import { ErrorHandler, Context } from "hono";
import { HTTPException } from "hono/http-exception";
import { logMessageToFile, logObjectToFile } from "./logger";
import { z } from "zod";
export const withServiceErrorHandling = <T extends any[], R>(handler: (...args: T) => Promise<R>) => {
return async (...args: T): Promise<R> => {
try {
return await handler(...args);
} catch (error: any) {
if (process.env.NODE_ENV !== "test") {
console.log(error);
console.error(error);
}
if (Boom.isBoom(error)) {
throw error;
} else if (error?.name === "QueryFailedError") {
switch (error.code) {
case "23505":
throw Boom.conflict("Resource already exists");
case "23503":
throw Boom.badRequest("Invalid reference");
case "23502":
throw Boom.badRequest("Missing required field");
default:
throw Boom.internal(error, { message: "An unexpected error occured" });
}
} else {
throw Boom.boomify(error);
}
}
};
};
export const withControllerErrorHandling = <T extends any[], R>(handler: (ctx: Context, ...args: T) => Promise<R>) => {
return async (ctx: Context, ...args: T) => {
try {
return await handler(ctx, ...args);
} catch (error) {
if (Boom.isBoom(error)) {
return ctx.json(
{ error: error.output.payload.message },
error.output.statusCode as 400 // instead of ContentfulErrorCode, we need to bound it to 400 or 500 so the types don't blow up down the road
);
}
if (error instanceof z.ZodError) {
return ctx.json({ error: z.prettifyError(error) }, 400);
}
// log unknown errors:
if (error instanceof Error) {
logErrors(error, ctx);
} else if (error instanceof Object) {
logObjectToFile(error);
} else {
logMessageToFile("Unknown error occured");
}
return ctx.json({ error: "Internal Server Error" }, 500);
}
};
};
export const errorHandler: ErrorHandler = (err: Error, c: Context) => {
logErrors(err, c);
if (err instanceof HTTPException) {
return c.json(
{
error: err.message,
status: err.status,
},
err.status
);
}
return c.json(
{
error: "Internal Server Error",
...(process.env.NODE_ENV === "development" && {
details: err.message,
stack: err.stack,
}),
},
500
);
};
const logErrors = (err: Error, c: Context) => {
const includeStack = process.env.NODE_ENV === "development";
if (process.env.NODE_ENV !== "test") {
console.error("Error occurred:", {
message: err.message,
...(includeStack && {
stack: err.stack,
}),
path: c.req.path,
method: c.req.method,
timestamp: new Date().toISOString(),
});
}
logMessageToFile(
"Error occurred:",
`message: ${err.message}`,
...(includeStack ? [`stack: ${err.stack}`] : []),
`path: ${c.req.path}`,
`method: ${c.req.method}`,
`timestamp: ${new Date().toISOString()}`
);
};
export function openApiErrorCodes(description: string) {
return {
400: {
content: {
"application/json": {
schema: z.object({ error: z.string() }),
},
},
description,
},
500: {
content: {
"application/json": {
schema: z.object({ error: z.string() }),
},
},
description,
},
};
}