A very performant and light (2mb in memory) link shortener and tracker. Written in Rust and React and uses Postgres/SQLite.
1use actix_web::{HttpResponse, ResponseError};
2use thiserror::Error;
3
4#[derive(Error, Debug)]
5pub enum AppError {
6 #[error("Database error: {0}")]
7 Database(#[from] sqlx::Error),
8
9 #[error("Not found")]
10 NotFound,
11
12 #[error("Invalid input: {0}")]
13 InvalidInput(String),
14
15 #[error("Authentication error: {0}")]
16 Auth(String),
17
18 #[error("Unauthorized")]
19 Unauthorized,
20}
21
22impl ResponseError for AppError {
23 fn error_response(&self) -> HttpResponse {
24 match self {
25 AppError::NotFound => HttpResponse::NotFound().json("Not found"),
26 AppError::Database(err) => HttpResponse::InternalServerError().json(format!("Database error: {}", err)), // Show actual error
27 AppError::InvalidInput(msg) => HttpResponse::BadRequest().json(msg),
28 AppError::Auth(msg) => HttpResponse::BadRequest().json(msg),
29 AppError::Unauthorized => HttpResponse::Unauthorized().json("Unauthorized"),
30 }
31 }
32}