refactor: improve error handling for getting projects

This commit is contained in:
Matouš Volf 2024-08-22 23:34:49 +02:00
parent 3735bb6fdb
commit 982897058e
3 changed files with 13 additions and 5 deletions

View File

@ -2,7 +2,7 @@ use serde::{Deserialize, Serialize};
use std::fmt::Display; use std::fmt::Display;
use std::str::FromStr; use std::str::FromStr;
#[derive(Serialize, Deserialize, Debug)] #[derive(Serialize, Deserialize, Clone, Debug)]
pub enum Error { pub enum Error {
ServerInternal, ServerInternal,
} }

View File

@ -1,7 +1,9 @@
use std::fmt::Display; use std::fmt::Display;
use std::str::FromStr; use std::str::FromStr;
use serde::Deserialize;
use serde_with::serde_derive::Serialize;
#[derive(Debug)] #[derive(Serialize, Deserialize, Clone, Debug)]
pub struct ErrorVec<T> { pub struct ErrorVec<T> {
errors: Vec<T>, errors: Vec<T>,
} }

View File

@ -33,14 +33,20 @@ pub(crate) async fn create_project(new_project: NewProject)
#[server] #[server]
pub(crate) async fn get_projects() pub(crate) async fn get_projects()
-> Result<Vec<Project>, ServerFnError> { -> Result<Vec<Project>, ServerFnError<ErrorVec<Error>>> {
use crate::schema::projects::dsl::*; use crate::schema::projects::dsl::*;
let mut connection = establish_database_connection()?; let mut connection = establish_database_connection()
.map_err::<ErrorVec<Error>, _>(
|_| vec![Error::ServerInternal].into()
)?;
let results = projects let results = projects
.select(Project::as_select()) .select(Project::as_select())
.load::<Project>(&mut connection)?; .load::<Project>(&mut connection)
.map_err::<ErrorVec<Error>, _>(
|_| vec![Error::ServerInternal].into()
)?;
Ok(results) Ok(results)
} }