Skip to content

Commit 19f34b4

Browse files
committed
refactor error
1 parent 91deb07 commit 19f34b4

File tree

4 files changed

+32
-29
lines changed

4 files changed

+32
-29
lines changed

optd-cost-model/src/lib.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ use common::{
22
nodes::{ArcPredicateNode, PhysicalNodeType},
33
types::{AttrId, EpochId, ExprId, GroupId, TableId},
44
};
5-
use optd_persistent::cost_model::interface::{Stat, StatType};
5+
use optd_persistent::{
6+
cost_model::interface::{Stat, StatType},
7+
BackendError,
8+
};
69

710
pub mod common;
811
pub mod cost;
@@ -32,10 +35,19 @@ pub struct EstimatedStatistic(pub u64);
3235

3336
pub type CostModelResult<T> = Result<T, CostModelError>;
3437

38+
#[derive(Debug)]
39+
pub enum SemanticError {
40+
// TODO: Add more error types
41+
UnknownStatisticType,
42+
VersionedStatisticNotFound,
43+
AttributeNotFound,
44+
}
45+
3546
#[derive(Debug)]
3647
pub enum CostModelError {
3748
// TODO: Add more error types
38-
ORMError,
49+
ORMError(BackendError),
50+
SemanticError(SemanticError),
3951
}
4052

4153
pub trait CostModel: 'static + Send + Sync {

optd-persistent/src/cost_model/interface.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#![allow(dead_code, unused_imports)]
22

3+
use crate::entities::attribute::Model as AttributeEntity;
34
use crate::entities::cascades_group;
4-
use crate::entities::event::Model as event_model;
55
use crate::entities::logical_expression;
66
use crate::entities::physical_expression;
77
use crate::StorageResult;
@@ -58,7 +58,7 @@ pub enum EpochOption {
5858
}
5959

6060
/// TODO: documentation
61-
#[derive(Clone)]
61+
#[derive(Clone, Debug)]
6262
pub struct Stat {
6363
pub stat_type: i32,
6464
pub stat_value: Json,

optd-persistent/src/cost_model/orm.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
#![allow(dead_code, unused_imports, unused_variables)]
22

3-
use std::ptr::null;
4-
53
use crate::cost_model::interface::Cost;
64
use crate::entities::{prelude::*, *};
7-
use crate::{BackendError, BackendManager, CostModelError, CostModelStorageLayer, StorageResult};
5+
use crate::{BackendError, BackendManager, CostModelStorageLayer, StorageResult};
86
use sea_orm::prelude::{Expr, Json};
97
use sea_orm::sea_query::Query;
108
use sea_orm::{sqlx::types::chrono::Utc, EntityTrait};
@@ -246,10 +244,9 @@ impl CostModelStorageLayer for BackendManager {
246244
match res {
247245
Ok(insert_res) => insert_res.last_insert_id,
248246
Err(_) => {
249-
return Err(BackendError::Database(DbErr::Exec(
250-
RuntimeErr::Internal(
251-
"Failed to insert into statistic table".to_string(),
252-
),
247+
return Err(BackendError::BackendError(format!(
248+
"failed to insert statistic {:?} into statistic table",
249+
stat
253250
)))
254251
}
255252
}
@@ -486,8 +483,9 @@ impl CostModelStorageLayer for BackendManager {
486483
.one(&self.db)
487484
.await?;
488485
if expr_exists.is_none() {
489-
return Err(BackendError::Database(DbErr::RecordNotFound(
490-
"ExprId not found in PhysicalExpression table".to_string(),
486+
return Err(BackendError::BackendError(format!(
487+
"physical expression id {} not found when storing cost",
488+
physical_expression_id
491489
)));
492490
}
493491

@@ -498,8 +496,9 @@ impl CostModelStorageLayer for BackendManager {
498496
.await
499497
.unwrap();
500498
if epoch_exists.is_none() {
501-
return Err(BackendError::Database(DbErr::RecordNotFound(
502-
"EpochId not found in Event table".to_string(),
499+
return Err(BackendError::BackendError(format!(
500+
"epoch id {} not found when storing cost",
501+
epoch_id
503502
)));
504503
}
505504

optd-persistent/src/lib.rs

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -41,29 +41,21 @@ fn get_sqlite_url(file: &str) -> String {
4141

4242
pub type StorageResult<T> = Result<T, BackendError>;
4343

44-
#[derive(Debug)]
45-
pub enum CostModelError {
46-
// TODO: Add more error types
47-
UnknownStatisticType,
48-
VersionedStatisticNotFound,
49-
}
50-
5144
#[derive(Debug)]
5245
pub enum BackendError {
53-
CostModel(CostModelError),
54-
Database(DbErr),
55-
// TODO: Add other variants as needed for different error types
46+
DatabaseError(DbErr),
47+
BackendError(String),
5648
}
5749

58-
impl From<CostModelError> for BackendError {
59-
fn from(value: CostModelError) -> Self {
60-
BackendError::CostModel(value)
50+
impl From<String> for BackendError {
51+
fn from(value: String) -> Self {
52+
BackendError::BackendError(value)
6153
}
6254
}
6355

6456
impl From<DbErr> for BackendError {
6557
fn from(value: DbErr) -> Self {
66-
BackendError::Database(value)
58+
BackendError::DatabaseError(value)
6759
}
6860
}
6961

0 commit comments

Comments
 (0)