π Reference
πΒ Chapter
β£
β£
β£
| μμΉ | 400 μ²λ¦¬(μ λ ₯ κ²μ¦ μ€ν¨ λ±) | 500 μ²λ¦¬(μμ λͺ»ν μμΈ) | UI νμ |
|---|---|---|---|
Route Handler app/api/**/route.ts |
NextResponse.json(..., { status: 400/422 }) |
NextResponse.json(..., { status: 500 }) (λλ throw ν catch) |
JSON(λλ νμΌ/μ€νΈλ¦Ό) |
Page/Server Component app/**/page.tsx |
νμ΄μ§ μνλ‘ βμλͺ»λ μμ²β μλ΄(μνμ½λ μ§μ μ§μ μ΄λ €μ) | μμΈλ₯Ό throw β ν΄λΉ μΈκ·Έλ¨ΌνΈμ error.tsxλ‘ μ΄λ(μλ΅ 500) |
error.tsx |
Server Action "use server" |
κ° λ°νμΌλ‘ νΌ λ΄λΆμ μλ¬ νμ(μνμ½λ μ μ΄ X) | throw νλ©΄ 500 + error.tsx |
νΌ μλ¬ λ©μμ§ or error.tsx |
| 404 (νΉμ) | notFound() νΈμΆ |
β | not-found.tsx (μλ΅ 404) |
Middleware middleware.ts |
κ°λ¨ν κ·μΉ μ°¨λ¨ μ new NextResponse("...", { status: 400 }) |
β | ν μ€νΈ/리λ€μ΄λ νΈ |
// app/api/users/route.ts
import { NextResponse } from "next/server";
export async function POST(req: Request) {
try {
const body = await req.json();
// μ
λ ₯ κ²μ¦ μ€ν¨ β 400 λλ 422
if (!body.email) {
return NextResponse.json(
{ error: "email is required" },
{ status: 400 } // 422λ μμ£Ό μ¬μ©
);
}
// ...λΉμ¦λμ€ λ‘μ§...
return NextResponse.json({ ok: true }, { status: 201 });
} catch (err) {
console.error("POST /api/users error:", err);
return NextResponse.json(
{ error: "Internal Server Error" },
{ status: 500 }
);
}
}