add initial chat server stuff

This commit is contained in:
sam 2024-01-16 22:20:19 +01:00
parent 3025f1380c
commit 0e71e9dc5f
18 changed files with 722 additions and 40 deletions

View file

@ -11,5 +11,6 @@ eyre = "0.6.11"
serde = { version = "1.0.195", features = ["derive"] }
serde_json = "1.0.111"
sqlx = "0.7.3"
thiserror = "1.0.56"
tracing = "0.1.40"
uuid = { version = "1.6.1", features = ["v7"] }

7
foxchat/src/error/mod.rs Normal file
View file

@ -0,0 +1,7 @@
use thiserror::Error;
#[derive(Error, Debug, Copy, Clone)]
pub enum QueryError {
#[error("object not found")]
NotFound,
}

View file

@ -8,6 +8,8 @@ use serde::Serialize;
use serde_json::json;
use tracing::error;
use crate::QueryError;
pub struct ApiError {
status: StatusCode,
code: ErrorCode,
@ -32,6 +34,7 @@ impl IntoResponse for ApiError {
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum ErrorCode {
InternalServerError,
ObjectNotFound,
}
impl From<sqlx::Error> for ApiError {
@ -50,6 +53,11 @@ impl From<sqlx::Error> for ApiError {
impl From<Report> for ApiError {
fn from(err: Report) -> Self {
match err.downcast_ref::<QueryError>() {
Some(e) => return (*e).into(),
None => {}
};
error!("Error in handler: {}", err);
ApiError {
status: StatusCode::INTERNAL_SERVER_ERROR,
@ -62,3 +70,15 @@ impl From<Report> for ApiError {
}
}
}
impl From<QueryError> for ApiError {
fn from(err: QueryError) -> Self {
match err {
QueryError::NotFound => ApiError {
status: StatusCode::NOT_FOUND,
code: ErrorCode::ObjectNotFound,
message: "Object not found".into(),
},
}
}
}

View file

@ -1,2 +1,5 @@
pub mod s2s;
pub mod error;
pub mod http;
pub mod s2s;
pub use error::QueryError;