Consistent Error Responses
Part of the MERN Stack Roadmap
In short
If one route returns { error: "x" }, another returns a plain string and a third returns { msg }, your React app needs a different check for each. One error shape for the whole API keeps the frontend simple.
Every error from your API should have the same JSON shape, such as { message, code, details }, so the React app can handle all failures with one piece of code.

Think of it like this
The standard complaint slip
A railway reservation counter uses one printed form for every problem: what went wrong, a reference code and extra details. Whether your ticket is waitlisted or the train is cancelled, you read it the same way. Your API's error JSON should work like that form.
Watch it happen
Messy vs consistent errors
Every route its own way
- res.send("Not found")
- res.json({ error: "Invalid email" })
- res.status(500).json({ msg: err })
- Frontend needs many special cases
One shape everywhere
- { message, code, details }
- Correct HTTP status every time
- Thrown as AppError, sent by one middleware
- Frontend reads error.message once
Throw a custom error class in your code and let a single error middleware turn it into the same JSON shape.
The big ideas
What you need to know
Pick a shape
For example { message, code, details }. message is for humans, code (like VALIDATION_ERROR) is for code to check.
Use real status codes
400 bad input, 401 not logged in, 403 not allowed, 404 not found, 409 conflict, 500 server bug.
A custom error class
class AppError extends Error carries a statusCode and code, so any layer can throw it.
Hide internals
For unexpected 500 errors, send a generic message and log the stack trace on the server only.
See it in code
Walk through the code
1// src/utils/AppError.js2export class AppError extends Error {3 constructor(statusCode, code, message, details) {4 super(message);5 this.statusCode = statusCode;6 this.code = code;7 this.details = details;8 }9}10 11// In a service: throw new AppError(404, "NOT_FOUND", "Product not found");12 13// src/middleware/errorHandler.js (registered last in app.js)14export function errorHandler(err, req, res, next) {15 if (err instanceof AppError) {16 return res.status(err.statusCode).json({17 message: err.message, code: err.code, details: err.details,18 });19 }20 console.error(err);21 res.status(500).json({ message: "Something went wrong", code: "INTERNAL_ERROR" });22}1/5A small class that adds a status and a machine-readable code to a normal Error.
Any layer throws AppError. One middleware decides how every error looks.
Save yourself the pain
Common mistakes
Oops
Sending err.stack or raw database errors to the client.
Do this instead
Log details on the server. Send a short, safe message so you don't leak internals.
Oops
Returning status 200 with { success: false } for failures.
Do this instead
Use the right HTTP status. fetch, axios and monitoring tools all rely on it.
Your mission
Standardise your errors
Quick check
A logged-in user tries to delete someone else's post. Which status fits best?
Go deeper
- 1Create an AppError class with statusCode and message and throw it from controllers.
- 2Map Mongoose validation and duplicate key errors to 400 and 409 responses.
- 3Document your error format in the README for anyone using the API.
More topics at this stop
Stuck on this topic? Ask an Insider
Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.