| 1 |
// Copyright 2018-2019 Mozilla |
| 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 |
|
| 15 |
use std::{error, fmt, result, str::Utf8Error, string::FromUtf16Error}; |
| 16 |
|
| 17 |
use crate::guid::Guid; |
| 18 |
use crate::Item; |
| 19 |
|
| 20 |
pub type Result<T> = result::Result<T, Error>; |
| 21 |
|
| 22 |
#[derive(Debug)] |
| 23 |
pub struct Error(ErrorKind); |
| 24 |
|
| 25 |
impl Error { |
| 26 |
pub fn kind(&self) -> &ErrorKind { |
| 27 |
&self.0 |
| 28 |
} |
| 29 |
} |
| 30 |
|
| 31 |
impl error::Error for Error { |
| 32 |
fn source(&self) -> Option<&(dyn error::Error + 'static)> { |
| 33 |
match self.kind() { |
| 34 |
ErrorKind::MalformedString(err) => Some(err.as_ref()), |
| 35 |
_ => None, |
| 36 |
} |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
impl From<ErrorKind> for Error { |
| 41 |
fn from(kind: ErrorKind) -> Error { |
| 42 |
Error(kind) |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
impl From<FromUtf16Error> for Error { |
| 47 |
fn from(error: FromUtf16Error) -> Error { |
| 48 |
Error(ErrorKind::MalformedString(error.into())) |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
impl From<Utf8Error> for Error { |
| 53 |
fn from(error: Utf8Error) -> Error { |
| 54 |
Error(ErrorKind::MalformedString(error.into())) |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
impl fmt::Display for Error { |
| 59 |
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 60 |
// We format the guid-specific params with <guid: {}> to make it easier on the |
| 61 |
// telemetry side to parse out the user-specific guid and normalize the errors |
| 62 |
// to better aggregate the data |
| 63 |
match self.kind() { |
| 64 |
ErrorKind::MismatchedItemKind(local_item, remote_item) => write!( |
| 65 |
f, |
| 66 |
"Can't merge local {} <guid: {}> and remote {} <guid: {}>", |
| 67 |
local_item.kind, local_item.guid, remote_item.kind, remote_item.guid, |
| 68 |
), |
| 69 |
ErrorKind::DuplicateItem(guid) => { |
| 70 |
write!(f, "Item <guid: {}> already exists in tree", guid) |
| 71 |
} |
| 72 |
ErrorKind::MissingItem(guid) => { |
| 73 |
write!(f, "Item <guid: {}> doesn't exist in tree", guid) |
| 74 |
} |
| 75 |
ErrorKind::InvalidParent(child, parent) => write!( |
| 76 |
f, |
| 77 |
"Can't insert {} <guid: {}> into {} <guid: {}>", |
| 78 |
child.kind, child.guid, parent.kind, parent.guid, |
| 79 |
), |
| 80 |
ErrorKind::InvalidParentForUnknownChild(child_guid, parent) => write!( |
| 81 |
f, |
| 82 |
"Can't insert unknown child <guid: {}> into {} <guid: {}>", |
| 83 |
child_guid, parent.kind, parent.guid, |
| 84 |
), |
| 85 |
ErrorKind::MissingParent(child, parent_guid) => write!( |
| 86 |
f, |
| 87 |
"Can't insert {} <guid: {}> into nonexistent parent <guid: {}>", |
| 88 |
child.kind, child.guid, parent_guid, |
| 89 |
), |
| 90 |
ErrorKind::MissingParentForUnknownChild(child_guid, parent_guid) => write!( |
| 91 |
f, |
| 92 |
"Can't insert unknown child <guid: {}> into nonexistent parent <guid: {}>", |
| 93 |
child_guid, parent_guid, |
| 94 |
), |
| 95 |
ErrorKind::Cycle(guid) => write!(f, "Item <guid: {}> can't contain itself", guid), |
| 96 |
ErrorKind::MergeConflict => write!(f, "Local tree changed during merge"), |
| 97 |
ErrorKind::UnmergedLocalItems => { |
| 98 |
write!(f, "Merged tree doesn't mention all items from local tree") |
| 99 |
} |
| 100 |
ErrorKind::UnmergedRemoteItems => { |
| 101 |
write!(f, "Merged tree doesn't mention all items from remote tree") |
| 102 |
} |
| 103 |
ErrorKind::InvalidGuid(invalid_guid) => { |
| 104 |
write!( |
| 105 |
f, |
| 106 |
"Merged tree contains invalid GUID <guid: {}>", |
| 107 |
invalid_guid |
| 108 |
) |
| 109 |
} |
| 110 |
ErrorKind::InvalidByte(b) => write!(f, "Invalid byte <byte: {}> in UTF-16 encoding", b), |
| 111 |
ErrorKind::MalformedString(err) => err.fmt(f), |
| 112 |
ErrorKind::Abort => write!(f, "Operation aborted"), |
| 113 |
} |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
#[derive(Debug)] |
| 118 |
pub enum ErrorKind { |
| 119 |
MismatchedItemKind(Item, Item), |
| 120 |
DuplicateItem(Guid), |
| 121 |
InvalidParent(Item, Item), |
| 122 |
InvalidParentForUnknownChild(Guid, Item), |
| 123 |
MissingParent(Item, Guid), |
| 124 |
MissingParentForUnknownChild(Guid, Guid), |
| 125 |
MissingItem(Guid), |
| 126 |
Cycle(Guid), |
| 127 |
MergeConflict, |
| 128 |
UnmergedLocalItems, |
| 129 |
UnmergedRemoteItems, |
| 130 |
InvalidGuid(Guid), |
| 131 |
InvalidByte(u16), |
| 132 |
MalformedString(Box<dyn error::Error + Send + Sync + 'static>), |
| 133 |
Abort, |
| 134 |
} |