Common HTTP Error Codes and Example Responses
• 200 OK: Request succeeded
• 400 Bad Request: Client sent invalid data
• 401 Unauthorized: Authentication required or failed
• 403 Forbidden: Client not allowed to access resource
• 404 Not Found: Resource not found
• 409 Conflict: Conflict with current resource state
• 500 Internal Server Error: Unexpected server error
Best Practices for Using Error Codes in User Flows
Signup:
- 400 Bad Request → Missing fields like email/password.
- 409 Conflict → Email already registered.
- 500 Internal Server Error → Unexpected DB or hashing error.
Login:
- 400 Bad Request → Missing email/password.
- 401 Unauthorized → Wrong credentials.
- 500 Internal Server Error → Unexpected DB error.
OTP Generation:
- 400 Bad Request → Email missing/invalid.
- 200 OK → OTP sent successfully.
- 500 Internal Server Error → Email service failure.
OTP Verification:
- 400 Bad Request → OTP input missing.
- 401 Unauthorized → OTP incorrect or expired.
- 200 OK → OTP verified, user authenticated.
- 500 Internal Server Error → Unexpected hashing/DB error.
Protected Routes:
- 401 Unauthorized → No token provided or invalid.
- 403 Forbidden → User authenticated but not authorized for this resource.
-
Example [Link] Responses
[Link](200).json({ success: true, message: "OTP sent" })
[Link](400).json({ success: false, errorCode: "INVALID_INPUT",
message: "Email is required" });
[Link](401).json({ success: false, errorCode: "INVALID_OTP",
message: "OTP is incorrect" });
[Link](403).json({ success: false, errorCode: "FORBIDDEN",
message: "You are not allowed to perform this action" });
[Link](404).json({ success: false, errorCode: "NOT_FOUND",
message: "User not found" });
[Link](409).json({ success: false, errorCode: "USER_EXISTS",
message: "User already registered" });
[Link](500).json({ success: false, errorCode: "SERVER_ERROR",
message: "Something went wrong on the server" });