-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add entity_api implementation of find_by_id
Also starts to consider error handling patterns. Not sure if this is ultimately where we will go as the usage does not feel quite right but will continue to play with it.
- Loading branch information
1 parent
4e25708
commit 820a507
Showing
7 changed files
with
112 additions
and
11 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
use std::error::Error as StdError; | ||
use std::fmt; | ||
|
||
use sea_orm::error::DbErr; | ||
|
||
/// Errors while executing operations related to entities. | ||
/// The intent is to categorize errors into two major types: | ||
/// * Errors related to data. Ex DbError::RecordNotFound | ||
/// * Errors related to interactions with the database itself. Ex DbError::Conn | ||
#[derive(Debug)] | ||
pub struct Error { | ||
// Underlying error emitted from seaORM internals | ||
pub inner: DbErr, | ||
// Enum representing which category of error | ||
pub error_type: EntityApiErrorType, | ||
} | ||
|
||
#[derive(Debug)] | ||
pub enum EntityApiErrorType { | ||
// Record not found | ||
RecordNotFound, | ||
// Record not updated | ||
RecordNotUpdated, | ||
// Errors related to interactions with the database itself. Ex DbError::Conn | ||
SystemError, | ||
} | ||
|
||
impl fmt::Display for Error { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
write!(f, "Entity API Error: {:?}", self) | ||
} | ||
} | ||
|
||
impl StdError for Error {} | ||
|
||
impl From<DbErr> for Error { | ||
fn from(err: DbErr) -> Self { | ||
match err { | ||
DbErr::RecordNotFound(_) => Error { | ||
inner: err, | ||
error_type: EntityApiErrorType::RecordNotFound, | ||
}, | ||
DbErr::RecordNotUpdated => Error { | ||
inner: err, | ||
error_type: EntityApiErrorType::RecordNotUpdated, | ||
}, | ||
DbErr::ConnectionAcquire(_) => Error { | ||
inner: err, | ||
error_type: EntityApiErrorType::SystemError, | ||
}, | ||
DbErr::Conn(_) => Error { | ||
inner: err, | ||
error_type: EntityApiErrorType::SystemError, | ||
}, | ||
DbErr::Exec(_) => Error { | ||
inner: err, | ||
error_type: EntityApiErrorType::SystemError, | ||
}, | ||
_ => Error { | ||
inner: err, | ||
error_type: EntityApiErrorType::SystemError, | ||
}, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters