threema_gateway/
errors.rs

1//! Error types used in this library.
2
3use std::io::Error as IoError;
4
5use reqwest::Error as ReqwestError;
6use thiserror::Error;
7
8/// Errors when interacting with the API.
9#[derive(Debug, Error)]
10pub enum ApiError {
11    /// The recipient identity is invalid or the account is not set up for basic mode
12    #[error("bad sender or recipient")]
13    BadSenderOrRecipient,
14
15    /// API identity or secret is incorrect
16    #[error("bad credentials")]
17    BadCredentials,
18
19    /// No credits remain
20    #[error("no credits")]
21    NoCredits,
22
23    /// Target ID not found
24    #[error("target ID not found")]
25    IdNotFound,
26
27    /// Message is too long
28    #[error("message is too long")]
29    MessageTooLong,
30
31    /// Internal server error
32    #[error("internal server error")]
33    ServerError,
34
35    /// Wrong hash length
36    #[error("bad hash length")]
37    BadHashLength,
38
39    /// Bad blob
40    #[error("bad blob")]
41    BadBlob,
42
43    /// Invalid blob ID
44    #[error("bad blob ID")]
45    BadBlobId,
46
47    /// Invalid MAC
48    #[error("invalid MAC")]
49    InvalidMac,
50
51    /// Too many requests, rate limit reached
52    #[error("rate limit reached")]
53    RateLimitReached,
54
55    /// Error when sending request (via reqwest)
56    #[error("request error: {0}")]
57    RequestError(#[source] ReqwestError),
58
59    /// Error when building request URL (via reqwest)
60    #[error("request URL parse error: {0}")]
61    RequestUrlParseError(#[from] url::ParseError),
62
63    /// Error when reading response
64    #[error("I/O error: {0}")]
65    IoError(#[from] IoError),
66
67    /// Error while parsing response
68    #[error("parse error: {0}")]
69    ParseError(String),
70
71    /// Other
72    #[error("other: {0}")]
73    Other(String),
74}
75
76impl From<ReqwestError> for ApiError {
77    fn from(err: ReqwestError) -> Self {
78        // Strip URL, as it might contain sensitive content (the API secret)
79        Self::RequestError(err.without_url())
80    }
81}
82
83/// Either an [`ApiError`] or a cache error.
84#[derive(Debug, Error)]
85pub enum ApiOrCacheError<C: core::error::Error> {
86    /// API error, see contained [`ApiError`] value
87    #[error("api error: {0}")]
88    ApiError(ApiError),
89    /// Cache error, see contained value `C`
90    #[error("cache error: {0}")]
91    CacheError(C),
92}
93
94/// Crypto related errors.
95#[derive(Debug, PartialEq, Clone, Error)]
96pub enum CryptoError {
97    /// Bad key
98    #[error("bad key: {0}")]
99    BadKey(String),
100
101    /// Invalid nonce
102    #[error("bad nonce")]
103    BadNonce,
104
105    /// Invalid PKCS#7 padding
106    #[error("invalid padding")]
107    BadPadding,
108
109    /// Decryption failed
110    #[error("decryption failed")]
111    DecryptionFailed,
112
113    /// Encryption failed
114    #[error("encryption failed")]
115    EncryptionFailed,
116}
117
118/// Errors when interacting with the [`ApiBuilder`](../struct.ApiBuilder.html).
119#[derive(Debug, PartialEq, Clone, Error)]
120pub enum ApiBuilderError {
121    /// No private key has been set.
122    #[error("missing private key")]
123    MissingKey,
124
125    /// Invalid libsodium private key.
126    #[error("invalid libsodium private key: {0}")]
127    InvalidKey(String),
128}
129
130/// Errors when interacting with the [`FileMessageBuilder`](../struct.FileMessageBuilder.html).
131#[derive(Debug, PartialEq, Clone, Error)]
132pub enum FileMessageBuilderError {
133    /// Illegal combination of fields (e.g. setting the `animated` flag on a PDF file message).
134    #[error("illegal combination: {0}")]
135    IllegalCombination(&'static str),
136}