github_local_remote/
error.rs

1use failure::{Backtrace, Context, Fail};
2use std::fmt::{self, Display};
3use std::io;
4use std::result;
5
6/// A specialized [`Result`] type for this crate's operations.
7///
8/// This is generally used to avoid writing out [Error] directly and
9/// is otherwise a direct mapping to [`Result`].
10///
11/// [`Result`]: https://doc.rust-lang.org/nightly/std/result/enum.Result.html
12/// [`Error`]: std.struct.Error.html
13pub type Result<T> = result::Result<T, Error>;
14
15/// A list enumerating the categories of errors in this crate.
16///
17/// This list is intended to grow over time and it is not recommended to
18/// exhaustively match against it.
19///
20/// It is used with the [`Error`] struct.
21///
22/// [`Error`]: std.struct.Error.html
23#[derive(Debug, Fail)]
24pub enum ErrorKind {
25  /// An error caused by the logger failing.
26  #[fail(display = "Log error.")]
27  Log,
28  /// Any error not part of this list.
29  #[fail(display = "Generic error.")]
30  Other,
31  /// A generic error caused by Git.
32  #[fail(display = "Git error.")]
33  Git,
34  /// Git couldn't find the "origin" remote.
35  #[fail(display = "The remote 'origin' not found in the git repo.")]
36  GitRemoteOrigin,
37  /// The Git remote URL was not found.
38  #[fail(display = "The remote 'origin' does not have a url set.")]
39  GitRemoteUrl,
40  /// The Git remote URL was not a valid GitHub url
41  #[fail(
42    display = "The git url for remote 'origin' is not a valid GitHub url."
43  )]
44  GitHubUrl,
45  /// An error caused by an IO failure.
46  #[fail(display = "{}", _0)]
47  Io(#[cause] io::Error),
48}
49
50/// A specialized [`Error`] type for this crate's operations.
51///
52/// [`Error`]: https://doc.rust-lang.org/nightly/std/error/trait.Error.html
53#[derive(Debug)]
54pub struct Error {
55  inner: Context<ErrorKind>,
56}
57
58impl Error {
59  /// Access the [`ErrorKind`] member.
60  ///
61  /// [`ErrorKind`]: enum.ErrorKind.html
62  pub fn kind(&self) -> &ErrorKind {
63    &*self.inner.get_context()
64  }
65}
66
67impl Fail for Error {
68  fn cause(&self) -> Option<&Fail> {
69    self.inner.cause()
70  }
71
72  fn backtrace(&self) -> Option<&Backtrace> {
73    self.inner.backtrace()
74  }
75}
76
77impl Display for Error {
78  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
79    Display::fmt(&self.inner, f)
80  }
81}
82
83impl From<ErrorKind> for Error {
84  fn from(kind: ErrorKind) -> Error {
85    let inner = Context::new(kind);
86    Error { inner }
87  }
88}
89
90impl From<Context<ErrorKind>> for Error {
91  fn from(inner: Context<ErrorKind>) -> Error {
92    Error { inner }
93  }
94}