etebase/
error.rs

1// SPDX-FileCopyrightText: © 2020 Etebase Authors
2// SPDX-License-Identifier: LGPL-2.1-only
3
4use std::error;
5use std::fmt;
6
7/// A short-hand version of a [`std::result::Result`] that always returns an Etebase [`Error`].
8pub type Result<T> = std::result::Result<T, Error>;
9
10/// The error type returned from the Etebase API
11#[derive(Debug, Clone, PartialEq, Eq)]
12pub enum Error {
13    /// A generic error
14    Generic(String),
15    /// An error with parsing the a URL (e.g. from the server URL)
16    UrlParse(String),
17    /// An error related to msgpack serialization and de-serialization
18    MsgPack(String),
19    /// A programming error that indicates the developers are using the API wrong
20    ProgrammingError(&'static str),
21    /// An attempt to fetch the content of an item that doesn't have the content yet
22    MissingContent(&'static str),
23    /// An issue with the padding of the encrypted content
24    Padding(&'static str),
25    /// An issue with the Base64 decoding
26    Base64(&'static str),
27    /// An issue with the encryption
28    Encryption(&'static str),
29    /// An authorization issue from the server
30    Unauthorized(String),
31    /// A conflict issue returned from the server, e.g. if a transaction failed
32    Conflict(String),
33    /// The operation was not allowed due to permissions
34    PermissionDenied(String),
35    /// The requested resource was not found
36    NotFound(String),
37
38    /// There was an issue with the connection (e.g. DNS lookup)
39    Connection(String),
40    /// There was an temporary server error (e.g. maintenance, or gateway issues)
41    TemporaryServerError(String),
42    /// There was a server error when processing the request (usually a bug in the server)
43    ServerError(String),
44    /// A generic error with the server request
45    Http(String),
46}
47
48impl fmt::Display for Error {
49    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
50        match self {
51            Error::Generic(s) => s.fmt(f),
52            Error::UrlParse(s) => s.fmt(f),
53            Error::MsgPack(s) => s.fmt(f),
54            Error::ProgrammingError(s) => s.fmt(f),
55            Error::MissingContent(s) => s.fmt(f),
56            Error::Padding(s) => s.fmt(f),
57            Error::Base64(s) => s.fmt(f),
58            Error::Encryption(s) => s.fmt(f),
59            Error::PermissionDenied(s) => s.fmt(f),
60            Error::NotFound(s) => s.fmt(f),
61            Error::Unauthorized(s) => s.fmt(f),
62            Error::Conflict(s) => s.fmt(f),
63
64            Error::Connection(s) => s.fmt(f),
65            Error::TemporaryServerError(s) => s.fmt(f),
66            Error::ServerError(s) => s.fmt(f),
67            Error::Http(s) => s.fmt(f),
68        }
69    }
70}
71
72impl From<Error> for String {
73    fn from(err: Error) -> String {
74        err.to_string()
75    }
76}
77
78impl error::Error for Error {
79    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
80        None
81    }
82}
83
84impl From<String> for Error {
85    fn from(err: String) -> Error {
86        Error::Generic(err)
87    }
88}
89
90impl From<url::ParseError> for Error {
91    fn from(err: url::ParseError) -> Error {
92        Error::UrlParse(err.to_string())
93    }
94}
95
96impl From<std::ffi::NulError> for Error {
97    fn from(err: std::ffi::NulError) -> Error {
98        Error::Generic(err.to_string())
99    }
100}
101
102impl From<std::io::Error> for Error {
103    fn from(err: std::io::Error) -> Error {
104        Error::UrlParse(err.to_string())
105    }
106}
107
108impl From<rmp_serde::encode::Error> for Error {
109    fn from(err: rmp_serde::encode::Error) -> Error {
110        Error::MsgPack(err.to_string())
111    }
112}
113
114impl From<rmp_serde::decode::Error> for Error {
115    fn from(err: rmp_serde::decode::Error) -> Error {
116        Error::MsgPack(err.to_string())
117    }
118}