Error the trait Handler<_, _, _>
is not implemented for fn item
#1735
Answered
by
davidpdrsn
uselessscat
asked this question in
Q&A
-
Hello, Im having the following error which I dont know what it means (Im new to rust and axum):
Here is the important part of the code: // user-service/src/drivers/web/app.rs
pub async fn run_web_server() {
let connection_pool: Pool<Postgres> = connection::get_connection_pool().await;
let router: Router = Router::new()
.route("/user", get(users_routes::get_user_by_username)) // here is the error
.with_state(connection_pool);
let addr = SocketAddr::from(([0, 0, 0, 0], 8000));
axum::Server::bind(&addr)
.serve(router.into_make_service())
.await
.unwrap();
} // user-service/src/drivers/web/user/routes.rs
use axum::{extract::State, Json};
use sqlx::PgPool;
use crate::{
application::users::{
entities::User, services::UserService,
},
drivers::{
db::users::repository::UserRepository,
web::{
auth::jwt::Session,
errors::Error,
user::{mappers::UserApiMapper, schemas::GetUserResponse},
},
},
};
pub async fn get_user_by_username(
State(pool): State<PgPool>,
session: Session,
) -> Result<Json<GetUserResponse>, Error> {
let user: User = UserService::new(&UserRepository::new(pool))
.get_user_by_id(session.user_id)
.await?;
Ok(Json(UserApiMapper::to_response(user)))
} // user-service/src/application/users/services.rs
use crate::application::{
errors::Error,
users::{entities::User, repositories::AbstractUserRepository},
};
pub struct UserService<'a> {
user_repository: &'a dyn AbstractUserRepository,
}
impl<'a> UserService<'a> {
pub fn new(user_repository: &'a impl AbstractUserRepository) -> Self {
UserService { user_repository }
}
pub async fn get_user_by_id(&self, id: i32) -> Result<User, Error> {
let user = self.user_repository.get_user_by_id(id).await?;
Ok(user)
}
} // user-service/src/drivers/db/users/repository.rs
use async_trait::async_trait;
use sqlx::{Error as SqlxError, Pool, Postgres};
use crate::{
application::{
errors::Error,
users::{entities::User, repositories::AbstractUserRepository},
},
drivers::db::users::{mappers::UserDbMapper, models::UserModel},
};
pub struct UserRepository {
pub db_connection: Pool<Postgres>,
}
impl UserRepository {
pub fn new(connection: Pool<Postgres>) -> UserRepository {
UserRepository {
db_connection: connection,
}
}
}
#[async_trait]
impl AbstractUserRepository for UserRepository {
async fn get_user_by_username(&self, username: &str) -> Result<User, Error> {
let result = sqlx::query_as::<_, UserModel>("SELECT * FROM users WHERE username = $1")
.bind(username)
.fetch_one(&self.db_connection)
.await;
match result {
Ok(user) => Ok(UserDbMapper::to_entity(user)),
Err(e) => match e {
SqlxError::RowNotFound => {
Err(Error::EntityNotFound(String::from("User not found.")))
}
_ => Err(Error::UnknownError(String::from("Unknown error."))),
},
}
}
} // user-service/src/drivers/web/errors.rs
use axum::{
http::StatusCode,
response::{IntoResponse, Response},
Json,
};
use serde_json::json;
pub use crate::application::errors::Error;
impl IntoResponse for Error {
fn into_response(self) -> Response {
let (status, error_message) = match self {
Error::WrongCredentials(s) => (StatusCode::UNAUTHORIZED, s),
Error::MissingCredentials(s) => (StatusCode::BAD_REQUEST, s),
Error::SessionCreation(s) => (StatusCode::INTERNAL_SERVER_ERROR, s),
Error::InvalidSession(s) => (StatusCode::BAD_REQUEST, s),
Error::EntityNotFound(s) => (StatusCode::NOT_FOUND, s),
Error::UnknownError(s) => (StatusCode::INTERNAL_SERVER_ERROR, s),
};
let body = Json(json!({
"error": error_message,
}));
(status, body).into_response()
}
} Im not sure how to debug or how to fix this, thanks in advance! |
Beta Was this translation helpful? Give feedback.
Answered by
davidpdrsn
Feb 7, 2023
Replies: 2 comments 5 replies
-
Have you tried https://docs.rs/axum/latest/axum/attr.debug_handler.html? |
Beta Was this translation helpful? Give feedback.
5 replies
Answer selected by
uselessscat
-
This solved my problem: #438 |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Have you tried https://docs.rs/axum/latest/axum/attr.debug_handler.html?