This exists to encapsulate a number of common patterns when returning errors from Axum routes.
These patterns include:
- wanting to return error responses as a JSON object
- to automatically convert errors raised in the route hander into that standardised JSON response
- provide appropriate default error messages for public use, which takes into account the status code in use
- a need to optionally add extra data to the error JSON output
## A basic tutorial
Below is a mock example route to download a User object:
```rust
use ::axum::extract::State;
use ::axum_route_error::RouteError;
use ::sea_orm::DatabaseConnection;
pub async fn route_get_user(
State(ref db): State,
Path(username): Path
) -> Result {
// If this errors, then a `RouteError::new_internal_server()` is returned.
let user = get_user_from_database(db, &username).await?;
Ok(user)
}
```
If the `get_user_from_database` function returns an error,
then the handler will return a Response.
The Response will have a 500 status code (for an internal error),
and return the following output:
```json
{
"error": "An unexpected error occurred"
}
```
### Different `RouteError` types
Let's presume `get_user_from_database` returns a `Result