mirror of
https://github.com/tokio-rs/axum.git
synced 2024-10-23 17:36:39 +02:00
Add error handling and dependency injection example (#23)
This commit is contained in:
parent
4fc3d8b5ba
commit
6a16cd40ca
3 changed files with 159 additions and 1 deletions
|
@ -51,7 +51,7 @@ tokio = { version = "1.6.1", features = ["macros", "rt", "rt-multi-thread"] }
|
||||||
tokio-postgres = "0.7.2"
|
tokio-postgres = "0.7.2"
|
||||||
tracing = "0.1"
|
tracing = "0.1"
|
||||||
tracing-subscriber = "0.2"
|
tracing-subscriber = "0.2"
|
||||||
uuid = "0.8"
|
uuid = { version = "0.8", features = ["serde"] }
|
||||||
|
|
||||||
[dev-dependencies.tower]
|
[dev-dependencies.tower]
|
||||||
version = "0.4"
|
version = "0.4"
|
||||||
|
|
146
examples/error_handling_and_dependency_injection.rs
Normal file
146
examples/error_handling_and_dependency_injection.rs
Normal file
|
@ -0,0 +1,146 @@
|
||||||
|
//! Example showing how to convert errors into responses and how one might do
|
||||||
|
//! dependency injection using trait objects.
|
||||||
|
|
||||||
|
#![allow(dead_code)]
|
||||||
|
|
||||||
|
use awebframework::{
|
||||||
|
async_trait,
|
||||||
|
extract::{Extension, Json, UrlParams},
|
||||||
|
prelude::*,
|
||||||
|
response::IntoResponse,
|
||||||
|
AddExtensionLayer,
|
||||||
|
};
|
||||||
|
use http::StatusCode;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use serde_json::json;
|
||||||
|
use std::{net::SocketAddr, sync::Arc};
|
||||||
|
use tower_http::trace::TraceLayer;
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
#[tokio::main]
|
||||||
|
async fn main() {
|
||||||
|
tracing_subscriber::fmt::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
|
||||||
|
let app = route("/users/:id", get(users_show))
|
||||||
|
.route("/users", post(users_create))
|
||||||
|
// Add our `user_repo` to all request's extensions so handlers can access
|
||||||
|
// it.
|
||||||
|
.layer(AddExtensionLayer::new(user_repo))
|
||||||
|
// Add tracing because why not.
|
||||||
|
.layer(TraceLayer::new_for_http());
|
||||||
|
|
||||||
|
// Run our application
|
||||||
|
let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
|
||||||
|
tracing::debug!("listening on {}", addr);
|
||||||
|
app.serve(&addr).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(
|
||||||
|
UrlParams((user_id,)): UrlParams<(Uuid,)>,
|
||||||
|
Extension(user_repo): Extension<DynUserRepo>,
|
||||||
|
) -> Result<response::Json<User>, AppError> {
|
||||||
|
let user = user_repo.find(user_id).await?;
|
||||||
|
|
||||||
|
Ok(user.into())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Handler for `POST /users`.
|
||||||
|
async fn users_create(
|
||||||
|
Json(params): Json<CreateUser>,
|
||||||
|
Extension(user_repo): Extension<DynUserRepo>,
|
||||||
|
) -> Result<response::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) -> http::Response<Body> {
|
||||||
|
let (status, error_json) = match self {
|
||||||
|
AppError::UserRepo(UserRepoError::NotFound) => {
|
||||||
|
(StatusCode::NOT_FOUND, json!("User not found"))
|
||||||
|
}
|
||||||
|
AppError::UserRepo(UserRepoError::InvalidUsername) => {
|
||||||
|
(StatusCode::UNPROCESSABLE_ENTITY, json!("Invalid username"))
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let mut response = response::Json(json!({
|
||||||
|
"error": error_json,
|
||||||
|
}))
|
||||||
|
.into_response();
|
||||||
|
|
||||||
|
*response.status_mut() = status;
|
||||||
|
|
||||||
|
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)]
|
||||||
|
struct CreateUser {
|
||||||
|
username: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Errors that can happen when using the user repo.
|
||||||
|
#[derive(Debug)]
|
||||||
|
enum UserRepoError {
|
||||||
|
NotFound,
|
||||||
|
InvalidUsername,
|
||||||
|
}
|
|
@ -188,6 +188,12 @@ where
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<T> From<T> for Html<T> {
|
||||||
|
fn from(inner: T) -> Self {
|
||||||
|
Self(inner)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// A JSON response.
|
/// A JSON response.
|
||||||
///
|
///
|
||||||
/// Can be created from any type that implements [`serde::Serialize`].
|
/// Can be created from any type that implements [`serde::Serialize`].
|
||||||
|
@ -238,3 +244,9 @@ where
|
||||||
res
|
res
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<T> From<T> for Json<T> {
|
||||||
|
fn from(inner: T) -> Self {
|
||||||
|
Self(inner)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue