@sentzunhat/zacatl 0.0.5 → 0.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/errors.ts DELETED
@@ -1,72 +0,0 @@
1
- import { ZodError } from "zod";
2
- import { isError } from "lodash";
3
-
4
- import { logger } from "./logs";
5
- import { FastifyError, FastifyReply, FastifyRequest } from "fastify";
6
-
7
- const isZodError = (error: unknown): error is ZodError =>
8
- isError(error) && "ZodError" === error.name;
9
-
10
- const handleApiError = async (handler: () => Promise<void>) => {
11
- try {
12
- await handler();
13
- } catch (err) {
14
- const { request, response } = err as {
15
- request: { data: unknown };
16
- response: { data: unknown };
17
- };
18
- if (request)
19
- logger.error("request", { logData: { request: { data: request.data } } });
20
- if (response)
21
- logger.error("response", {
22
- logData: { response: { data: response.data } },
23
- });
24
-
25
- if (err) {
26
- logger.error("errored", { logData: { error: err } });
27
- throw err;
28
- }
29
- }
30
- };
31
-
32
- const handleRouteError = (
33
- error: FastifyError,
34
- request: FastifyRequest,
35
- reply: FastifyReply
36
- ) => {
37
- console.log("====================================");
38
- console.log({ request });
39
- console.log("====================================");
40
-
41
- if (isZodError(error)) {
42
- const zodError = error as ZodError;
43
- const logDataZodError = {
44
- name: zodError.name,
45
- issues: zodError.issues,
46
- };
47
- return reply.status(400).send({ ok: false, error: logDataZodError });
48
- }
49
-
50
- if (error instanceof SyntaxError) {
51
- reply.status(400).send({ error: "Bad Request", message: error.message });
52
- } else if (error instanceof ZodError) {
53
- reply
54
- .status(422)
55
- .send({ error: "Validation Error", message: error.errors });
56
- } else {
57
- reply
58
- .status(500)
59
- .send({ error: "Internal Server Error", message: error.message });
60
- }
61
-
62
- return reply.status(400).send({
63
- ok: false,
64
- error: {
65
- name: error.name,
66
- message: error.message,
67
- error,
68
- },
69
- });
70
- };
71
-
72
- export { isZodError, handleApiError, handleRouteError };