-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into feat/api/notify-newcomer
- Loading branch information
Showing
4 changed files
with
62 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { NextResponse } from "next/server"; | ||
import { z } from "zod"; | ||
|
||
export class APIError extends Error { | ||
constructor( | ||
message: string, | ||
public status: number = 400, | ||
public code?: string | ||
) { | ||
super(message); | ||
this.name = "APIError"; | ||
} | ||
} | ||
|
||
export function handleAPIError(error: unknown) { | ||
if (error instanceof APIError) { | ||
return NextResponse.json( | ||
{ | ||
error: { | ||
message: error.message, | ||
code: error.code, | ||
}, | ||
}, | ||
{ status: error.status } | ||
); | ||
} | ||
|
||
if (error instanceof z.ZodError) { | ||
return NextResponse.json( | ||
{ | ||
error: { | ||
message: "入力値が不正です", | ||
details: error.errors, | ||
}, | ||
}, | ||
{ status: 400 } | ||
); | ||
} | ||
|
||
console.error(error); | ||
return NextResponse.json( | ||
{ error: { message: "サーバーエラーが発生しました" } }, | ||
{ status: 500 } | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from "./error"; | ||
export * from "./response"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { NextResponse } from "next/server"; | ||
|
||
export type APIResponse<T> = { | ||
data: T; | ||
status: number; | ||
}; | ||
|
||
export function createAPIResponse<T>(data: T): APIResponse<T> { | ||
return { data, status: 200 }; | ||
} | ||
|
||
export function handleAPISuccess<T>(data: T) { | ||
return NextResponse.json(createAPIResponse(data)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from "./api"; | ||
export * from "./swr"; |