Skip to content
Backend & APIChapter 25 / 57Intermediate6 min read

Error Handling & Error Envelope

API error response format ek jaisa rakhein, sahi status code dein, 500 mein andar ki details leak na karein aur asal cause server log mein likhein.

On this page

1. Yeh practice kya hai?

Envelope matlab ek fixed lifafa. Is course mein Next.js ke liye shape yeh hai:

json
{ "error": "Human readable message", "code": "OPTIONAL_MACHINE_CODE" }

Shape kuch bhi ho sakta hai, jaise { message, errors, success }. Asal baat yeh hai ke poori app mein ek ho, aur message strings constants hon taake tests wahi constant assert karein jo route return karta hai.

2. Kyun zaroori hai?

Ek endpoint { message } deta hai, doosra { err }, teesra status 200 ke saath { success: false }. Frontend developer har call ke liye alag if likhta hai, aur ek jagah bhool jata hai. User ko "Something went wrong" ki jagah blank screen milti hai.

Security wala bug zyada khatarnak hai. Catch block mein error.message client ko bhej diya gaya. Ab response mein likha aata hai duplicate key value violates unique constraint "users_email_key". Attacker ko table ka naam, column aur yeh bhi pata chal gaya ke yeh email registered hai.

Bina rule keRule ke saath
Har endpoint ka apna error shapeEk envelope, client ek jagah handle karta hai
Error par bhi status 200Status code khud batata hai kya hua
500 mein SQL error ya stack trace leakGeneric message, details log mein
Login error batata hai ke account mojood haiAuth errors account ka hona ya na hona chhupate hain
Empty catch error ko nigal jata haiError ya to handle hota hai ya log ho kar upar jata hai

3. Kab follow karein?

LevelKitna zarooriKyun
Learning projectStatus codes sahi rakhna seekhein401, 403, 404, 422 ka farq yahin samajh aata hai
Production codeLazmiClient, tests aur monitoring sab is shape par chalte hain
Senior levelEnvelope aur error classes design karnaNaya endpoint khud ba khud sahi errors deta hai

Kab seekhein: jab aap ka frontend pehli dafa kisi API ka error dikhana chahe.

4. Bura vs acha code

Bura: har jagah apna tareeqa

ts
export async function POST(request: Request) {
  try {
    const body = await request.json();
    const invite = await acceptInvite(body.token);
    return Response.json({ success: true, invite });
  } catch (e) {
    const error = e as Error;
    return Response.json({ success: false, message: error.message, stack: error.stack });
  }
}

Status hamesha 200 hai. Expired invite aur database crash dono ek jaise lagte hain. Stack trace aur internal message seedha browser mein.

Acha: error classes aur ek mapper

File lib/errors/errors.ts:

ts
import { NextResponse } from "next/server";

import { GENERIC_ERROR } from "./errors.types";

export class AppError extends Error {
  constructor(
    message: string,
    readonly status: number,
    readonly code: string,
  ) {
    super(message);
  }
}

export function toErrorResponse(error: unknown, scope: string) {
  if (error instanceof AppError) {
    return NextResponse.json({ error: error.message, code: error.code }, { status: error.status });
  }

  console.error(`[${scope}]`, error);

  return NextResponse.json({ error: GENERIC_ERROR }, { status: 500 });
}

Service expected halat mein AppError throw karti hai:

ts
export async function acceptInvite(token: string) {
  const invite = await findInviteByTokenHash(hashToken(token));
  if (!invite) {
    throw new AppError(INVITE_MESSAGES.notFound, 404, "INVITE_NOT_FOUND");
  }
  if (invite.expiresAt < new Date()) {
    throw new AppError(INVITE_MESSAGES.expired, 410, "INVITE_EXPIRED");
  }

  return markInviteAccepted(invite.id);
}

Handler sirf map karta hai:

ts
export async function POST(request: Request) {
  const parsed = acceptInviteSchema.safeParse(await request.json().catch(() => null));
  if (!parsed.success) {
    return NextResponse.json({ error: INVITE_MESSAGES.invalidInput }, { status: 422 });
  }

  try {
    const invite = await acceptInvite(parsed.data.token);
    return NextResponse.json({ invite });
  } catch (error) {
    return toErrorResponse(error, "invites:accept");
  }
}

Farq kya hai?

CheezBura codeAcha code
StatusHamesha 200404, 410, 422, 500 jo sach hai
Shapesuccess, message, stackerror aur code
Unexpected errorMessage aur stack client koGeneric message, cause log mein [scope] ke saath
MessagesInline stringsConstants, tests bhi wahi use karte hain

Status codes ka chhota naqsha

StatusKab
400Request hi kharab hai, jaise invalid JSON
401Caller authenticated nahi
403Authenticated hai lekin ijazat nahi
404Resource nahi mila
409Conflict, jaise duplicate ya out of stock
422Shape theek, values validation fail
429Rate limited, Retry-After header ke saath
500Sach mein unexpected failure
502 / 503Upstream service ya configuration ka masla

5. Kaise apply karein?

Error ka safar
Service expected halat mein typed error throw kare
Unexpected error ko na pakro, upar jane do
Handler ek mapper se envelope banaye
Known error apne status ke saath
Baqi sab 500, cause log mein, client ko generic message

6. Exceptions aur common mistakes

Kabhi kabhi 404 jaan boojh kar 403 ki jagah diya jata hai taake attacker ko pata na chale ke resource mojood hai. Yeh theek hai agar soch samajh kar kiya gaya ho aur team ko pata ho. Login aur "forgot password" mein bhi jawab ek jaisa rakhein, chahe account ho ya na ho.

Galati: Catch block khaali chhor dena

Why? Error ghayab ho jata hai, user ko galat success dikhta hai aur log mein koi nishaan nahi hota.

Behtar: Ya to error ko handle karo aur sahi response do, ya log kar ke upar throw karo.

Galati: 500 response mein error.message bhejna

Why? Database, file path aur library ki andar ki details attacker tak pohanchti hain.

Behtar: Client ko generic constant do aur asal error scope prefix ke saath server par log karo.

Galati: Signed in user ko permission na hone par 401 dena

Why? Client samajhta hai session khatam ho gaya aur user ko baar baar login page par bhejta hai.

Behtar: 401 sirf missing ya invalid credentials ke liye, ijazat na ho to 403.

Galati: Har layer par log kar ke rethrow karna

Why? Ek hi error log mein teen chaar dafa aata hai aur asal jagah dhoondhna mushkil ho jata hai.

Behtar: Error ko ek jagah log karo, aam taur par wahan jahan woh response mein badalta hai.

7. Pehle aur baad mein kya seekhein?

Final quick revision

CheckSawal
EnvelopeKya har error ek hi shape mein hai?
StatusKya status code sach batata hai?
LeakKya 500 kabhi error.message ya stack bhejta hai?
LogKya unexpected error [scope] ke saath log hota hai?
ConstantsKya messages constants hain jo tests bhi use karte hain?
AuthKya auth errors account ka hona chhupate hain?