This commit is contained in:
parent
9fb59879b2
commit
6c947abb18
1
Cargo.lock
generated
1
Cargo.lock
generated
@ -607,6 +607,7 @@ dependencies = [
|
||||
"diesel",
|
||||
"diesel-async",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"tokio",
|
||||
]
|
||||
|
||||
|
@ -12,4 +12,5 @@ ctxerr = "0.2.5"
|
||||
diesel = { version = "2.0.4", features = ["chrono"] }
|
||||
diesel-async = { version = "0.2.2", features = ["postgres", "tokio-postgres", "deadpool"] }
|
||||
serde = { version = "1.0.163", features = ["derive"] }
|
||||
serde_json = "1.0.96"
|
||||
tokio = { version = "1.28.0", features = ["parking_lot", "macros", "rt", "rt-multi-thread"] }
|
||||
|
29
src/api/error.rs
Normal file
29
src/api/error.rs
Normal file
@ -0,0 +1,29 @@
|
||||
use axum::{
|
||||
http::StatusCode,
|
||||
response::{IntoResponse, Response},
|
||||
Json,
|
||||
};
|
||||
use ctxerr::ctxerr;
|
||||
use serde_json::json;
|
||||
|
||||
#[ctxerr]
|
||||
pub enum ErrorKind {
|
||||
#[error("{0}")]
|
||||
InternalError(#[from] crate::Error),
|
||||
}
|
||||
|
||||
impl IntoResponse for Error {
|
||||
fn into_response(self) -> Response {
|
||||
let (status, error_message) = match self.kind {
|
||||
ErrorKind::InternalError(err) => (StatusCode::INTERNAL_SERVER_ERROR, err.to_string()),
|
||||
};
|
||||
|
||||
(
|
||||
status,
|
||||
Json(json! ({
|
||||
"error": error_message,
|
||||
})),
|
||||
)
|
||||
.into_response()
|
||||
}
|
||||
}
|
@ -1,3 +1,4 @@
|
||||
mod error;
|
||||
mod user;
|
||||
|
||||
use axum::{
|
||||
|
@ -6,7 +6,7 @@ use crate::db::{
|
||||
repos,
|
||||
};
|
||||
|
||||
use super::ApiState;
|
||||
use super::{error::Error, ApiState};
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct ApiUser {
|
||||
@ -18,7 +18,7 @@ pub struct ApiUser {
|
||||
pub async fn create_user(
|
||||
State(s): State<ApiState>,
|
||||
Json(payload): Json<ApiUser>,
|
||||
) -> Result<Json<User>, (StatusCode, String)> {
|
||||
) -> Result<(StatusCode, Json<User>), Error> {
|
||||
let user = repos::user::create_user(
|
||||
&s.pool,
|
||||
NewUser {
|
||||
@ -27,10 +27,9 @@ pub async fn create_user(
|
||||
email: &payload.email,
|
||||
},
|
||||
)
|
||||
.await
|
||||
.map_err(internal_error)?;
|
||||
.await?;
|
||||
|
||||
Ok(Json(user))
|
||||
Ok((StatusCode::CREATED, Json(user)))
|
||||
}
|
||||
|
||||
pub async fn list_user(State(s): State<ApiState>) -> (StatusCode, Json<Vec<User>>) {
|
||||
@ -38,10 +37,3 @@ pub async fn list_user(State(s): State<ApiState>) -> (StatusCode, Json<Vec<User>
|
||||
|
||||
(StatusCode::CREATED, Json(user))
|
||||
}
|
||||
|
||||
fn internal_error<E>(err: E) -> (StatusCode, String)
|
||||
where
|
||||
E: std::error::Error,
|
||||
{
|
||||
(StatusCode::INTERNAL_SERVER_ERROR, err.to_string())
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user