Error Handling Philosophy
In Mizu, error handling follows a simple principle: handlers return errors, the framework handles them. This means:- Your handlers focus on business logic
- Errors flow up naturally through
return err - One central error handler formats all responses
- Panics are caught and treated like errors
Returning Errors from Handlers
Any handler can return an error to indicate something went wrong:Default Behavior
If you donβt configure a custom error handler, Mizu will:- Log the error with the request context
- Return a
500 Internal Server Errorto the client - Include a generic error message (no sensitive details exposed)
The ErrorHandler
Define a global error handler to customize how all errors are processed:The error handler should always send a response. If it doesnβt, the client will receive an empty response.
Custom Error Types
Create custom error types to carry additional information like HTTP status codes:Using Custom Errors in Handlers
Handling Custom Errors
Error Wrapping with Context
Use Goβs error wrapping to add context as errors bubble up:Unwrapping in Error Handler
Panic Recovery
Mizu automatically recovers from panics and converts them to errors. This prevents your server from crashing:*mizu.PanicError:
Best Practices
Do
Donβt
Summary
Next steps
Middleware
Error handling in middleware.
Logging
Structured logging for errors.