event_loop/
error.rs

1use failure::{self, Backtrace, Context, Fail};
2use std::fmt::{self, Display};
3use std::result;
4
5/// A specialized [`Result`] type for this crate's operations.
6///
7/// This is generally used to avoid writing out [Error] directly and
8/// is otherwise a direct mapping to [`Result`].
9///
10/// [`Result`]: https://doc.rust-lang.org/nightly/std/result/enum.Result.html
11/// [`Error`]: std.struct.Error.html
12pub type Result<T> = result::Result<T, failure::Error>;
13
14/// A list enumerating the categories of errors in this crate.
15///
16/// This list is intended to grow over time and it is not recommended to
17/// exhaustively match against it.
18///
19/// It is used with the [`Error`] struct.
20///
21/// [`Error`]: std.struct.Error.html
22#[derive(Debug, Fail)]
23pub enum ErrorKind {
24  /// Any error not part of this list.
25  #[fail(display = "Generic error.")]
26  Other,
27}
28
29/// A specialized [`Error`] type for this crate's operations.
30///
31/// [`Error`]: https://doc.rust-lang.org/nightly/std/error/trait.Error.html
32#[derive(Debug)]
33pub struct Error {
34  inner: Context<ErrorKind>,
35}
36
37impl Error {
38  /// Access the [`ErrorKind`] member.
39  ///
40  /// [`ErrorKind`]: enum.ErrorKind.html
41  pub fn kind(&self) -> &ErrorKind {
42    &*self.inner.get_context()
43  }
44}
45
46impl Fail for Error {
47  fn cause(&self) -> Option<&dyn Fail> {
48    self.inner.cause()
49  }
50
51  fn backtrace(&self) -> Option<&Backtrace> {
52    self.inner.backtrace()
53  }
54}
55
56impl Display for Error {
57  fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
58    Display::fmt(&self.inner, f)
59  }
60}
61
62impl From<ErrorKind> for Error {
63  fn from(kind: ErrorKind) -> Error {
64    let inner = Context::new(kind);
65    Error { inner }
66  }
67}
68
69impl From<Context<ErrorKind>> for Error {
70  fn from(inner: Context<ErrorKind>) -> Error {
71    Error { inner }
72  }
73}