1use crate::storage::bookmarks::BookmarkRootGuid;
6use crate::types::BookmarkType;
7use error_support::{ErrorHandling, GetErrorHandling};
8use interrupt_support::Interrupted;
9
10pub use error_support::{debug, error, info, trace, warn};
12
13pub type Result<T> = std::result::Result<T, Error>;
15pub type ApiResult<T> = std::result::Result<T, PlacesApiError>;
17
18#[derive(Debug, thiserror::Error)]
20pub enum PlacesApiError {
21 #[error("Unexpected error: {reason}")]
22 UnexpectedPlacesException { reason: String },
23
24 #[error("UrlParseFailed: {reason}")]
29 UrlParseFailed { reason: String },
30
31 #[error("PlacesConnectionBusy error: {reason}")]
32 PlacesConnectionBusy { reason: String },
33
34 #[error("Operation Interrupted: {reason}")]
35 OperationInterrupted { reason: String },
36
37 #[error("Unknown bookmark: {reason}")]
40 UnknownBookmarkItem { reason: String },
41
42 #[error("Invalid bookmark operation: {reason}")]
49 InvalidBookmarkOperation { reason: String },
50}
51
52#[derive(Debug, thiserror::Error)]
54pub enum Error {
55 #[error("Invalid place info: {0}")]
56 InvalidPlaceInfo(#[from] InvalidPlaceInfo),
57
58 #[error("The store is corrupt: {0}")]
59 Corruption(#[from] Corruption),
60
61 #[error("Error merging: {0}")]
62 MergeError(#[from] dogear::Error),
63
64 #[error("Error parsing JSON data: {0}")]
65 JsonError(#[from] serde_json::Error),
66
67 #[error("Error executing SQL: {0}")]
68 SqlError(#[from] rusqlite::Error),
69
70 #[error("Error parsing URL: {0}")]
71 UrlParseError(#[from] url::ParseError),
72
73 #[error("A connection of this type is already open")]
74 ConnectionAlreadyOpen,
75
76 #[error("An invalid connection type was specified")]
77 InvalidConnectionType,
78
79 #[error("IO error: {0}")]
80 IoError(#[from] std::io::Error),
81
82 #[error("Operation interrupted")]
83 InterruptedError(#[from] Interrupted),
84
85 #[error("Tried to close connection on wrong PlacesApi instance")]
86 WrongApiForClose,
87
88 #[error("Incoming bookmark missing type")]
89 MissingBookmarkKind,
90
91 #[error("Synced bookmark has unsupported kind {0}")]
92 UnsupportedSyncedBookmarkKind(u8),
93
94 #[error("Synced bookmark has unsupported validity {0}")]
95 UnsupportedSyncedBookmarkValidity(u8),
96
97 #[error("Illegal database path: {0:?}")]
101 IllegalDatabasePath(std::path::PathBuf),
102
103 #[error("UTF8 Error: {0}")]
104 Utf8Error(#[from] std::str::Utf8Error),
105
106 #[error("Can not import from database version {0}")]
109 UnsupportedDatabaseVersion(i64),
110
111 #[error("Error opening database: {0}")]
112 OpenDatabaseError(#[from] sql_support::open_database::Error),
113
114 #[error("Invalid metadata observation: {0}")]
115 InvalidMetadataObservation(#[from] InvalidMetadataObservation),
116}
117
118#[derive(Debug, thiserror::Error)]
119pub enum InvalidPlaceInfo {
120 #[error("No url specified")]
121 NoUrl,
122 #[error("Invalid guid")]
123 InvalidGuid,
124 #[error("Invalid parent: {0}")]
125 InvalidParent(String),
126 #[error("Invalid child guid")]
127 InvalidChildGuid,
128
129 #[error("No such item: {0}")]
132 NoSuchGuid(String),
133
134 #[error("No such url")]
137 NoSuchUrl,
138
139 #[error("Can't update a bookmark of type {0} with one of type {1}")]
140 MismatchedBookmarkType(u8, u8),
141
142 #[error("URL too long")]
145 UrlTooLong,
146
147 #[error("The tag value is invalid")]
149 InvalidTag,
150 #[error("Cannot change the '{0}' property of a bookmark of type {1:?}")]
151 IllegalChange(&'static str, BookmarkType),
152
153 #[error("Cannot update the bookmark root {0:?}")]
154 CannotUpdateRoot(BookmarkRootGuid),
155}
156
157#[derive(Debug, thiserror::Error)]
162pub enum Corruption {
163 #[error("Bookmark '{0}' has a parent of '{1}' which does not exist")]
164 NoParent(String, String),
165
166 #[error("The local roots are invalid")]
167 InvalidLocalRoots,
168
169 #[error("The synced roots are invalid")]
170 InvalidSyncedRoots,
171
172 #[error("Bookmark '{0}' has no parent but is not the bookmarks root")]
173 NonRootWithoutParent(String),
174}
175
176#[derive(Debug, thiserror::Error)]
177pub enum InvalidMetadataObservation {
178 #[error("Observed view time is invalid (too long)")]
179 ViewTimeTooLong,
180}
181
182impl GetErrorHandling for Error {
185 type ExternalError = PlacesApiError;
186
187 fn get_error_handling(&self) -> ErrorHandling<Self::ExternalError> {
188 match self {
189 Error::InvalidPlaceInfo(info) => {
190 let label = info.to_string();
191 ErrorHandling::convert(match &info {
192 InvalidPlaceInfo::InvalidParent(..) => {
193 PlacesApiError::InvalidBookmarkOperation { reason: label }
194 }
195 InvalidPlaceInfo::UrlTooLong => {
196 PlacesApiError::UrlParseFailed { reason: label }
197 }
198 InvalidPlaceInfo::NoSuchGuid(..) => {
199 PlacesApiError::UnknownBookmarkItem { reason: label }
200 }
201 InvalidPlaceInfo::IllegalChange(..) => {
202 PlacesApiError::InvalidBookmarkOperation { reason: label }
203 }
204 InvalidPlaceInfo::CannotUpdateRoot(..) => {
205 PlacesApiError::InvalidBookmarkOperation { reason: label }
206 }
207 _ => PlacesApiError::UnexpectedPlacesException { reason: label },
208 })
209 .report_error("places-invalid-place-info")
210 }
211 Error::UrlParseError(e) => {
212 ErrorHandling::convert(PlacesApiError::UrlParseFailed {
215 reason: e.to_string(),
216 })
217 .log_warning()
218 }
219 Error::SqlError(rusqlite::Error::SqliteFailure(err, _)) => match err.code {
220 rusqlite::ErrorCode::DatabaseBusy => {
221 ErrorHandling::convert(PlacesApiError::PlacesConnectionBusy {
222 reason: self.to_string(),
223 })
224 .log_warning()
225 }
226 rusqlite::ErrorCode::OperationInterrupted => {
227 ErrorHandling::convert(PlacesApiError::OperationInterrupted {
228 reason: self.to_string(),
229 })
230 .log_info()
231 }
232 rusqlite::ErrorCode::DatabaseCorrupt => {
233 ErrorHandling::convert(PlacesApiError::UnexpectedPlacesException {
234 reason: self.to_string(),
235 })
236 .report_error("places-db-corrupt")
237 }
238 rusqlite::ErrorCode::DiskFull => {
239 ErrorHandling::convert(PlacesApiError::UnexpectedPlacesException {
240 reason: self.to_string(),
241 })
242 .report_error("places-db-disk-full")
243 }
244 _ => ErrorHandling::convert(PlacesApiError::UnexpectedPlacesException {
245 reason: self.to_string(),
246 })
247 .report_error("places-unexpected"),
248 },
249 Error::InterruptedError(err) => {
250 ErrorHandling::convert(PlacesApiError::OperationInterrupted {
252 reason: err.to_string(),
253 })
254 .log_info()
255 }
256 Error::Corruption(e) => {
257 ErrorHandling::convert(PlacesApiError::UnexpectedPlacesException {
258 reason: e.to_string(),
259 })
260 .report_error("places-bookmarks-corruption")
261 }
262 Error::InvalidMetadataObservation(InvalidMetadataObservation::ViewTimeTooLong) => {
263 ErrorHandling::convert(PlacesApiError::UnexpectedPlacesException {
264 reason: self.to_string(),
265 })
266 .log_warning()
267 }
268 _ => ErrorHandling::convert(PlacesApiError::UnexpectedPlacesException {
269 reason: self.to_string(),
270 })
271 .report_error("places-unexpected-error"),
272 }
273 }
274}