kvdb_web/error.rs
1// Copyright 2020 Parity Technologies
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6// option. This file may not be copied, modified, or distributed
7// except according to those terms.
8
9//! Errors that can occur when working with IndexedDB.
10
11use std::fmt;
12
13/// An error that occurred when working with IndexedDB.
14#[derive(Clone, PartialEq, Debug)]
15pub enum Error {
16 /// Accessing a Window has failed.
17 /// Are we in a WebWorker?
18 WindowNotAvailable,
19 /// IndexedDB is not supported by your browser.
20 NotSupported(String),
21 /// This enum may grow additional variants,
22 /// so this makes sure clients don't count on exhaustive matching.
23 /// (Otherwise, adding a new variant could break existing code.)
24 #[doc(hidden)]
25 __Nonexhaustive,
26}
27
28impl std::error::Error for Error {
29 fn description(&self) -> &str {
30 match *self {
31 Error::WindowNotAvailable => "Accessing a Window has failed",
32 Error::NotSupported(_) => "IndexedDB is not supported by your browser",
33 Error::__Nonexhaustive => unreachable!(),
34 }
35 }
36}
37
38impl fmt::Display for Error {
39 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
40 match *self {
41 Error::WindowNotAvailable => write!(f, "Accessing a Window has failed"),
42 Error::NotSupported(ref err) => write!(f, "IndexedDB is not supported by your browser: {}", err,),
43 Error::__Nonexhaustive => unreachable!(),
44 }
45 }
46}