Stage 9 - Validation and Error Handling

Validation and error handling are part of the API contract. Validation protects the application from invalid input before business logic starts. Error handling tells the client what failed, where it failed, and what can be corrected. A good API does not return a stack trace, random string, or HTML error page. It returns a stable JSON error format with a status, code, message, field errors when relevant, and a correlation id for support.

Stage 9 - Validation and Error Handling

The Problem This Solves

API design becomes important when more than one client depends on the same backend. A controller method can be easy to write and still be hard to use. The client needs names that match the domain, response bodies that do not change randomly, errors that are predictable, and list endpoints that keep working when data grows. Good API design is the difference between a server that merely responds and a product interface that teams can safely build on.

A useful way to think about this topic is contract first. The contract is not only a generated document. It is the promise that a request with a certain method, path, parameters, body, authentication state, and headers will receive a response with a known status code and shape. When a backend developer changes that promise without care, the frontend, mobile app, partner integration, or automated job can break even though the server still starts.

How To Think About It

Start with the business action, then translate it into API language. If the user wants to see orders, the resource is probably orders, not getOrdersData. If the user wants to cancel an order, check whether cancellation is a state change on an existing order, a sub-resource, or a domain command that needs its own endpoint. This decision should be visible in the URL, method, response code, and error format.

Do not optimize for cleverness. Optimize for the developer who opens the API six months later. Names should be boring and obvious. Optional parameters should have clear defaults. A response should include only what the client is allowed to know. Internal database names, stack traces, enum experiments, and temporary migration fields should not leak into the public contract unless they are deliberately part of it.

Concrete Example

GET /api/orders?status=PAID&page=0&size=20&sort=createdAt,desc
Accept: application/json

A clean endpoint like this tells a clear story. The client is asking for orders, not calling a backend function by name. The query parameters are explicit: one filter, one page request, and one sort rule. The server can validate those parameters, document them in OpenAPI, return a stable response shape, and use a consistent error format if the request is invalid.

Useful Reference

Concept Meaning
validation Checking request data
field error Problem tied to one input field
error code Stable machine-readable reason
correlation id Identifier used to trace one request

Common Mistakes

  • Designing endpoints from controller method names instead of resource names.
  • Returning whatever the database entity currently looks like.
  • Treating validation, errors, pagination, and versioning as details to add later.
  • Changing response shapes without checking existing clients.
  • Documenting the API only after implementation, when the design is already hard to change.
Glossary
REST
Architectural style for designing APIs around resources, representations, HTTP methods, and stateless requests.
resource
A domain object exposed by an API, such as order, user, invoice, or payment.
endpoint
A concrete API operation identified by an HTTP method and path.
contract
The stable promise an API gives about requests, responses, status codes, and schemas.
pagination
Splitting a large result set into smaller pages or cursor windows.
sorting
Returning collection items in a defined and stable order.
filtering
Narrowing a collection response by explicit query parameters.
API versioning
Managing contract changes so existing clients are not broken without warning.
OpenAPI
Machine-readable specification that describes HTTP API paths, operations, schemas, and responses.
Swagger UI
Browser interface that renders an OpenAPI contract and lets developers try requests.
validation
Checking request data before business logic runs.
error handling
Returning predictable error responses for invalid requests or failed operations.
HATEOAS
REST constraint where responses include links that guide available next actions.
Richardson Maturity Model
Model that describes REST maturity from RPC-style endpoints to hypermedia links.