palpo_core/error/
kind.rs

1//! Errors that can be sent from the homeserver.
2
3use std::{collections::BTreeMap, fmt, time::Duration};
4
5use serde_json::Value as JsonValue;
6
7use crate::{PrivOwnedStr, RoomVersionId};
8
9/// An enum for the error kind.
10///
11/// Items may contain additional information.
12#[derive(Clone, Debug, PartialEq, Eq)]
13#[non_exhaustive]
14pub enum ErrorKind {
15    /// M_FORBIDDEN
16    Forbidden,
17
18    /// M_UNKNOWN_TOKEN
19    UnknownToken {
20        /// If this is `true`, the client can acquire a new access token by specifying the device
21        /// ID it is already using to the login API.
22        ///
23        /// For more information, see [the spec].
24        ///
25        /// [the spec]: https://spec.matrix.org/latest/client-server-api/#soft-logout
26        soft_logout: bool,
27    },
28
29    /// M_MISSING_TOKEN
30    MissingToken,
31
32    /// M_BAD_JSON
33    BadJson,
34
35    /// M_NOT_JSON
36    NotJson,
37
38    /// M_NOT_FOUND
39    NotFound,
40
41    /// M_LIMIT_EXCEEDED
42    LimitExceeded {
43        /// How long a client should wait in milliseconds before they can try again.
44        retry_after_ms: Option<Duration>,
45    },
46
47    /// M_UNKNOWN
48    Unknown,
49
50    /// M_UNRECOGNIZED
51    Unrecognized,
52
53    /// M_UNAUTHORIZED
54    Unauthorized,
55
56    /// M_USER_DEACTIVATED
57    UserDeactivated,
58
59    /// M_USER_IN_USE
60    UserInUse,
61
62    /// M_INVALID_USERNAME
63    InvalidUsername,
64
65    /// M_ROOM_IN_USE
66    RoomInUse,
67
68    /// M_INVALID_ROOM_STATE
69    InvalidRoomState,
70
71    /// M_THREEPID_IN_USE
72    ThreepidInUse,
73
74    /// M_THREEPID_NOT_FOUND
75    ThreepidNotFound,
76
77    /// M_THREEPID_AUTH_FAILED
78    ThreepidAuthFailed,
79
80    /// M_THREEPID_DENIED
81    ThreepidDenied,
82
83    /// M_SERVER_NOT_TRUSTED
84    ServerNotTrusted,
85
86    /// M_UNSUPPORTED_ROOM_VERSION
87    UnsupportedRoomVersion,
88
89    /// M_INCOMPATIBLE_ROOM_VERSION
90    IncompatibleRoomVersion {
91        /// The room's version.
92        room_version: RoomVersionId,
93    },
94
95    /// M_BAD_STATE
96    BadState,
97
98    /// M_GUEST_ACCESS_FORBIDDEN
99    GuestAccessForbidden,
100
101    /// M_CAPTCHA_NEEDED
102    CaptchaNeeded,
103
104    /// M_CAPTCHA_INVALID
105    CaptchaInvalid,
106
107    /// M_MISSING_PARAM
108    MissingParam,
109
110    /// M_INVALID_PARAM
111    InvalidParam,
112
113    /// M_TOO_LARGE
114    TooLarge,
115
116    /// M_EXCLUSIVE
117    Exclusive,
118
119    /// M_RESOURCE_LIMIT_EXCEEDED
120    ResourceLimitExceeded {
121        /// A URI giving a contact method for the server administrator.
122        admin_contact: String,
123    },
124
125    /// M_CANNOT_LEAVE_SERVER_NOTICE_ROOM
126    CannotLeaveServerNoticeRoom,
127
128    /// M_WEAK_PASSWORD
129    WeakPassword,
130
131    /// M_UNABLE_TO_AUTHORISE_JOIN
132    UnableToAuthorizeJoin,
133
134    /// M_UNABLE_TO_GRANT_JOIN
135    UnableToGrantJoin,
136
137    /// M_BAD_ALIAS
138    BadAlias,
139
140    /// M_DUPLICATE_ANNOTATION
141    DuplicateAnnotation,
142
143    /// M_NOT_YET_UPLOADED
144    NotYetUploaded,
145
146    /// M_CANNOT_OVERWRITE_MEDIA
147    CannotOverwriteMedia,
148
149    /// M_UNKNOWN_POS for sliding sync
150    UnknownPos,
151
152    /// M_URL_NOT_SET
153    UrlNotSet,
154
155    /// M_BAD_STATUS
156    BadStatus,
157
158    /// M_CONNECTION_FAILED
159    ConnectionFailed,
160
161    /// M_CONNECTION_TIMEOUT
162    ConnectionTimeout,
163
164    /// M_WRONG_ROOM_KEYS_VERSION
165    WrongRoomKeysVersion {
166        /// The currently active backup version.
167        current_version: Option<String>,
168    },
169
170    #[doc(hidden)]
171    _Custom {
172        errcode: PrivOwnedStr,
173        extra: BTreeMap<String, JsonValue>,
174    },
175}
176
177impl AsRef<str> for ErrorKind {
178    fn as_ref(&self) -> &str {
179        match self {
180            Self::Forbidden => "M_FORBIDDEN",
181            Self::UnknownToken { .. } => "M_UNKNOWN_TOKEN",
182            Self::MissingToken => "M_MISSING_TOKEN",
183            Self::BadJson => "M_BAD_JSON",
184            Self::NotJson => "M_NOT_JSON",
185            Self::NotFound => "M_NOT_FOUND",
186            Self::LimitExceeded { .. } => "M_LIMIT_EXCEEDED",
187            Self::Unknown => "M_UNKNOWN",
188            Self::Unrecognized => "M_UNRECOGNIZED",
189            Self::Unauthorized => "M_UNAUTHORIZED",
190            Self::UserDeactivated => "M_USER_DEACTIVATED",
191            Self::UserInUse => "M_USER_IN_USE",
192            Self::InvalidUsername => "M_INVALID_USERNAME",
193            Self::RoomInUse => "M_ROOM_IN_USE",
194            Self::InvalidRoomState => "M_INVALID_ROOM_STATE",
195            Self::ThreepidInUse => "M_THREEPID_IN_USE",
196            Self::ThreepidNotFound => "M_THREEPID_NOT_FOUND",
197            Self::ThreepidAuthFailed => "M_THREEPID_AUTH_FAILED",
198            Self::ThreepidDenied => "M_THREEPID_DENIED",
199            Self::ServerNotTrusted => "M_SERVER_NOT_TRUSTED",
200            Self::UnsupportedRoomVersion => "M_UNSUPPORTED_ROOM_VERSION",
201            Self::IncompatibleRoomVersion { .. } => "M_INCOMPATIBLE_ROOM_VERSION",
202            Self::BadState => "M_BAD_STATE",
203            Self::GuestAccessForbidden => "M_GUEST_ACCESS_FORBIDDEN",
204            Self::CaptchaNeeded => "M_CAPTCHA_NEEDED",
205            Self::CaptchaInvalid => "M_CAPTCHA_INVALID",
206            Self::MissingParam => "M_MISSING_PARAM",
207            Self::InvalidParam => "M_INVALID_PARAM",
208            Self::TooLarge => "M_TOO_LARGE",
209            Self::Exclusive => "M_EXCLUSIVE",
210            Self::ResourceLimitExceeded { .. } => "M_RESOURCE_LIMIT_EXCEEDED",
211            Self::CannotLeaveServerNoticeRoom => "M_CANNOT_LEAVE_SERVER_NOTICE_ROOM",
212            Self::WeakPassword => "M_WEAK_PASSWORD",
213            Self::UnableToAuthorizeJoin => "M_UNABLE_TO_AUTHORISE_JOIN",
214            Self::UnableToGrantJoin => "M_UNABLE_TO_GRANT_JOIN",
215            Self::BadAlias => "M_BAD_ALIAS",
216            Self::DuplicateAnnotation => "M_DUPLICATE_ANNOTATION",
217            Self::NotYetUploaded => "M_NOT_YET_UPLOADED",
218            Self::CannotOverwriteMedia => "M_CANNOT_OVERWRITE_MEDIA",
219            Self::UnknownPos => "M_UNKNOWN_POS",
220            Self::UrlNotSet => "M_URL_NOT_SET",
221            Self::BadStatus { .. } => "M_BAD_STATUS",
222            Self::ConnectionFailed => "M_CONNECTION_FAILED",
223            Self::ConnectionTimeout => "M_CONNECTION_TIMEOUT",
224            Self::WrongRoomKeysVersion { .. } => "M_WRONG_ROOM_KEYS_VERSION",
225            Self::_Custom { errcode, .. } => &errcode.0,
226        }
227    }
228}
229
230impl fmt::Display for ErrorKind {
231    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
232        write!(f, "{}", self.as_ref())
233    }
234}