-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Simplify error message for missing repository (#151)
This simplifies the error message by showing a custom message instead of the message from the underlying git error.
- Loading branch information
1 parent
786a55b
commit cf6e412
Showing
3 changed files
with
91 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
use std::error::Error as ErrorTrait; | ||
use std::fmt; | ||
|
||
macro_rules! error_kind { | ||
($($name:ident, $message:literal),*) => { | ||
/// The kind of error that occurred. | ||
#[derive(Debug)] | ||
#[non_exhaustive] | ||
pub enum ErrorKind { | ||
$( | ||
$name, | ||
)* | ||
} | ||
|
||
impl fmt::Display for ErrorKind { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
match self { | ||
$( | ||
Self::$name => write!(f, $message), | ||
)* | ||
} | ||
} | ||
} | ||
}; | ||
} | ||
|
||
error_kind!(NoRepository, "no repository found"); | ||
|
||
impl ErrorTrait for ErrorKind {} | ||
|
||
#[derive(Debug)] | ||
pub struct Error { | ||
kind: ErrorKind, | ||
source: Option<Box<dyn ErrorTrait>>, | ||
} | ||
|
||
impl Error { | ||
pub fn new(kind: ErrorKind) -> Self { | ||
Self { kind, source: None } | ||
} | ||
|
||
pub fn with_source<E>(kind: ErrorKind, source: E) -> Self | ||
where | ||
E: ErrorTrait + 'static, | ||
{ | ||
Self { | ||
kind, | ||
source: Some(Box::new(source)), | ||
} | ||
} | ||
|
||
pub fn kind(&self) -> &ErrorKind { | ||
&self.kind | ||
} | ||
} | ||
|
||
impl fmt::Display for Error { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
write!(f, "{}", &self.kind) | ||
} | ||
} | ||
|
||
impl ErrorTrait for Error { | ||
fn source(&self) -> Option<&(dyn ErrorTrait + 'static)> { | ||
self.source.as_ref().map(|s| s.as_ref()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters