convert Json and Proto errors to tuple

This commit is contained in:
Damien Elmes 2021-04-01 17:45:12 +10:00
parent 55a6f10d24
commit 7a29d987c4
3 changed files with 10 additions and 16 deletions

View file

@ -25,8 +25,8 @@ pub(super) fn anki_error_to_proto_error(err: AnkiError, tr: &I18n) -> pb::Backen
AnkiError::Interrupted => V::Interrupted(pb::Empty {}), AnkiError::Interrupted => V::Interrupted(pb::Empty {}),
AnkiError::CollectionNotOpen => V::InvalidInput(pb::Empty {}), AnkiError::CollectionNotOpen => V::InvalidInput(pb::Empty {}),
AnkiError::CollectionAlreadyOpen => V::InvalidInput(pb::Empty {}), AnkiError::CollectionAlreadyOpen => V::InvalidInput(pb::Empty {}),
AnkiError::JsonError { info } => V::JsonError(info), AnkiError::JsonError(info) => V::JsonError(info),
AnkiError::ProtoError { info } => V::ProtoError(info), AnkiError::ProtoError(info) => V::ProtoError(info),
AnkiError::NotFound => V::NotFoundError(pb::Empty {}), AnkiError::NotFound => V::NotFoundError(pb::Empty {}),
AnkiError::Existing => V::Exists(pb::Empty {}), AnkiError::Existing => V::Exists(pb::Empty {}),
AnkiError::DeckIsFiltered => V::DeckIsFiltered(pb::Empty {}), AnkiError::DeckIsFiltered => V::DeckIsFiltered(pb::Empty {}),

View file

@ -20,14 +20,14 @@ pub type Result<T, E = AnkiError> = std::result::Result<T, E>;
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
pub enum AnkiError { pub enum AnkiError {
InvalidInput(String), InvalidInput(String),
TemplateError { info: String }, TemplateError(String),
TemplateSaveError { ordinal: usize }, TemplateSaveError { ordinal: usize },
IoError(String), IoError(String),
DbError(DbError), DbError(DbError),
NetworkError(NetworkError), NetworkError(NetworkError),
SyncError(SyncError), SyncError(SyncError),
JsonError { info: String }, JsonError(String),
ProtoError { info: String }, ProtoError(String),
ParseNumError, ParseNumError,
Interrupted, Interrupted,
CollectionNotOpen, CollectionNotOpen,
@ -56,7 +56,7 @@ impl AnkiError {
match self { match self {
AnkiError::SyncError(err) => err.localized_description(tr), AnkiError::SyncError(err) => err.localized_description(tr),
AnkiError::NetworkError(err) => err.localized_description(tr), AnkiError::NetworkError(err) => err.localized_description(tr),
AnkiError::TemplateError { info } => { AnkiError::TemplateError(info) => {
// already localized // already localized
info.into() info.into()
} }
@ -102,25 +102,19 @@ impl From<io::Error> for AnkiError {
impl From<serde_json::Error> for AnkiError { impl From<serde_json::Error> for AnkiError {
fn from(err: serde_json::Error) -> Self { fn from(err: serde_json::Error) -> Self {
AnkiError::JsonError { AnkiError::JsonError(err.to_string())
info: err.to_string(),
}
} }
} }
impl From<prost::EncodeError> for AnkiError { impl From<prost::EncodeError> for AnkiError {
fn from(err: prost::EncodeError) -> Self { fn from(err: prost::EncodeError) -> Self {
AnkiError::ProtoError { AnkiError::ProtoError(err.to_string())
info: err.to_string(),
}
} }
} }
impl From<prost::DecodeError> for AnkiError { impl From<prost::DecodeError> for AnkiError {
fn from(err: prost::DecodeError) -> Self { fn from(err: prost::DecodeError) -> Self {
AnkiError::ProtoError { AnkiError::ProtoError(err.to_string())
info: err.to_string(),
}
} }
} }

View file

@ -259,7 +259,7 @@ fn template_error_to_anki_error(err: TemplateError, q_side: bool, tr: &I18n) ->
header, details, TEMPLATE_ERROR_LINK, more_info header, details, TEMPLATE_ERROR_LINK, more_info
); );
AnkiError::TemplateError { info } AnkiError::TemplateError(info)
} }
fn localized_template_error(tr: &I18n, err: TemplateError) -> String { fn localized_template_error(tr: &I18n, err: TemplateError) -> String {