mirror of
https://github.com/Lumither/nocturne.git
synced 2026-10-17 18:36:21 +00:00
feat(backend/api/get/post) fix error handling
This commit is contained in:
1 file changed
+28
-5
@@ -1,17 +1,18 @@
|
||||
use std::str::FromStr;
|
||||
|
||||
use axum::extract::{Path, State};
|
||||
use axum::http::StatusCode;
|
||||
use axum::Json;
|
||||
use chrono::NaiveDateTime;
|
||||
use serde_json::{json, Map, Value};
|
||||
use sqlx::{PgPool, query, Row};
|
||||
use sqlx::{Error, PgPool, query, Row};
|
||||
use uuid::Uuid;
|
||||
|
||||
pub async fn get_post(
|
||||
State(db_connection): State<PgPool>,
|
||||
Path(post_id): Path<String>,
|
||||
) -> Result<Json<Value>, Json<Value>> {
|
||||
let meta = query(
|
||||
) -> Result<Json<Value>, (StatusCode, Json<Value>)> {
|
||||
let meta = match query(
|
||||
r#"
|
||||
SELECT *
|
||||
FROM Post
|
||||
@@ -20,8 +21,30 @@ WHERE post_id = $1;
|
||||
)
|
||||
.bind(Uuid::from_str(post_id.as_str()).expect("Failed to parse UUID"))
|
||||
.fetch_one(&db_connection)
|
||||
.await
|
||||
.expect("Failed to execute db query");
|
||||
.await {
|
||||
Ok(value) => { value }
|
||||
Err(e) => {
|
||||
return match e {
|
||||
Error::RowNotFound => {
|
||||
Err((
|
||||
StatusCode::BAD_REQUEST,
|
||||
Json::from(json!(format!(
|
||||
"{{error: {}}}",
|
||||
e.to_string()
|
||||
)))))
|
||||
}
|
||||
_ => {
|
||||
Err((
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
Json::from(json!(format!(
|
||||
"{{error: {}}}",
|
||||
e.to_string()
|
||||
)))))
|
||||
}
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
let tags = query(
|
||||
r#"
|
||||
|
||||
Reference in new issue
Block a user