Improve error handling

This commit is contained in:
2023-05-07 18:20:19 -04:00
parent c2c0f7a28d
commit de157a3b1e
7 changed files with 293 additions and 32 deletions

View File

@@ -1,19 +1,19 @@
use axum::{extract::{State, Path}, Json};
use sqlx::PgPool;
use crate::error::AppError;
use crate::error::Error;
use crate::models::item::{create_item, get_item, CreateItem, Item};
pub async fn get(
State(pool): State<PgPool>,
Path(id): Path<i32>,
) -> Result<Json<Item>, AppError> {
) -> Result<Json<Item>, Error> {
Ok(Json(get_item(pool, id).await?))
}
pub async fn post(
State(pool): State<PgPool>,
Json(payload): Json<CreateItem>,
) -> Result<Json<Item>, AppError> {
) -> Result<Json<Item>, Error> {
Ok(Json(create_item(pool, payload).await?))
}

View File

@@ -1,9 +1,9 @@
use axum::{extract::State, Json};
use sqlx::PgPool;
use crate::error::AppError;
use crate::error::Error;
use crate::models::item::{get_items, Item};
pub async fn get(State(pool): State<PgPool>) -> Result<Json<Vec<Item>>, AppError> {
pub async fn get(State(pool): State<PgPool>) -> Result<Json<Vec<Item>>, Error> {
Ok(Json(get_items(pool).await?))
}