Files
axum/examples/error-handling-and-dependency-injection/src/main.rs
T

156 lines
4.2 KiB
Rust
Raw Normal View History

//! Example showing how to convert errors into responses and how one might do
//! dependency injection using trait objects.
2021-08-02 23:09:09 +02:00
//!
//! Run with
//!
//! ```not_rust
//! cargo run -p example-error-handling-and-dependency-injection
2021-08-02 23:09:09 +02:00
//! ```
2021-07-09 21:36:14 +02:00
use axum::{
async_trait,
2022-08-17 17:13:31 +02:00
extract::{Path, State},
http::StatusCode,
response::{IntoResponse, Response},
routing::{get, post},
2022-03-01 00:39:22 +01:00
Json, Router,
};
use serde::{Deserialize, Serialize};
use serde_json::json;
use std::sync::Arc;
2022-03-06 12:37:00 +01:00
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
use uuid::Uuid;
#[tokio::main]
async fn main() {
2022-03-06 12:37:00 +01:00
tracing_subscriber::registry()
.with(
tracing_subscriber::EnvFilter::try_from_default_env()
2022-03-06 12:37:00 +01:00
.unwrap_or_else(|_| "example_error_handling_and_dependency_injection=debug".into()),
)
2022-03-06 12:37:00 +01:00
.with(tracing_subscriber::fmt::layer())
.init();
// Inject a `UserRepo` into our handlers via a trait object. This could be
// the live implementation or just a mock for testing.
let user_repo = Arc::new(ExampleUserRepo) as DynUserRepo;
// Build our application with some routes
2022-11-18 12:02:58 +01:00
let app = Router::new()
.route("/users/:id", get(users_show))
2022-11-18 12:02:58 +01:00
.route("/users", post(users_create))
.with_state(user_repo);
// Run our application
let listener = tokio::net::TcpListener::bind("127.0.0.1:3000")
2021-06-19 12:50:33 +02:00
.await
.unwrap();
tracing::debug!("listening on {}", listener.local_addr().unwrap());
axum::serve(listener, app).await.unwrap();
}
/// Handler for `GET /users/:id`.
///
/// Extracts the user repo from request extensions and calls it. `UserRepoError`s
/// are automatically converted into `AppError` which implements `IntoResponse`
/// so it can be returned from handlers directly.
async fn users_show(
2021-08-06 16:17:57 +08:00
Path(user_id): Path<Uuid>,
2022-08-17 17:13:31 +02:00
State(user_repo): State<DynUserRepo>,
2021-08-18 00:04:15 +02:00
) -> Result<Json<User>, AppError> {
let user = user_repo.find(user_id).await?;
Ok(user.into())
}
/// Handler for `POST /users`.
async fn users_create(
2022-08-17 17:13:31 +02:00
State(user_repo): State<DynUserRepo>,
Json(params): Json<CreateUser>,
2021-08-18 00:04:15 +02:00
) -> Result<Json<User>, AppError> {
let user = user_repo.create(params).await?;
Ok(user.into())
}
/// Our app's top level error type.
enum AppError {
/// Something went wrong when calling the user repo.
UserRepo(UserRepoError),
}
/// This makes it possible to use `?` to automatically convert a `UserRepoError`
/// into an `AppError`.
impl From<UserRepoError> for AppError {
fn from(inner: UserRepoError) -> Self {
AppError::UserRepo(inner)
}
}
impl IntoResponse for AppError {
fn into_response(self) -> Response {
let (status, error_message) = match self {
AppError::UserRepo(UserRepoError::NotFound) => {
(StatusCode::NOT_FOUND, "User not found")
}
AppError::UserRepo(UserRepoError::InvalidUsername) => {
(StatusCode::UNPROCESSABLE_ENTITY, "Invalid username")
}
};
let body = Json(json!({
"error": error_message,
}));
(status, body).into_response()
}
}
/// Example implementation of `UserRepo`.
struct ExampleUserRepo;
#[async_trait]
impl UserRepo for ExampleUserRepo {
async fn find(&self, _user_id: Uuid) -> Result<User, UserRepoError> {
unimplemented!()
}
async fn create(&self, _params: CreateUser) -> Result<User, UserRepoError> {
unimplemented!()
}
}
/// Type alias that makes it easier to extract `UserRepo` trait objects.
type DynUserRepo = Arc<dyn UserRepo + Send + Sync>;
/// A trait that defines things a user repo might support.
#[async_trait]
trait UserRepo {
/// Loop up a user by their id.
async fn find(&self, user_id: Uuid) -> Result<User, UserRepoError>;
/// Create a new user.
async fn create(&self, params: CreateUser) -> Result<User, UserRepoError>;
}
#[derive(Debug, Serialize)]
struct User {
id: Uuid,
username: String,
}
#[derive(Debug, Deserialize)]
2021-10-07 16:49:57 +02:00
#[allow(dead_code)]
struct CreateUser {
username: String,
}
/// Errors that can happen when using the user repo.
#[derive(Debug)]
enum UserRepoError {
#[allow(dead_code)]
NotFound,
#[allow(dead_code)]
InvalidUsername,
}