places/
error.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5use crate::storage::bookmarks::BookmarkRootGuid;
6use crate::types::BookmarkType;
7use error_support::{ErrorHandling, GetErrorHandling};
8use interrupt_support::Interrupted;
9
10// reexport logging helpers.
11pub use error_support::{debug, error, info, trace, warn};
12
13// Result type used internally
14pub type Result<T> = std::result::Result<T, Error>;
15// Functions which are part of the public API should use this Result.
16pub type ApiResult<T> = std::result::Result<T, PlacesApiError>;
17
18// Errors we return via the public interface.
19#[derive(Debug, thiserror::Error)]
20pub enum PlacesApiError {
21    #[error("Unexpected error: {reason}")]
22    UnexpectedPlacesException { reason: String },
23
24    /// Thrown for invalid URLs
25    ///
26    /// This includes attempting to insert a URL greater than 65536 bytes
27    /// (after punycoding and percent encoding).
28    #[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    /// Thrown when providing a guid to a create or update function
38    /// which does not refer to a known bookmark.
39    #[error("Unknown bookmark: {reason}")]
40    UnknownBookmarkItem { reason: String },
41
42    /// Attempt to create/update/delete a bookmark item in an illegal way.
43    ///
44    /// Some examples:
45    ///  - Attempting to change the URL of a bookmark folder
46    ///  - Referring to a non-folder as the parentGUID parameter to a create or update
47    ///  - Attempting to insert a child under BookmarkRoot.Root,
48    #[error("Invalid bookmark operation: {reason}")]
49    InvalidBookmarkOperation { reason: String },
50}
51
52/// Error enum used internally
53#[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    // This will happen if you provide something absurd like
98    // "/" or "" as your database path. For more subtley broken paths,
99    // we'll likely return an IoError.
100    #[error("Illegal database path: {0:?}")]
101    IllegalDatabasePath(std::path::PathBuf),
102
103    #[error("UTF8 Error: {0}")]
104    Utf8Error(#[from] std::str::Utf8Error),
105
106    // This error is saying an old Fennec or iOS version isn't supported - it's never used for
107    // our specific version.
108    #[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    // NoSuchGuid is used for guids, which aren't considered private information,
130    // so it's fine if this error, including the guid, is in the logs.
131    #[error("No such item: {0}")]
132    NoSuchGuid(String),
133
134    // NoSuchUrl is used for URLs, which are private information, so the URL
135    // itself is not included in the error.
136    #[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    // Only returned when attempting to insert a bookmark --
143    // for history we just ignore it.
144    #[error("URL too long")]
145    UrlTooLong,
146
147    // Like Urls, a tag is considered private info, so the value isn't in the error.
148    #[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// Error types used when we can't continue due to corruption.
158// Note that this is currently only for "logical" corruption. Should we
159// consider mapping sqlite error codes which mean a lower-level of corruption
160// into an enum value here?
161#[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
182// Define how our internal errors are handled and converted to external errors
183// See `support/error/README.md` for how this works, especially the warning about PII.
184impl 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                // This is a known issue with invalid URLs coming from Fenix. Let's just log a
213                // warning for this one. See #5235 for more details.
214                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                // Can't unify with the above ... :(
251                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}