Advertisement
Intermediate Node.js, Error Handling

What is the difference between a validation error and a business-logic error, and how might a Node.js application handle each differently?

Short answer

A validation error indicates the request itself is malformed or violates basic data constraints (a missing required field, an invalid email format) -- typically caught early, before any business logic runs, and mapped to a 400 Bad Request.

Save Node.js mock interview
Advertisement
AModel answer
A validation error indicates the request itself is malformed or violates basic data constraints (a missing required field, an invalid email format) -- typically caught early, before any business logic runs, and mapped to a 400 Bad Request. A business-logic error indicates the request is well-formed but violates a domain rule (attempting to withdraw more money than an account balance, booking an already-fully-reserved time slot) -- often mapped to a 409 Conflict or 422 Unprocessable Entity, since the request syntax was valid but the requested operation can't be completed given the current state of the system.
// Validation error: malformed request
if (!amount || amount <= 0) throw new ValidationError('Amount must be positive');

// Business-logic error: well-formed request, but violates a domain rule
if (amount > account.balance) throw new InsufficientFundsError('Insufficient balance for this withdrawal');
Real-world example
A banking API returns a 400 for a withdrawal request missing the amount field entirely (a validation error caught before touching the database), but returns a 409 for a well-formed withdrawal request that exceeds the account's actual current balance (a business-logic error only detectable after checking the account's state).
Likely follow-ups
  • Why does separating these two error categories with different status codes help API consumers write more precise error-handling logic?
  • At what layer of the application should validation typically happen versus business-logic checks?
Related topics