Skip to content

Commit 44a92b3

Browse files
committed
Refactor errors of storage interface
Ref. #75
1 parent 821e13e commit 44a92b3

File tree

9 files changed

+1628
-924
lines changed

9 files changed

+1628
-924
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ authors = ["Tobias Reiher <valens@ardeidae.de>"]
1616
edition = "2024"
1717

1818
[workspace.dependencies]
19+
anyhow = "1.0.97"
1920
assert_approx_eq = "1.1.0"
2021
chrono = { version = "0.4.20", default-features = false }
2122
log = { version = "0.4.26" }

crates/domain/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ edition.workspace = true
66
publish = false
77

88
[dependencies]
9+
anyhow = { workspace = true }
910
chrono = { workspace = true, default-features = false }
1011
derive_more = { version = "2.0.1", features = ["as_ref", "deref", "display", "into"] }
1112
thiserror = { workspace = true }

crates/domain/src/lib.rs

Lines changed: 112 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -18,104 +18,178 @@ use uuid::Uuid;
1818

1919
#[allow(async_fn_in_trait)]
2020
pub trait SessionRepository {
21-
async fn request_session(&self, user_id: UserID) -> Result<User, String>;
22-
async fn initialize_session(&self) -> Result<User, String>;
23-
async fn delete_session(&self) -> Result<(), String>;
21+
async fn request_session(&self, user_id: UserID) -> Result<User, ReadError>;
22+
async fn initialize_session(&self) -> Result<User, ReadError>;
23+
async fn delete_session(&self) -> Result<(), DeleteError>;
2424
}
2525

2626
#[allow(async_fn_in_trait)]
2727
pub trait VersionRepository {
28-
async fn read_version(&self) -> Result<String, String>;
28+
async fn read_version(&self) -> Result<String, ReadError>;
2929
}
3030

3131
#[allow(async_fn_in_trait)]
3232
pub trait UserRepository {
33-
async fn read_users(&self) -> Result<Vec<User>, String>;
34-
async fn create_user(&self, name: Name, sex: Sex) -> Result<User, String>;
35-
async fn replace_user(&self, user: User) -> Result<User, String>;
36-
async fn delete_user(&self, id: UserID) -> Result<UserID, String>;
33+
async fn read_users(&self) -> Result<Vec<User>, ReadError>;
34+
async fn create_user(&self, name: Name, sex: Sex) -> Result<User, CreateError>;
35+
async fn replace_user(&self, user: User) -> Result<User, UpdateError>;
36+
async fn delete_user(&self, id: UserID) -> Result<UserID, DeleteError>;
3737
}
3838

3939
#[allow(async_fn_in_trait)]
4040
pub trait BodyWeightRepository {
41-
async fn sync_body_weight(&self) -> Result<Vec<BodyWeight>, String>;
42-
async fn read_body_weight(&self) -> Result<Vec<BodyWeight>, String>;
43-
async fn create_body_weight(&self, body_weight: BodyWeight) -> Result<BodyWeight, String>;
44-
async fn replace_body_weight(&self, body_weight: BodyWeight) -> Result<BodyWeight, String>;
45-
async fn delete_body_weight(&self, date: NaiveDate) -> Result<NaiveDate, String>;
41+
async fn sync_body_weight(&self) -> Result<Vec<BodyWeight>, SyncError>;
42+
async fn read_body_weight(&self) -> Result<Vec<BodyWeight>, ReadError>;
43+
async fn create_body_weight(&self, body_weight: BodyWeight) -> Result<BodyWeight, CreateError>;
44+
async fn replace_body_weight(&self, body_weight: BodyWeight)
45+
-> Result<BodyWeight, UpdateError>;
46+
async fn delete_body_weight(&self, date: NaiveDate) -> Result<NaiveDate, DeleteError>;
4647
}
4748

