Skip to main content

matrix_sdk_ui/timeline/
error.rs

1// Copyright 2023 The Matrix.org Foundation C.I.C.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use matrix_sdk::{
16    HttpError, event_cache::EventCacheError, paginators::PaginatorError, room::reply::ReplyError,
17    send_queue::RoomSendQueueError,
18};
19use thiserror::Error;
20
21use crate::timeline::TimelineEventItemId;
22
23/// Errors specific to the timeline.
24#[derive(Error, Debug)]
25#[non_exhaustive]
26pub enum Error {
27    /// The requested event is not in the timeline.
28    #[error("Event not found in timeline: {0:?}")]
29    EventNotInTimeline(TimelineEventItemId),
30
31    /// The event is currently unsupported for this use case..
32    #[error("Unsupported event")]
33    UnsupportedEvent,
34
35    /// Couldn't read the attachment data from the given URL.
36    #[error("Invalid attachment data")]
37    InvalidAttachmentData,
38
39    /// The attachment file name used as a body is invalid.
40    #[error("Invalid attachment file name")]
41    InvalidAttachmentFileName,
42
43    /// The attachment could not be sent.
44    #[error("Failed sending attachment")]
45    FailedSendingAttachment,
46
47    /// The redaction could not be sent.
48    #[error("Failed sending redaction")]
49    FailedSendingRedaction,
50
51    /// The reaction could not be toggled.
52    #[error("Failed toggling reaction")]
53    FailedToToggleReaction,
54
55    /// Couldn't read the encryption state of the room.
56    #[error("The room's encryption state is unknown.")]
57    UnknownEncryptionState,
58
59    /// An error from the underlying Matrix SDK.
60    #[error(transparent)]
61    Sdk(#[from] matrix_sdk::Error),
62
63    /// The Event Cache must be enabled.
64    #[error("The Event Cache must be enabled to use the Timeline.")]
65    EventCacheIsDisabled,
66
67    /// Something went wrong with the room event cache.
68    #[error(transparent)]
69    EventCacheError(#[from] EventCacheError),
70
71    /// An error happened during pagination.
72    #[error(transparent)]
73    PaginationError(#[from] PaginationError),
74
75    /// An error happened while operating the room's send queue.
76    #[error(transparent)]
77    SendQueueError(#[from] RoomSendQueueError),
78
79    /// An error happened while attempting to edit an event.
80    #[error(transparent)]
81    EditError(#[from] EditError),
82
83    /// An error happened while attempting to reply to an event.
84    #[error(transparent)]
85    ReplyError(#[from] ReplyError),
86
87    /// An error happened while attempting to redact an event.
88    #[error(transparent)]
89    RedactError(#[from] RedactError),
90}
91
92#[derive(Error, Debug)]
93pub enum EditError {
94    /// The content types have changed.
95    #[error("the new content type ({new}) doesn't match that of the previous content ({original}")]
96    ContentMismatch { original: String, new: String },
97
98    /// The local echo we tried to edit has been lost.
99    #[error("Invalid state: the local echo we tried to abort has been lost.")]
100    InvalidLocalEchoState,
101
102    /// An error happened at a lower level.
103    #[error(transparent)]
104    RoomError(#[from] matrix_sdk::room::edit::EditError),
105}
106
107#[derive(Error, Debug)]
108pub enum RedactError {
109    /// Local event to redact wasn't found for transaction id
110    #[error("Event to redact wasn't found for item id {0:?}")]
111    ItemNotFound(TimelineEventItemId),
112
113    /// An error happened while attempting to redact an event.
114    #[error(transparent)]
115    HttpError(#[from] HttpError),
116
117    /// The local echo we tried to abort has been lost.
118    #[error("Invalid state: the local echo we tried to abort has been lost.")]
119    InvalidLocalEchoState,
120}
121
122#[derive(Error, Debug)]
123pub enum PaginationError {
124    /// An error occurred while paginating.
125    #[error("Error when paginating: {0}")]
126    Pagination(#[from] PaginatorError),
127
128    /// An error occurred in the event cache.
129    #[error("Error in event cache.")]
130    EventCache(#[source] EventCacheError),
131
132    /// The focused event doesn't have an attached cache.
133    #[error("Missing cache for focused event")]
134    MissingCache,
135
136    #[error("Pagination type not supported in this focus mode")]
137    NotSupported,
138}
139
140#[derive(Debug, Error)]
141pub enum UnsupportedEditItem {
142    #[error("tried to edit a non-poll event")]
143    NotPollEvent,
144    #[error("tried to edit another user's event")]
145    NotOwnEvent,
146    #[error("event to edit not found")]
147    MissingEvent,
148}
149
150#[derive(Debug, Error)]
151pub enum SendEventError {
152    #[error(transparent)]
153    UnsupportedEditItem(#[from] UnsupportedEditItem),
154
155    #[error(transparent)]
156    RoomQueueError(#[from] RoomSendQueueError),
157}