110 lines
2.4 KiB
Rust
Raw Normal View History

2022-01-03 12:20:06 +08:00
use std::{fmt, fmt::Debug, str::Utf8Error};
2021-07-31 20:53:45 +08:00
2022-01-03 12:20:06 +08:00
#[derive(thiserror::Error, Clone, Debug)]
2021-08-05 22:52:19 +08:00
pub struct OTError {
pub code: OTErrorCode,
pub msg: String,
}
2021-12-07 22:32:34 +08:00
macro_rules! static_ot_error {
($name:ident, $code:expr) => {
#[allow(non_snake_case, missing_docs)]
2022-01-23 12:14:00 +08:00
pub fn $name() -> OTError {
$code.into()
}
2021-12-07 22:32:34 +08:00
};
}
impl std::convert::From<OTErrorCode> for OTError {
fn from(code: OTErrorCode) -> Self {
OTError {
code: code.clone(),
msg: format!("{:?}", code),
}
}
}
2021-08-05 22:52:19 +08:00
impl OTError {
pub fn new(code: OTErrorCode, msg: &str) -> OTError {
Self {
code,
msg: msg.to_owned(),
}
}
2021-12-07 22:32:34 +08:00
pub fn context<T: Debug>(mut self, error: T) -> Self {
self.msg = format!("{:?}", error);
self
}
static_ot_error!(duplicate_revision, OTErrorCode::DuplicatedRevision);
2021-12-08 17:33:22 +08:00
static_ot_error!(revision_id_conflict, OTErrorCode::RevisionIDConflict);
2021-12-11 17:48:39 +08:00
static_ot_error!(internal, OTErrorCode::Internal);
2021-08-05 22:52:19 +08:00
}
2021-07-31 20:53:45 +08:00
impl fmt::Display for OTError {
2022-01-23 12:14:00 +08:00
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "incompatible lengths")
}
2021-07-31 20:53:45 +08:00
}
2021-08-18 16:04:22 +08:00
impl std::convert::From<serde_json::Error> for OTError {
2022-01-23 12:14:00 +08:00
fn from(error: serde_json::Error) -> Self {
ErrorBuilder::new(OTErrorCode::SerdeError).error(error).build()
}
2021-08-18 16:04:22 +08:00
}
2021-09-14 16:22:44 +08:00
impl std::convert::From<Utf8Error> for OTError {
2022-01-23 12:14:00 +08:00
fn from(error: Utf8Error) -> Self {
ErrorBuilder::new(OTErrorCode::SerdeError).error(error).build()
}
2021-09-14 16:22:44 +08:00
}
2022-08-22 16:46:24 +08:00
#[derive(Debug, Clone, Eq, PartialEq)]
2021-08-05 22:52:19 +08:00
pub enum OTErrorCode {
IncompatibleLength,
2021-08-11 23:34:35 +08:00
ApplyInsertFail,
2021-08-15 00:05:18 +08:00
ApplyDeleteFail,
2021-08-11 23:34:35 +08:00
ApplyFormatFail,
2021-08-14 16:44:39 +08:00
ComposeOperationFail,
2021-08-15 00:05:18 +08:00
IntervalOutOfBound,
2021-08-05 22:52:19 +08:00
UndoFail,
RedoFail,
2021-08-18 16:04:22 +08:00
SerdeError,
2021-12-07 22:32:34 +08:00
DuplicatedRevision,
2021-12-08 17:33:22 +08:00
RevisionIDConflict,
2021-12-11 17:48:39 +08:00
Internal,
2022-08-22 16:46:24 +08:00
PathNotFound,
2021-08-05 22:52:19 +08:00
}
pub struct ErrorBuilder {
pub code: OTErrorCode,
pub msg: Option<String>,
}
impl ErrorBuilder {
2022-01-23 12:14:00 +08:00
pub fn new(code: OTErrorCode) -> Self {
ErrorBuilder { code, msg: None }
}
2021-08-05 22:52:19 +08:00
pub fn msg<T>(mut self, msg: T) -> Self
where
T: Into<String>,
{
self.msg = Some(msg.into());
self
}
pub fn error<T>(mut self, msg: T) -> Self
where
T: std::fmt::Debug,
{
self.msg = Some(format!("{:?}", msg));
self
}
2022-01-23 12:14:00 +08:00
pub fn build(mut self) -> OTError {
OTError::new(self.code, &self.msg.take().unwrap_or_else(|| "".to_owned()))
}
2021-08-05 22:52:19 +08:00
}