4849
#[allow(async_fn_in_trait)]
4950
pub trait BodyFatRepository {
50-
async fn sync_body_fat(&self) -> Result<Vec<BodyFat>, String>;
51-
async fn read_body_fat(&self) -> Result<Vec<BodyFat>, String>;
52-
async fn create_body_fat(&self, body_fat: BodyFat) -> Result<BodyFat, String>;
53-
async fn replace_body_fat(&self, body_fat: BodyFat) -> Result<BodyFat, String>;
54-
async fn delete_body_fat(&self, date: NaiveDate) -> Result<NaiveDate, String>;
51+
async fn sync_body_fat(&self) -> Result<Vec<BodyFat>, SyncError>;
52+
async fn read_body_fat(&self) -> Result<Vec<BodyFat>, ReadError>;
53+
async fn create_body_fat(&self, body_fat: BodyFat) -> Result<BodyFat, CreateError>;
54+
async fn replace_body_fat(&self, body_fat: BodyFat) -> Result<BodyFat, UpdateError>;
55+
async fn delete_body_fat(&self, date: NaiveDate) -> Result<NaiveDate, DeleteError>;
5556
}
5657

5758
#[allow(async_fn_in_trait)]
5859
pub trait PeriodRepository {
59-
async fn sync_period(&self) -> Result<Vec<Period>, String>;
60-
async fn read_period(&self) -> Result<Vec<Period>, String>;
61-
async fn create_period(&self, period: Period) -> Result<Period, String>;
62-
async fn replace_period(&self, period: Period) -> Result<Period, String>;
63-
async fn delete_period(&self, date: NaiveDate) -> Result<NaiveDate, String>;
60+
async fn sync_period(&self) -> Result<Vec<Period>, SyncError>;
61+
async fn read_period(&self) -> Result<Vec<Period>, ReadError>;
62+
async fn create_period(&self, period: Period) -> Result<Period, CreateError>;
63+
async fn replace_period(&self, period: Period) -> Result<Period, UpdateError>;
64+
async fn delete_period(&self, date: NaiveDate) -> Result<NaiveDate, DeleteError>;
6465
}
6566

6667
#[allow(async_fn_in_trait)]
6768
pub trait ExerciseRepository {
68-
async fn sync_exercises(&self) -> Result<Vec<Exercise>, String>;
69-
async fn read_exercises(&self) -> Result<Vec<Exercise>, String>;
69+
async fn sync_exercises(&self) -> Result<Vec<Exercise>, SyncError>;
70+
async fn read_exercises(&self) -> Result<Vec<Exercise>, ReadError>;
7071
async fn create_exercise(
7172
&self,
7273
name: Name,
7374
muscles: Vec<ExerciseMuscle>,
74-
) -> Result<Exercise, String>;
75-
async fn replace_exercise(&self, exercise: Exercise) -> Result<Exercise, String>;
76-
async fn delete_exercise(&self, id: ExerciseID) -> Result<ExerciseID, String>;
75+
) -> Result<Exercise, CreateError>;
76+
async fn replace_exercise(&self, exercise: Exercise) -> Result<Exercise, UpdateError>;
77+
async fn delete_exercise(&self, id: ExerciseID) -> Result<ExerciseID, DeleteError>;
7778
}
7879

7980
#[allow(async_fn_in_trait)]
8081
pub trait RoutineRepository {
81-
async fn sync_routines(&self) -> Result<Vec<Routine>, String>;
82-
async fn read_routines(&self) -> Result<Vec<Routine>, String>;
82+
async fn sync_routines(&self) -> Result<Vec<Routine>, SyncError>;
83+
async fn read_routines(&self) -> Result<Vec<Routine>, ReadError>;
8384
async fn create_routine(
8485
&self,
8586
name: Name,
8687
sections: Vec<RoutinePart>,
87-
) -> Result<Routine, String>;
88+
) -> Result<Routine, CreateError>;
8889
async fn modify_routine(
8990
&self,
9091
id: RoutineID,
9192
name: Option<Name>,
9293
archived: Option<bool>,
9394
sections: Option<Vec<RoutinePart>>,
94-
) -> Result<Routine, String>;
95-
async fn delete_routine(&self, id: RoutineID) -> Result<RoutineID, String>;
95+
) -> Result<Routine, UpdateError>;
96+
async fn delete_routine(&self, id: RoutineID) -> Result<RoutineID, DeleteError>;
9697
}
9798

