Skip to content

Commit 31ce5b1

Browse files
committed
fix: refactor analyticsquery error handling
1 parent 181c2ae commit 31ce5b1

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-8
lines changed

primitives/src/analytics.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::DomainError;
12
use serde::{Deserialize, Serialize};
23

34
#[derive(Debug, Serialize, Deserialize)]
@@ -34,26 +35,26 @@ pub struct AnalyticsQuery {
3435
}
3536

3637
impl AnalyticsQuery {
37-
pub fn is_valid(&self) -> Result<(), String> {
38+
pub fn is_valid(&self) -> Result<(), DomainError> {
3839
let valid_event_types = ["IMPRESSION"];
3940
let valid_metric = ["eventPayouts", "eventCounts"];
4041
let valid_timeframe = ["year", "month", "week", "day", "hour"];
4142

4243
if !valid_event_types.contains(&self.event_type.as_str()) {
43-
Err(format!(
44+
Err(DomainError::InvalidArgument(format!(
4445
"invalid event_type, possible values are: {}",
4546
valid_event_types.join(" ,")
46-
))
47+
)))
4748
} else if !valid_metric.contains(&self.metric.as_str()) {
48-
Err(format!(
49+
Err(DomainError::InvalidArgument(format!(
4950
"invalid metric, possible values are: {}",
5051
valid_metric.join(" ,")
51-
))
52+
)))
5253
} else if !valid_timeframe.contains(&self.timeframe.as_str()) {
53-
Err(format!(
54+
Err(DomainError::InvalidArgument(format!(
5455
"invalid timeframe, possible values are: {}",
5556
valid_timeframe.join(" ,")
56-
))
57+
)))
5758
} else {
5859
Ok(())
5960
}

primitives/src/lib.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,10 @@ pub enum DomainError {
4343

4444
impl fmt::Display for DomainError {
4545
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
46-
write!(f, "Domain error",)
46+
match self {
47+
DomainError::InvalidArgument(err) => write!(f, "{}", err),
48+
DomainError::RuleViolation(err) => write!(f, "{}", err),
49+
}
4750
}
4851
}
4952

0 commit comments

Comments
 (0)