0

I've been writing a simple crud app in Axum and Sea-orm, but I keep getting when trying to handle a Database Error.

Here's the create user function:

 
async fn create_user(Json(user_input): Json<User>) -> Result<Json<User>, Json<DbErr>> {
    let mcrypt = new_magic_crypt!(env::var("SECRET_KEY").unwrap(), 256);
    let db = db::DB::new().await;   
    let username = user_input.username;
    let encrypted_password = mcrypt.encrypt_to_base64(&user_input.password);
    

    let user_db = user::ActiveModel {
        username: Set(username.clone().into()),
        password: Set(encrypted_password.clone().into()),
        ..Default::default()
    };
    match user_db.insert(&db.db).await {
        Ok(_) => Ok(Json(User {
        username,
        password: encrypted_password
        })),
        Err(e) => Err(Json(e))
    }

    

}


And here's the the route: .route("/api/create_user", post(create_user));

This is the error I get:

error[E0277]: the trait bound `fn(axum::Json<User>) -> impl Future<Output = Result<axum::Json<User>, axum::Json<sea_orm::DbErr>>> {create_user}: Handler<_, _, _>` is not satisfied
   --> src\main.rs:35:41
    |
35  |         .route("/api/create_user", post(create_user));
    |                                    ---- ^^^^^^^^^^^ the trait `Handler<_, _, _>` is not implemented for fn item `fn(axum::Json<User>) -> impl Future<Output = Result<axum::Json<User>, axum::Json<sea_orm::DbErr>>> {create_user}`
    |                                    |
    |                                    required by a bound introduced by this call
    |
    = help: the following other types implement trait `Handler<T, S, B>`:
              <Layered<L, H, T, S, B, B2> as Handler<T, S, B2>>
              <MethodRouter<S, B> as Handler<(), S, B>>
note: required by a bound in `post`
   --> C:\Users\Lobby\.cargo\registry\src\github.com-1ecc6299db9ec823\axum-0.6.18\src\routing\method_routing.rs:407:1
    |
407 | top_level_handler_fn!(post, POST);
    | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `post`
    = note: this error originates in the macro `top_level_handler_fn` (in Nightly builds, run with -Z macro-backtrace for more info)
cafce25
  • 15,907
  • 4
  • 25
  • 31
Xetify
  • 41
  • 1
  • 4
  • 4
    If I had to guess, your orm error is not serializable to JSON. – Maximilian Burszley May 31 '23 at 20:14
  • 2
    Add `#[axum::debug_handler]` on top of `create_user()`. – Chayim Friedman Jun 01 '23 at 04:37
  • 1
    Maximilian has the right of it: [`Json` needs `Serialize` to be convertible to a `Response`](https://docs.rs/axum/latest/axum/struct.Json.html#impl-IntoResponse-for-Json%3CT%3E), [`sea_orm::DbErr`](https://docs.rs/sea-orm/latest/sea_orm/error/enum.DbErr.html) is not `Serialize`. – Masklinn Jun 01 '23 at 06:49

0 Answers0