Skip to content

Commit bf0d90f

Browse files
committed
improve error handling
1 parent 82f0269 commit bf0d90f

File tree

6 files changed

+51
-12
lines changed

6 files changed

+51
-12
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@omer-x/next-openapi-route-handler",
3-
"version": "0.3.0",
3+
"version": "0.3.1",
44
"description": "a Next.js plugin to generate OpenAPI documentation from route handlers",
55
"keywords": [
66
"next.js",

src/core/body.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { HttpMethod } from "~/types/http";
22
import type { FixedRequest } from "~/types/request";
33
import { resolveContent } from "./content";
44
import type { RequestBodyObject } from "@omer-x/openapi-types/request-body";
5-
import type { ZodType } from "zod";
5+
import type { ZodError, ZodType } from "zod";
66

77
export function resolveRequestBody(source?: ZodType<unknown> | string, isFormData: boolean = false) {
88
if (!source) return undefined;
@@ -26,7 +26,23 @@ export async function parseRequestBody<B>(
2626
const body = Array.from(formData.keys()).reduce((collection, key) => ({
2727
...collection, [key]: formData.get(key),
2828
}), {});
29-
return schema.parse(body);
29+
try {
30+
return schema.parse(body);
31+
} catch (error) {
32+
if (process.env.NODE_ENV !== "production") {
33+
// eslint-disable-next-line no-console
34+
console.log((error as ZodError).issues);
35+
}
36+
throw new Error("PARSE_FORM_DATA");
37+
}
38+
}
39+
try {
40+
return schema.parse(await request.json());
41+
} catch (error) {
42+
if (process.env.NODE_ENV !== "production") {
43+
// eslint-disable-next-line no-console
44+
console.log((error as ZodError).issues);
45+
}
46+
throw new Error("PARSE_REQUEST_BODY");
3047
}
31-
return schema.parse(await request.json());
3248
}

src/core/path-params.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
import { safeParse } from "./zod-error-handler";
2-
import type { ZodType } from "zod";
2+
import type { ZodError, ZodType } from "zod";
33

44
export default function parsePathParams<T>(source?: T, schema?: ZodType<T>) {
55
if (!schema || !source) return null;
6-
return safeParse(schema, source as Record<string, unknown>);
6+
try {
7+
return safeParse(schema, source as Record<string, unknown>);
8+
} catch (error) {
9+
if (process.env.NODE_ENV !== "production") {
10+
// eslint-disable-next-line no-console
11+
console.log((error as ZodError).issues);
12+
}
13+
throw new Error("PARSE_PATH_PARAMS");
14+
}
715
}

src/core/search-params.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { safeParse } from "./zod-error-handler";
2-
import type { ZodType } from "zod";
2+
import type { ZodError, ZodType } from "zod";
33

44
function serializeArray(value: string[]) {
55
return value.join(",");
@@ -19,5 +19,13 @@ export default function parseSearchParams<T>(source: URLSearchParams, schema?: Z
1919
[key]: serializeArray(values),
2020
};
2121
}, {});
22-
return safeParse(schema, params);
22+
try {
23+
return safeParse(schema, params);
24+
} catch (error) {
25+
if (process.env.NODE_ENV !== "production") {
26+
// eslint-disable-next-line no-console
27+
console.log((error as ZodError).issues);
28+
}
29+
throw new Error("PARSE_SEARCH_PARAMS");
30+
}
2331
}

src/index.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,15 @@ export default function createRoute<M extends HttpMethod, PP, QP, RB>(input: Rou
4747
const body = await parseRequestBody(request, input.method, input.requestBody ?? undefined, input.hasFormData) as RB;
4848
return await input.action({ pathParams, queryParams, body });
4949
} catch (error) {
50-
if (error instanceof Error && error.constructor.name === "ZodError") {
51-
return new Response(null, { status: 400 });
50+
if (error instanceof Error) {
51+
switch (error.message) {
52+
case "PARSE_FORM_DATA":
53+
case "PARSE_REQUEST_BODY":
54+
case "PARSE_SEARCH_PARAMS":
55+
return new Response(null, { status: 400 });
56+
case "PARSE_PATH_PARAMS":
57+
return new Response(null, { status: 404 });
58+
}
5259
}
5360
return new Response(null, { status: 500 });
5461
}

0 commit comments

Comments
 (0)