9899
#[allow(async_fn_in_trait)]
99100
pub trait TrainingSessionRepository {
100-
async fn sync_training_sessions(&self) -> Result<Vec<TrainingSession>, String>;
101-
async fn read_training_sessions(&self) -> Result<Vec<TrainingSession>, String>;
101+
async fn sync_training_sessions(&self) -> Result<Vec<TrainingSession>, SyncError>;
102+
async fn read_training_sessions(&self) -> Result<Vec<TrainingSession>, ReadError>;
102103
async fn create_training_session(
103104
&self,
104105
routine_id: RoutineID,
105106
date: NaiveDate,
106107
notes: String,
107108
elements: Vec<TrainingSessionElement>,
108-
) -> Result<TrainingSession, String>;
109+
) -> Result<TrainingSession, CreateError>;
109110
async fn modify_training_session(
110111
&self,
111112
id: TrainingSessionID,
112113
notes: Option<String>,
113114
elements: Option<Vec<TrainingSessionElement>>,
114-
) -> Result<TrainingSession, String>;
115+
) -> Result<TrainingSession, UpdateError>;
115116
async fn delete_training_session(
116117
&self,
117118
id: TrainingSessionID,
118-
) -> Result<TrainingSessionID, String>;
119+
) -> Result<TrainingSessionID, DeleteError>;
120+
}
121+
122+
#[derive(Error, Debug)]
123+
pub enum StorageError {
124+
#[error("no connection")]
125+
NoConnection,
126+
#[error("no session")]
127+
NoSession,
128+
#[error(transparent)]
129+
Other(#[from] Box<dyn std::error::Error>),
130+
}
131+
132+
#[derive(Error, Debug)]
133+
pub enum SyncError {
134+
#[error(transparent)]
135+
Storage(#[from] StorageError),
136+
#[error(transparent)]
137+
Other(#[from] Box<dyn std::error::Error>),
138+
}
139+
140+
impl From<ReadError> for SyncError {
141+
fn from(value: ReadError) -> Self {
142+
match value {
143+
ReadError::Storage(storage) => SyncError::Storage(storage),
144+
ReadError::Other(other) => SyncError::Other(other),
145+
}
146+
}
147+
}
148+
149+
#[derive(Error, Debug)]
150+
pub enum ReadError {
151+
#[error(transparent)]
152+
Storage(#[from] StorageError),
153+
#[error(transparent)]
154+
Other(#[from] Box<dyn std::error::Error>),
155+
}
156+
157+
#[derive(Error, Debug)]
158+
pub enum CreateError {
159+
#[error("conflict")]
160+
Conflict,
161+
#[error(transparent)]
162+
Storage(#[from] StorageError),
163+
#[error(transparent)]
164+
Other(#[from] Box<dyn std::error::Error>),
165+
}
166+
167+
impl From<UpdateError> for CreateError {
168+
fn from(value: UpdateError) -> Self {
169+
match value {
170+
UpdateError::Conflict => CreateError::Conflict,
171+
UpdateError::Storage(storage) => CreateError::Storage(storage),
172+
UpdateError::Other(other) => CreateError::Other(other),
173+
}
174+
}
175+
}
176+
177+
#[derive(Error, Debug)]
178+
pub enum UpdateError {
179+
#[error("conflict")]
180+
Conflict,
181+
#[error(transparent)]
182+
Storage(#[from] StorageError),
183+
#[error(transparent)]
184+
Other(#[from] Box<dyn std::error::Error>),
185+
}
186+
187+
#[derive(Error, Debug)]
188+
pub enum DeleteError {
189+
#[error(transparent)]
190+
Storage(#[from] StorageError),
191+
#[error(transparent)]
192+
Other(#[from] Box<dyn std::error::Error>),
119193
}
120194

121195
#[derive(Debug, Clone, PartialEq, Eq)]

crates/storage/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ publish = false
99
valens-domain = { path = "../domain" }
1010
valens-web-app = { path = "../web-app" }
1111

12-
anyhow = "1.0.97"
12+
anyhow = { workspace = true }
1313
chrono = { workspace = true, default-features = false, features = ["serde"] }
1414
gloo-net = { version = "0.6.0", default-features = false, features = ["http", "json"] }
1515
gloo-storage = "0.3.0"

0 commit comments

Comments
 (0)