Catching Panics in Requests
Catch Panic is used to capture crashes that occur during request processing in the program. For specific APIs, please refer to the documentation.
Note: To use
CatchPanic, you need to enable thecatch-panicfeature inCargo.toml:
Middleware Introduction
CatchPanic is a middleware designed to catch panics in handlers. When a panic occurs during request processing, it captures the panic and writes a 500 Internal Server Error to the response, preventing the entire server from crashing.
Important Note: This middleware should be used as the first middleware to ensure it can catch panics from other middlewares or handlers.
Basic Usage
Comparison with Other Frameworks for Quick Concept Understanding
Axum
Similar to the catch_panic middleware in Axum's Tower:
Gin (Go)
In the Go language's Gin framework, similar to the Recovery middleware: