Overview
The Hono OpenAPI Starter uses standardized HTTP status codes and error response formats powered by Stoker. All error responses follow a consistent structure that makes debugging and error handling predictable.HTTP Status Codes
The API uses the following HTTP status codes:Success Codes
The request succeeded. The response body contains the requested resource or result.Used for:
GET /tasks- List all tasksGET /tasks/{id}- Get a single taskPOST /tasks- Create a new taskPATCH /tasks/{id}- Update a task
A new resource was successfully created. The response body contains the created resource.
This starter uses
200 OK for resource creation instead of 201 Created for simplicity.The request succeeded, but there is no content to return.Used for:
DELETE /tasks/{id}- Delete a task
Client Error Codes
The request was malformed or contains invalid syntax.
The requested resource does not exist.Used for:
GET /tasks/{id}- Task with specified ID not foundPATCH /tasks/{id}- Task with specified ID not foundDELETE /tasks/{id}- Task with specified ID not found- Any undefined route
The request was well-formed but contains semantic errors or failed validation.Used for:
- Invalid request body (Zod validation errors)
- Invalid URL parameters
- Custom validation errors (e.g., no updates provided)
Server Error Codes
An unexpected error occurred on the server. The error handler middleware (
onError from Stoker) catches and formats these errors.Error Response Structure
Not Found (404)
When a resource is not found, the API returns a simple message object:notFoundSchema is defined in src/lib/constants.ts:15:
Validation Errors (422)
Validation errors follow the Zod error format with detailed information about what went wrong:Validation Error Examples
Missing Required Field
When creating a task without a required field:String Length Validation
When the task name exceeds the maximum length:Invalid ID Parameter
When providing an invalid ID in the URL:Custom Validation Error
When updating a task with no fields provided (fromsrc/routes/tasks/tasks.handlers.ts:48-64):
Error Handling Middleware
The API uses Stoker’s built-in error handling middleware configured insrc/lib/create-app.ts:25-26:
Not Found Handler
ThenotFound middleware catches requests to undefined routes and returns:
Global Error Handler
TheonError middleware catches unhandled exceptions and returns appropriate error responses. For validation errors, it automatically formats Zod errors into the standard error response structure.
Validation Schema Definition
Validation errors are automatically generated from Zod schemas using Stoker’screateErrorSchema function:
createErrorSchema function introspects the Zod schema and generates OpenAPI documentation for all possible validation errors.
Best Practices
Handle All Error Cases
Always handle both validation errors and not found errors in your client code:Display Validation Errors
Use thepath field to associate errors with specific form fields:
Check HTTP Status First
Always check the HTTP status code before parsing the response body, as the structure varies by status code:- 200/204: Success response or no content
- 404:
{ message: string } - 422:
{ success: false, error: { issues: [...], name: "ZodError" } } - 500:
{ message: string }(sanitized in production)
Related Documentation
- API Overview - Complete API endpoint documentation
- Type Safety - Understanding type flow in the application
- Stoker Documentation - Error handling utilities
