Skip to content

Commit

Permalink
feat: ✨ close #47 handler内で予期しないエラーが発生したら500を返すようにした
Browse files Browse the repository at this point in the history
  • Loading branch information
waonpad committed Dec 1, 2023
1 parent 7447567 commit f4708cb
Showing 1 changed file with 38 additions and 34 deletions.
72 changes: 38 additions & 34 deletions apps/web/src/app/api/_contracts/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,55 +27,59 @@ export const cHandler = <T extends ApiContract>(
| z.infer<T['response']>
>
> => {
const parsed: {
params?: z.infer<NonNullable<T['params']>>;
searchParams?: z.infer<NonNullable<T['searchParams']>>;
body?: z.infer<NonNullable<T['body']>>;
} = {
params: undefined,
searchParams: undefined,
body: undefined,
};
try {
const parsed: {
params?: z.infer<NonNullable<T['params']>>;
searchParams?: z.infer<NonNullable<T['searchParams']>>;
body?: z.infer<NonNullable<T['body']>>;
} = {
params: undefined,
searchParams: undefined,
body: undefined,
};

if (contract.params) {
const parsedParams = contract.params.safeParse(params);
if (contract.params) {
const parsedParams = contract.params.safeParse(params);

if (!parsedParams.success) {
return NextResponse.json({ error: parsedParams.error }, { status: 400 });
if (!parsedParams.success) {
return NextResponse.json({ error: parsedParams.error }, { status: 400 });
}

parsed.params = parsedParams.data;
}

parsed.params = parsedParams.data;
}
if (contract.searchParams) {
const parsedSearchParams = contract.searchParams.safeParse(reqSearchParams(req));

if (contract.searchParams) {
const parsedSearchParams = contract.searchParams.safeParse(reqSearchParams(req));
if (!parsedSearchParams.success) {
return NextResponse.json({ error: parsedSearchParams.error }, { status: 400 });
}

if (!parsedSearchParams.success) {
return NextResponse.json({ error: parsedSearchParams.error }, { status: 400 });
parsed.searchParams = parsedSearchParams.data;
}

parsed.searchParams = parsedSearchParams.data;
}
if (contract.body) {
const parsedBody = contract.body.safeParse(req.body);

if (contract.body) {
const parsedBody = contract.body.safeParse(req.body);
if (!parsedBody.success) {
return NextResponse.json({ error: parsedBody.error }, { status: 422 });
}

if (!parsedBody.success) {
return NextResponse.json({ error: parsedBody.error }, { status: 422 });
parsed.body = parsedBody.data;
}

parsed.body = parsedBody.data;
}
const response = await process(req, parsed);
const responseBody = await response.json();

const response = await process(req, parsed);
const responseBody = await response.json();
const parsedResponseBody = contract.response.safeParse(responseBody);

const parsedResponseBody = contract.response.safeParse(responseBody);
if (!parsedResponseBody.success) {
return NextResponse.json({ error: parsedResponseBody.error }, { status: 500 });
}

if (!parsedResponseBody.success) {
return NextResponse.json({ error: parsedResponseBody.error }, { status: 500 });
return NextResponse.json(parsedResponseBody.data, response);
} catch (error) {
return NextResponse.json({ error: { message: 'Internal Server Error' } }, { status: 500 });
}

return NextResponse.json(parsedResponseBody.data, response);
};
};

0 comments on commit f4708cb

Please sign in to comment.