Creates projects to organize tasks under. (Note the migration also contains the tables for creating folders for projects as well because adding foreign keys is a PITA in sqlite apparently). Reviewed-on: #19 Co-authored-by: Drew Galbraith <drew@tiramisu.one> Co-committed-by: Drew Galbraith <drew@tiramisu.one>
95 lines
2.4 KiB
Rust
95 lines
2.4 KiB
Rust
mod projects;
|
|
mod tasks;
|
|
|
|
use axum::{
|
|
Json,
|
|
extract::rejection::{JsonRejection, PathRejection},
|
|
http::StatusCode,
|
|
response::IntoResponse,
|
|
};
|
|
|
|
// AppError
|
|
// Borrowed from example: https://github.com/tokio-rs/axum/blob/axum-v0.8.4/examples/anyhow-error-response/src/main.rs
|
|
#[derive(Debug)]
|
|
pub enum AppError {
|
|
InternalError(anyhow::Error),
|
|
JsonExtractError(JsonRejection),
|
|
PathError(PathRejection),
|
|
Unprocessable(String),
|
|
NotFound,
|
|
}
|
|
|
|
#[derive(Serialize)]
|
|
pub struct ErrorJson {
|
|
error: String,
|
|
}
|
|
|
|
impl IntoResponse for AppError {
|
|
fn into_response(self) -> axum::response::Response {
|
|
match self {
|
|
Self::InternalError(anyhow_error) => {
|
|
error!(err = ?anyhow_error, "Internal Server Error");
|
|
(
|
|
StatusCode::INTERNAL_SERVER_ERROR,
|
|
format!("Something went wrong {}", anyhow_error),
|
|
)
|
|
.into_response()
|
|
}
|
|
Self::JsonExtractError(rej) => (
|
|
StatusCode::UNPROCESSABLE_ENTITY,
|
|
Json(ErrorJson {
|
|
error: rej.status().to_string(),
|
|
}),
|
|
)
|
|
.into_response(),
|
|
Self::Unprocessable(msg) => (
|
|
StatusCode::UNPROCESSABLE_ENTITY,
|
|
Json(ErrorJson { error: msg }),
|
|
)
|
|
.into_response(),
|
|
Self::PathError(rej) => (
|
|
StatusCode::BAD_REQUEST,
|
|
Json(ErrorJson {
|
|
error: rej.status().to_string(),
|
|
}),
|
|
)
|
|
.into_response(),
|
|
Self::NotFound => (
|
|
StatusCode::NOT_FOUND,
|
|
Json(ErrorJson {
|
|
error: "Requeted Entity Not Found".to_string(),
|
|
}),
|
|
)
|
|
.into_response(),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl From<JsonRejection> for AppError {
|
|
fn from(value: JsonRejection) -> Self {
|
|
Self::JsonExtractError(value)
|
|
}
|
|
}
|
|
|
|
impl From<PathRejection> for AppError {
|
|
fn from(value: PathRejection) -> Self {
|
|
Self::PathError(value)
|
|
}
|
|
}
|
|
|
|
impl From<anyhow::Error> for AppError {
|
|
fn from(err: anyhow::Error) -> Self {
|
|
Self::InternalError(err)
|
|
}
|
|
}
|
|
|
|
impl From<sqlx::Error> for AppError {
|
|
fn from(err: sqlx::Error) -> Self {
|
|
Self::InternalError(err.into())
|
|
}
|
|
}
|
|
|
|
pub use projects::*;
|
|
use serde::Serialize;
|
|
pub use tasks::*;
|
|
use tracing::error;
|