| 1 |
// Copyright 2018-2019 Mozilla |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use |
| 4 |
// this file except in compliance with the License. You may obtain a copy of the |
| 5 |
// License at http://www.apache.org/licenses/LICENSE-2.0 |
| 6 |
// Unless required by applicable law or agreed to in writing, software distributed |
| 7 |
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR |
| 8 |
// CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 9 |
// specific language governing permissions and limitations under the License. |
| 10 |
|
| 11 |
use std::{fmt, io, path::PathBuf}; |
| 12 |
|
| 13 |
use bincode::Error as BincodeError; |
| 14 |
|
| 15 |
use crate::{backend::traits::BackendError, error::StoreError}; |
| 16 |
|
| 17 |
#[derive(Debug)] |
| 18 |
pub enum ErrorImpl { |
| 19 |
KeyValuePairNotFound, |
| 20 |
EnvPoisonError, |
| 21 |
DbsFull, |
| 22 |
DbsIllegalOpen, |
| 23 |
DbNotFoundError, |
| 24 |
DbIsForeignError, |
| 25 |
UnsuitableEnvironmentPath(PathBuf), |
| 26 |
IoError(io::Error), |
| 27 |
BincodeError(BincodeError), |
| 28 |
} |
| 29 |
|
| 30 |
impl BackendError for ErrorImpl {} |
| 31 |
|
| 32 |
impl fmt::Display for ErrorImpl { |
| 33 |
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { |
| 34 |
match self { |
| 35 |
ErrorImpl::KeyValuePairNotFound => write!(fmt, "KeyValuePairNotFound (safe mode)"), |
| 36 |
ErrorImpl::EnvPoisonError => write!(fmt, "EnvPoisonError (safe mode)"), |
| 37 |
ErrorImpl::DbsFull => write!(fmt, "DbsFull (safe mode)"), |
| 38 |
ErrorImpl::DbsIllegalOpen => write!(fmt, "DbIllegalOpen (safe mode)"), |
| 39 |
ErrorImpl::DbNotFoundError => write!(fmt, "DbNotFoundError (safe mode)"), |
| 40 |
ErrorImpl::DbIsForeignError => write!(fmt, "DbIsForeignError (safe mode)"), |
| 41 |
ErrorImpl::UnsuitableEnvironmentPath(_) => { |
| 42 |
write!(fmt, "UnsuitableEnvironmentPath (safe mode)") |
| 43 |
} |
| 44 |
ErrorImpl::IoError(e) => e.fmt(fmt), |
| 45 |
ErrorImpl::BincodeError(e) => e.fmt(fmt), |
| 46 |
} |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
impl Into<StoreError> for ErrorImpl { |
| 51 |
fn into(self) -> StoreError { |
| 52 |
// The `StoreError::KeyValuePairBadSize` error is unused, because this |
| 53 |
// backend supports keys and values of arbitrary sizes. |
| 54 |
// The `StoreError::MapFull` and `StoreError::ReadersFull` are |
| 55 |
// unimplemented yet, but they should be in the future. |
| 56 |
match self { |
| 57 |
ErrorImpl::KeyValuePairNotFound => StoreError::KeyValuePairNotFound, |
| 58 |
ErrorImpl::BincodeError(_) => StoreError::FileInvalid, |
| 59 |
ErrorImpl::DbsFull => StoreError::DbsFull, |
| 60 |
ErrorImpl::UnsuitableEnvironmentPath(path) => { |
| 61 |
StoreError::UnsuitableEnvironmentPath(path) |
| 62 |
} |
| 63 |
ErrorImpl::IoError(error) => StoreError::IoError(error), |
| 64 |
_ => StoreError::SafeModeError(self), |
| 65 |
} |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
impl From<io::Error> for ErrorImpl { |
| 70 |
fn from(e: io::Error) -> ErrorImpl { |
| 71 |
ErrorImpl::IoError(e) |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
impl From<BincodeError> for ErrorImpl { |
| 76 |
fn from(e: BincodeError) -> ErrorImpl { |
| 77 |
ErrorImpl::BincodeError(e) |
| 78 |
} |
| 79 |
} |