In your Express error handler, check for Mongoose ValidationError, CastError (invalid ObjectId), and MongoDB error code 11000 (duplicate unique key) so clients receive clean 400 or 409 responses instead of generic 500 errors.
ADVERTISEMENT