2020-01-10 12:00:48 +01:00
|
|
|
// Copyright: Ankitects Pty Ltd and contributors
|
|
|
|
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
|
|
|
|
2020-04-01 09:59:08 +02:00
|
|
|
use crate::i18n::{tr_strs, I18n, TR};
|
2019-12-24 04:33:44 +01:00
|
|
|
pub use failure::{Error, Fail};
|
2020-02-04 10:39:31 +01:00
|
|
|
use reqwest::StatusCode;
|
2020-01-28 12:45:26 +01:00
|
|
|
use std::io;
|
2019-12-24 04:33:44 +01:00
|
|
|
|
|
|
|
pub type Result<T> = std::result::Result<T, AnkiError>;
|
|
|
|
|
2020-04-17 06:36:45 +02:00
|
|
|
#[derive(Debug, Fail, PartialEq)]
|
2019-12-24 04:33:44 +01:00
|
|
|
pub enum AnkiError {
|
|
|
|
#[fail(display = "invalid input: {}", info)]
|
|
|
|
InvalidInput { info: String },
|
2019-12-24 05:05:15 +01:00
|
|
|
|
|
|
|
#[fail(display = "invalid card template: {}", info)]
|
2020-02-14 09:10:57 +01:00
|
|
|
TemplateError { info: String },
|
2020-01-28 12:45:26 +01:00
|
|
|
|
|
|
|
#[fail(display = "I/O error: {}", info)]
|
|
|
|
IOError { info: String },
|
2020-01-29 04:17:07 +01:00
|
|
|
|
|
|
|
#[fail(display = "DB error: {}", info)]
|
2020-03-03 06:04:03 +01:00
|
|
|
DBError { info: String, kind: DBErrorKind },
|
2020-02-01 10:05:22 +01:00
|
|
|
|
2020-02-04 10:39:31 +01:00
|
|
|
#[fail(display = "Network error: {:?} {}", kind, info)]
|
|
|
|
NetworkError {
|
|
|
|
info: String,
|
|
|
|
kind: NetworkErrorKind,
|
|
|
|
},
|
2020-02-01 10:05:22 +01:00
|
|
|
|
2020-02-04 10:39:31 +01:00
|
|
|
#[fail(display = "Sync error: {:?}, {}", kind, info)]
|
|
|
|
SyncError { info: String, kind: SyncErrorKind },
|
2020-02-02 12:02:20 +01:00
|
|
|
|
2020-04-06 08:29:31 +02:00
|
|
|
#[fail(display = "JSON encode/decode error: {}", info)]
|
|
|
|
JSONError { info: String },
|
|
|
|
|
2020-04-12 09:45:21 +02:00
|
|
|
#[fail(display = "Protobuf encode/decode error: {}", info)]
|
|
|
|
ProtoError { info: String },
|
|
|
|
|
2020-02-02 12:02:20 +01:00
|
|
|
#[fail(display = "The user interrupted the operation.")]
|
|
|
|
Interrupted,
|
2020-03-13 11:49:01 +01:00
|
|
|
|
|
|
|
#[fail(display = "Operation requires an open collection.")]
|
|
|
|
CollectionNotOpen,
|
2020-03-14 00:08:02 +01:00
|
|
|
|
|
|
|
#[fail(display = "Close the existing collection first.")]
|
|
|
|
CollectionAlreadyOpen,
|
2020-04-23 02:10:28 +02:00
|
|
|
|
|
|
|
#[fail(display = "A requested item was not found.")]
|
|
|
|
NotFound,
|
2020-05-02 04:41:53 +02:00
|
|
|
|
|
|
|
#[fail(display = "The provided item already exists.")]
|
|
|
|
Existing,
|
|
|
|
|
|
|
|
#[fail(display = "Unable to place item in/under a filtered deck.")]
|
|
|
|
DeckIsFiltered,
|
2020-06-09 03:29:40 +02:00
|
|
|
|
|
|
|
#[fail(display = "Invalid search.")]
|
|
|
|
SearchError(Option<String>),
|
2019-12-24 04:33:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// error helpers
|
|
|
|
impl AnkiError {
|
|
|
|
pub(crate) fn invalid_input<S: Into<String>>(s: S) -> AnkiError {
|
|
|
|
AnkiError::InvalidInput { info: s.into() }
|
|
|
|
}
|
2020-02-04 10:39:31 +01:00
|
|
|
|
|
|
|
pub(crate) fn server_message<S: Into<String>>(msg: S) -> AnkiError {
|
|
|
|
AnkiError::SyncError {
|
|
|
|
info: msg.into(),
|
|
|
|
kind: SyncErrorKind::ServerMessage,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn sync_misc<S: Into<String>>(msg: S) -> AnkiError {
|
|
|
|
AnkiError::SyncError {
|
|
|
|
info: msg.into(),
|
|
|
|
kind: SyncErrorKind::Other,
|
|
|
|
}
|
|
|
|
}
|
2020-02-17 02:38:22 +01:00
|
|
|
|
|
|
|
pub fn localized_description(&self, i18n: &I18n) -> String {
|
|
|
|
match self {
|
2020-02-23 03:21:12 +01:00
|
|
|
AnkiError::SyncError { info, kind } => match kind {
|
|
|
|
SyncErrorKind::ServerMessage => info.into(),
|
|
|
|
SyncErrorKind::Other => info.into(),
|
2020-04-01 09:59:08 +02:00
|
|
|
SyncErrorKind::Conflict => i18n.tr(TR::SyncConflict),
|
|
|
|
SyncErrorKind::ServerError => i18n.tr(TR::SyncServerError),
|
|
|
|
SyncErrorKind::ClientTooOld => i18n.tr(TR::SyncClientTooOld),
|
|
|
|
SyncErrorKind::AuthFailed => i18n.tr(TR::SyncWrongPass),
|
|
|
|
SyncErrorKind::ResyncRequired => i18n.tr(TR::SyncResyncRequired),
|
2020-05-28 11:49:44 +02:00
|
|
|
// fixme: i18n
|
|
|
|
SyncErrorKind::ClockIncorrect => "Please check your clock.".into(),
|
|
|
|
SyncErrorKind::DatabaseCheckRequired => "Please check the database.".into(),
|
2020-02-17 02:38:22 +01:00
|
|
|
}
|
2020-02-23 03:21:12 +01:00
|
|
|
.into(),
|
2020-02-27 01:46:24 +01:00
|
|
|
AnkiError::NetworkError { kind, info } => {
|
|
|
|
let summary = match kind {
|
2020-04-01 09:59:08 +02:00
|
|
|
NetworkErrorKind::Offline => i18n.tr(TR::NetworkOffline),
|
|
|
|
NetworkErrorKind::Timeout => i18n.tr(TR::NetworkTimeout),
|
|
|
|
NetworkErrorKind::ProxyAuth => i18n.tr(TR::NetworkProxyAuth),
|
|
|
|
NetworkErrorKind::Other => i18n.tr(TR::NetworkOther),
|
2020-02-27 01:46:24 +01:00
|
|
|
};
|
2020-04-01 09:59:08 +02:00
|
|
|
let details = i18n.trn(TR::NetworkDetails, tr_strs!["details"=>info]);
|
2020-02-27 02:22:48 +01:00
|
|
|
format!("{}\n\n{}", summary, details)
|
2020-02-17 02:38:22 +01:00
|
|
|
}
|
2020-02-27 02:07:18 +01:00
|
|
|
AnkiError::TemplateError { info } => {
|
|
|
|
// already localized
|
|
|
|
info.into()
|
|
|
|
}
|
2020-05-10 05:50:04 +02:00
|
|
|
AnkiError::DBError { info, kind } => match kind {
|
|
|
|
DBErrorKind::Corrupt => info.clone(),
|
2020-07-01 03:19:06 +02:00
|
|
|
DBErrorKind::Locked => "Anki already open, or media currently syncing.".into(),
|
2020-05-10 05:50:04 +02:00
|
|
|
_ => format!("{:?}", self),
|
|
|
|
},
|
2020-06-09 03:29:40 +02:00
|
|
|
AnkiError::SearchError(details) => {
|
|
|
|
if let Some(details) = details {
|
|
|
|
details.to_owned()
|
|
|
|
} else {
|
|
|
|
i18n.tr(TR::SearchInvalid).to_string()
|
|
|
|
}
|
|
|
|
}
|
2020-02-27 02:07:18 +01:00
|
|
|
_ => format!("{:?}", self),
|
2020-02-17 02:38:22 +01:00
|
|
|
}
|
|
|
|
}
|
2019-12-24 04:33:44 +01:00
|
|
|
}
|
2020-01-16 07:37:44 +01:00
|
|
|
|
2020-01-16 08:23:25 +01:00
|
|
|
#[derive(Debug, PartialEq)]
|
2020-01-16 07:37:44 +01:00
|
|
|
pub enum TemplateError {
|
|
|
|
NoClosingBrackets(String),
|
|
|
|
ConditionalNotClosed(String),
|
2020-02-08 01:32:34 +01:00
|
|
|
ConditionalNotOpen {
|
|
|
|
closed: String,
|
|
|
|
currently_open: Option<String>,
|
|
|
|
},
|
|
|
|
FieldNotFound {
|
|
|
|
filters: String,
|
|
|
|
field: String,
|
|
|
|
},
|
2020-01-16 07:37:44 +01:00
|
|
|
}
|
2020-01-28 12:45:26 +01:00
|
|
|
|
|
|
|
impl From<io::Error> for AnkiError {
|
|
|
|
fn from(err: io::Error) -> Self {
|
|
|
|
AnkiError::IOError {
|
|
|
|
info: format!("{:?}", err),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-01-29 04:17:07 +01:00
|
|
|
|
|
|
|
impl From<rusqlite::Error> for AnkiError {
|
|
|
|
fn from(err: rusqlite::Error) -> Self {
|
2020-07-01 03:19:06 +02:00
|
|
|
if let rusqlite::Error::SqliteFailure(error, Some(reason)) = &err {
|
|
|
|
if error.code == rusqlite::ErrorCode::DatabaseBusy {
|
|
|
|
return AnkiError::DBError {
|
|
|
|
info: "".to_string(),
|
|
|
|
kind: DBErrorKind::Locked,
|
|
|
|
};
|
|
|
|
}
|
2020-06-09 03:29:40 +02:00
|
|
|
if reason.contains("regex parse error") {
|
|
|
|
return AnkiError::SearchError(Some(reason.to_owned()));
|
|
|
|
}
|
|
|
|
}
|
2020-01-29 04:17:07 +01:00
|
|
|
AnkiError::DBError {
|
|
|
|
info: format!("{:?}", err),
|
2020-03-03 06:04:03 +01:00
|
|
|
kind: DBErrorKind::Other,
|
2020-01-29 04:17:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<rusqlite::types::FromSqlError> for AnkiError {
|
|
|
|
fn from(err: rusqlite::types::FromSqlError) -> Self {
|
|
|
|
AnkiError::DBError {
|
|
|
|
info: format!("{:?}", err),
|
2020-03-03 06:04:03 +01:00
|
|
|
kind: DBErrorKind::Other,
|
2020-01-29 04:17:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-02-01 10:05:22 +01:00
|
|
|
|
2020-02-04 10:39:31 +01:00
|
|
|
#[derive(Debug, PartialEq)]
|
|
|
|
pub enum NetworkErrorKind {
|
|
|
|
Offline,
|
|
|
|
Timeout,
|
|
|
|
ProxyAuth,
|
|
|
|
Other,
|
|
|
|
}
|
|
|
|
|
2020-02-01 10:05:22 +01:00
|
|
|
impl From<reqwest::Error> for AnkiError {
|
|
|
|
fn from(err: reqwest::Error) -> Self {
|
2020-02-04 00:07:15 +01:00
|
|
|
let url = err.url().map(|url| url.as_str()).unwrap_or("");
|
|
|
|
let str_err = format!("{}", err);
|
|
|
|
// strip url from error to avoid exposing keys
|
2020-02-04 10:39:31 +01:00
|
|
|
let info = str_err.replace(url, "");
|
|
|
|
|
|
|
|
if err.is_timeout() {
|
|
|
|
AnkiError::NetworkError {
|
|
|
|
info,
|
|
|
|
kind: NetworkErrorKind::Timeout,
|
|
|
|
}
|
|
|
|
} else if err.is_status() {
|
|
|
|
error_for_status_code(info, err.status().unwrap())
|
|
|
|
} else {
|
|
|
|
guess_reqwest_error(info)
|
|
|
|
}
|
2020-02-01 10:05:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-04 10:39:31 +01:00
|
|
|
#[derive(Debug, PartialEq)]
|
|
|
|
pub enum SyncErrorKind {
|
|
|
|
Conflict,
|
|
|
|
ServerError,
|
|
|
|
ClientTooOld,
|
|
|
|
AuthFailed,
|
|
|
|
ServerMessage,
|
2020-05-28 11:49:44 +02:00
|
|
|
ClockIncorrect,
|
2020-02-04 10:39:31 +01:00
|
|
|
Other,
|
2020-02-05 04:35:40 +01:00
|
|
|
ResyncRequired,
|
2020-05-28 11:49:44 +02:00
|
|
|
DatabaseCheckRequired,
|
2020-02-04 10:39:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn error_for_status_code(info: String, code: StatusCode) -> AnkiError {
|
|
|
|
use reqwest::StatusCode as S;
|
|
|
|
match code {
|
|
|
|
S::PROXY_AUTHENTICATION_REQUIRED => AnkiError::NetworkError {
|
|
|
|
info,
|
|
|
|
kind: NetworkErrorKind::ProxyAuth,
|
|
|
|
},
|
|
|
|
S::CONFLICT => AnkiError::SyncError {
|
|
|
|
info,
|
|
|
|
kind: SyncErrorKind::Conflict,
|
|
|
|
},
|
|
|
|
S::FORBIDDEN => AnkiError::SyncError {
|
|
|
|
info,
|
|
|
|
kind: SyncErrorKind::AuthFailed,
|
|
|
|
},
|
|
|
|
S::NOT_IMPLEMENTED => AnkiError::SyncError {
|
|
|
|
info,
|
|
|
|
kind: SyncErrorKind::ClientTooOld,
|
|
|
|
},
|
|
|
|
S::INTERNAL_SERVER_ERROR | S::BAD_GATEWAY | S::GATEWAY_TIMEOUT | S::SERVICE_UNAVAILABLE => {
|
|
|
|
AnkiError::SyncError {
|
|
|
|
info,
|
|
|
|
kind: SyncErrorKind::ServerError,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => AnkiError::NetworkError {
|
|
|
|
info,
|
|
|
|
kind: NetworkErrorKind::Other,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn guess_reqwest_error(info: String) -> AnkiError {
|
2020-06-11 05:26:14 +02:00
|
|
|
if info.contains("dns error: cancelled") {
|
|
|
|
return AnkiError::Interrupted;
|
|
|
|
}
|
2020-02-04 10:39:31 +01:00
|
|
|
let kind = if info.contains("unreachable") || info.contains("dns") {
|
|
|
|
NetworkErrorKind::Offline
|
|
|
|
} else {
|
|
|
|
NetworkErrorKind::Other
|
|
|
|
};
|
|
|
|
AnkiError::NetworkError { info, kind }
|
|
|
|
}
|
|
|
|
|
2020-02-01 10:05:22 +01:00
|
|
|
impl From<zip::result::ZipError> for AnkiError {
|
|
|
|
fn from(err: zip::result::ZipError) -> Self {
|
2020-02-04 10:39:31 +01:00
|
|
|
AnkiError::sync_misc(err.to_string())
|
2020-02-01 10:05:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<serde_json::Error> for AnkiError {
|
|
|
|
fn from(err: serde_json::Error) -> Self {
|
2020-04-06 08:29:31 +02:00
|
|
|
AnkiError::JSONError {
|
|
|
|
info: err.to_string(),
|
|
|
|
}
|
2020-02-01 10:05:22 +01:00
|
|
|
}
|
|
|
|
}
|
2020-03-03 06:04:03 +01:00
|
|
|
|
2020-04-12 09:45:21 +02:00
|
|
|
impl From<prost::EncodeError> for AnkiError {
|
|
|
|
fn from(err: prost::EncodeError) -> Self {
|
|
|
|
AnkiError::ProtoError {
|
|
|
|
info: err.to_string(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<prost::DecodeError> for AnkiError {
|
|
|
|
fn from(err: prost::DecodeError) -> Self {
|
|
|
|
AnkiError::ProtoError {
|
|
|
|
info: err.to_string(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-03 06:04:03 +01:00
|
|
|
#[derive(Debug, PartialEq)]
|
|
|
|
pub enum DBErrorKind {
|
|
|
|
FileTooNew,
|
|
|
|
FileTooOld,
|
|
|
|
MissingEntity,
|
2020-05-10 05:50:04 +02:00
|
|
|
Corrupt,
|
2020-07-01 03:19:06 +02:00
|
|
|
Locked,
|
2020-03-03 06:04:03 +01:00
|
|
|
Other,
|
|
|
|
}
|