refactor: improve error handling for getting projects
This commit is contained in:
		| @@ -2,7 +2,7 @@ use serde::{Deserialize, Serialize}; | ||||
| use std::fmt::Display; | ||||
| use std::str::FromStr; | ||||
|  | ||||
| #[derive(Serialize, Deserialize, Debug)] | ||||
| #[derive(Serialize, Deserialize, Clone, Debug)] | ||||
| pub enum Error { | ||||
|     ServerInternal, | ||||
| } | ||||
|   | ||||
| @@ -1,7 +1,9 @@ | ||||
| use std::fmt::Display; | ||||
| use std::str::FromStr; | ||||
| use serde::Deserialize; | ||||
| use serde_with::serde_derive::Serialize; | ||||
|  | ||||
| #[derive(Debug)] | ||||
| #[derive(Serialize, Deserialize, Clone, Debug)] | ||||
| pub struct ErrorVec<T> { | ||||
|     errors: Vec<T>, | ||||
| } | ||||
|   | ||||
| @@ -33,14 +33,20 @@ pub(crate) async fn create_project(new_project: NewProject) | ||||
|  | ||||
| #[server] | ||||
| pub(crate) async fn get_projects() | ||||
|     -> Result<Vec<Project>, ServerFnError> { | ||||
|     -> Result<Vec<Project>, ServerFnError<ErrorVec<Error>>> { | ||||
|     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 | ||||
|         .select(Project::as_select()) | ||||
|         .load::<Project>(&mut connection)?; | ||||
|         .load::<Project>(&mut connection) | ||||
|         .map_err::<ErrorVec<Error>, _>( | ||||
|             |_| vec![Error::ServerInternal].into() | ||||
|         )?; | ||||
|  | ||||
|     Ok(results) | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Matouš Volf
					Matouš Volf