Error Handling and Recover
In this lesson, you will learn how Mizu automatically handles errors and panics, and how you can define a custom global error handler usingapp.ErrorHandler.
Mizu recovers from panics internally, logs errors, and keeps your app running without crashing.
You only need to register an error handler if you want to customize how errors are shown to users.
Code
Create a file namedmain.go and add this code:
Run
Start the server:-
http://localhost:8080/
Response:Home page - all good -
http://localhost:8080/fail
Response:Something went wrong: something went wrongThe error is returned by the handler and handled by your custom function. -
http://localhost:8080/panic
Response:Something went wrong: unexpected panic occurredThe panic is recovered automatically by Mizu and passed to your error handler.
How it works
Mizu wraps every handler with built-in recovery logic. When a handler returns an error or panics:- Mizu catches the error or recovered panic.
- The error (or
*mizu.PanicErrorfor panics) is sent to your global error handler. - If no handler is defined, Mizu logs the issue and returns a plain 500 Internal Server Error.