mirror of https://github.com/bobwen-dev/hunter
some error handling modifications
This commit is contained in:
parent
45203cff36
commit
33a9562379
65
src/fail.rs
65
src/fail.rs
|
@ -16,29 +16,29 @@ pub enum HError {
|
|||
#[fail(display = "IO error: {} ", error)]
|
||||
IoError{#[cause] error: std::io::Error, backtrace: Backtrace},
|
||||
#[fail(display = "Mutex failed")]
|
||||
MutexError,
|
||||
MutexError(Backtrace),
|
||||
#[fail(display = "Can't lock!")]
|
||||
TryLockError,
|
||||
TryLockError(Backtrace),
|
||||
#[fail(display = "Channel failed: {}", error)]
|
||||
ChannelTryRecvError{#[cause] error: std::sync::mpsc::TryRecvError},
|
||||
#[fail(display = "Channel failed: {}", error)]
|
||||
ChannelRecvError{#[cause] error: std::sync::mpsc::RecvError},
|
||||
#[fail(display = "Channel failed")]
|
||||
ChannelSendError,
|
||||
ChannelSendError(Backtrace),
|
||||
#[fail(display = "Previewer failed on file: {}", file)]
|
||||
PreviewFailed{file: String},
|
||||
PreviewFailed{file: String, backtrace: Backtrace},
|
||||
#[fail(display = "StalePreviewer for file: {}", file)]
|
||||
StalePreviewError{file: String},
|
||||
#[fail(display = "Failed: {}", error)]
|
||||
Error{#[cause] error: failure::Error },
|
||||
Error{#[cause] error: failure::Error , backtrace: Backtrace},
|
||||
#[fail(display = "Was None!")]
|
||||
NoneError,
|
||||
NoneError(Backtrace),
|
||||
#[fail(display = "Not ready yet!")]
|
||||
WillBeNotReady(Backtrace),
|
||||
#[fail(display = "No widget found")]
|
||||
NoWidgetError(Backtrace),
|
||||
#[fail(display = "Path: {:?} not in this directory: {:?}", path, dir)]
|
||||
WrongDirectoryError{ path: PathBuf, dir: PathBuf },
|
||||
WrongDirectoryError{ path: PathBuf, dir: PathBuf , backtrace: Backtrace},
|
||||
#[fail(display = "Widget finnished")]
|
||||
PopupFinnished,
|
||||
#[fail(display = "No completions found")]
|
||||
|
@ -54,11 +54,11 @@ pub enum HError {
|
|||
#[fail(display = "HBox ratio mismatch: {} widgets, ratio is {:?}", wnum, ratio)]
|
||||
HBoxWrongRatioError{ wnum: usize, ratio: Vec<usize> },
|
||||
#[fail(display = "Got wrong widget: {}! Wanted: {}", got, wanted)]
|
||||
WrongWidgetError{got: String, wanted: String},
|
||||
WrongWidgetError{got: String, wanted: String, backtrace: Backtrace},
|
||||
#[fail(display = "Strip Prefix Error: {}", error)]
|
||||
StripPrefixError{#[cause] error: std::path::StripPrefixError},
|
||||
StripPrefixError{#[cause] error: std::path::StripPrefixError, backtrace: Backtrace},
|
||||
#[fail(display = "INofify failed: {}", error)]
|
||||
INotifyError{#[cause] error: notify::Error},
|
||||
INotifyError{#[cause] error: notify::Error, backtrace: Backtrace},
|
||||
#[fail(display = "Tags not loaded yet")]
|
||||
TagsNotLoadedYetError,
|
||||
#[fail(display = "Input cancelled!")]
|
||||
|
@ -81,7 +81,8 @@ impl HError {
|
|||
}
|
||||
pub fn wrong_widget<T>(got: &str, wanted: &str) -> HResult<T> {
|
||||
Err(HError::WrongWidgetError{ got: got.to_string(),
|
||||
wanted: wanted.to_string()})
|
||||
wanted: wanted.to_string(),
|
||||
backtrace: Backtrace::new()})
|
||||
}
|
||||
pub fn popup_finnished<T>() -> HResult<T> {
|
||||
Err(HError::PopupFinnished)
|
||||
|
@ -98,6 +99,16 @@ impl HError {
|
|||
pub fn undefined_key<T>(key: Key) -> HResult<T> {
|
||||
Err(HError::WidgetUndefinedKeyError { key: key })
|
||||
}
|
||||
pub fn wrong_directory<T>(path: PathBuf, dir: PathBuf) -> HResult<T> {
|
||||
Err(HError::WrongDirectoryError{ path: path,
|
||||
dir: dir,
|
||||
backtrace: Backtrace::new() })
|
||||
}
|
||||
pub fn preview_failed<T>(file: &crate::files::File) -> HResult<T> {
|
||||
let name = file.name.clone();
|
||||
Err(HError::PreviewFailed{ file: name,
|
||||
backtrace: Backtrace::new() })
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@ -122,7 +133,7 @@ pub trait ErrorLog where Self: Sized {
|
|||
impl<T> ErrorLog for HResult<T> {
|
||||
fn log(self) {
|
||||
if let Err(err) = self {
|
||||
eprintln!("{:?}", err);
|
||||
// eprintln!("{:?}", err);
|
||||
put_log(&err).ok();
|
||||
}
|
||||
}
|
||||
|
@ -140,7 +151,7 @@ impl<T> ErrorLog for HResult<T> {
|
|||
|
||||
impl From<std::io::Error> for HError {
|
||||
fn from(error: std::io::Error) -> Self {
|
||||
dbg!(&error);
|
||||
// dbg!(&error);
|
||||
let err = HError::IoError { error: error, backtrace: Backtrace::new() };
|
||||
put_log(&err).ok();
|
||||
err
|
||||
|
@ -149,8 +160,8 @@ impl From<std::io::Error> for HError {
|
|||
|
||||
impl From<failure::Error> for HError {
|
||||
fn from(error: failure::Error) -> Self {
|
||||
dbg!(&error);
|
||||
let err = HError::Error { error: error };
|
||||
// dbg!(&error);
|
||||
let err = HError::Error { error: error, backtrace: Backtrace::new() };
|
||||
put_log(&err).ok();
|
||||
err
|
||||
}
|
||||
|
@ -158,7 +169,7 @@ impl From<failure::Error> for HError {
|
|||
|
||||
impl From<std::sync::mpsc::TryRecvError> for HError {
|
||||
fn from(error: std::sync::mpsc::TryRecvError) -> Self {
|
||||
dbg!(&error);
|
||||
// dbg!(&error);
|
||||
let err = HError::ChannelTryRecvError { error: error };
|
||||
put_log(&err).ok();
|
||||
err
|
||||
|
@ -167,7 +178,7 @@ impl From<std::sync::mpsc::TryRecvError> for HError {
|
|||
|
||||
impl From<std::sync::mpsc::RecvError> for HError {
|
||||
fn from(error: std::sync::mpsc::RecvError) -> Self {
|
||||
dbg!(&error);
|
||||
// dbg!(&error);
|
||||
let err = HError::ChannelRecvError { error: error };
|
||||
put_log(&err).ok();
|
||||
err
|
||||
|
@ -177,7 +188,7 @@ impl From<std::sync::mpsc::RecvError> for HError {
|
|||
impl<T> From<std::sync::mpsc::SendError<T>> for HError {
|
||||
fn from(error: std::sync::mpsc::SendError<T>) -> Self {
|
||||
dbg!(&error);
|
||||
let err = HError::ChannelSendError;
|
||||
let err = HError::ChannelSendError(Backtrace::new());
|
||||
put_log(&err).ok();
|
||||
err
|
||||
}
|
||||
|
@ -185,8 +196,8 @@ impl<T> From<std::sync::mpsc::SendError<T>> for HError {
|
|||
|
||||
impl<T> From<std::sync::PoisonError<T>> for HError {
|
||||
fn from(_: std::sync::PoisonError<T>) -> Self {
|
||||
dbg!("Poisoned Mutex");
|
||||
let err = HError::MutexError;
|
||||
// dbg!("Poisoned Mutex");
|
||||
let err = HError::MutexError(Backtrace::new());
|
||||
put_log(&err).ok();
|
||||
err
|
||||
}
|
||||
|
@ -194,8 +205,8 @@ impl<T> From<std::sync::PoisonError<T>> for HError {
|
|||
|
||||
impl<T> From<std::sync::TryLockError<T>> for HError {
|
||||
fn from(error: std::sync::TryLockError<T>) -> Self {
|
||||
dbg!(&error);
|
||||
let err = HError::TryLockError;
|
||||
// dbg!(&error);
|
||||
let err = HError::TryLockError(Backtrace::new());
|
||||
put_log(&err).ok();
|
||||
err
|
||||
}
|
||||
|
@ -204,7 +215,7 @@ impl<T> From<std::sync::TryLockError<T>> for HError {
|
|||
impl From<std::option::NoneError> for HError {
|
||||
fn from(error: std::option::NoneError) -> Self {
|
||||
//dbg!(&error);
|
||||
let err = HError::NoneError;
|
||||
let err = HError::NoneError(Backtrace::new());
|
||||
//put_log(&err).ok();
|
||||
err
|
||||
}
|
||||
|
@ -212,8 +223,8 @@ impl From<std::option::NoneError> for HError {
|
|||
|
||||
impl From<std::path::StripPrefixError> for HError {
|
||||
fn from(error: std::path::StripPrefixError) -> Self {
|
||||
dbg!(&error);
|
||||
let err = HError::StripPrefixError{error: error};
|
||||
// dbg!(&error);
|
||||
let err = HError::StripPrefixError{error: error, backtrace: Backtrace::new() };
|
||||
put_log(&err).ok();
|
||||
err
|
||||
}
|
||||
|
@ -221,8 +232,8 @@ impl From<std::path::StripPrefixError> for HError {
|
|||
|
||||
impl From<notify::Error> for HError {
|
||||
fn from(error: notify::Error) -> Self {
|
||||
dbg!(&error);
|
||||
let err = HError::INotifyError{error: error};
|
||||
// dbg!(&error);
|
||||
let err = HError::INotifyError{error: error, backtrace: Backtrace::new() };
|
||||
put_log(&err).ok();
|
||||
err
|
||||
}
|
||||
|
|
|
@ -364,11 +364,13 @@ impl FileBrowser {
|
|||
|
||||
pub fn set_left_selection(&mut self) -> HResult<()> {
|
||||
if !self.left_widget()?.ready() { return Ok(()) }
|
||||
if self.cwd.parent().is_none() { return Ok(()) }
|
||||
|
||||
let parent = self.cwd()?.parent_as_file();
|
||||
|
||||
let left_selection = self.get_selection(&parent?)?;
|
||||
self.left_widget()?.widget()?.lock()?.as_mut()?.select_file(&left_selection);
|
||||
if let Ok(left_selection) = self.get_selection(&parent?) {
|
||||
self.left_widget()?.widget()?.lock()?.as_mut()?.select_file(&left_selection);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
@ -448,8 +450,15 @@ impl FileBrowser {
|
|||
}
|
||||
if let Some(preview_dir) = preview_dir {
|
||||
if !watched_dirs.contains(&preview_dir) && preview_dir.is_dir() {
|
||||
self.watcher.watch(&preview_dir, RecursiveMode::NonRecursive)?;
|
||||
self.watches.push(preview_dir);
|
||||
match self.watcher.watch(&preview_dir, RecursiveMode::NonRecursive) {
|
||||
Ok(_) => self.watches.push(preview_dir),
|
||||
Err(notify::Error::Io(ioerr)) => {
|
||||
if ioerr.kind() != std::io::ErrorKind::PermissionDenied {
|
||||
Err(ioerr)?
|
||||
}
|
||||
}
|
||||
err @ _ => err?
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
|
|
|
@ -256,8 +256,7 @@ impl Files {
|
|||
if dir == path {
|
||||
Ok(true)
|
||||
} else {
|
||||
Err(HError::WrongDirectoryError{path: path.into(),
|
||||
dir: dir})
|
||||
HError::wrong_directory(path.into(), dir)?
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -381,14 +380,14 @@ impl File {
|
|||
pub fn meta(&self) -> HResult<std::fs::Metadata> {
|
||||
match &self.meta {
|
||||
Some(meta) => Ok(meta.clone()),
|
||||
None => { Ok(std::fs::metadata(&self.path)?) }
|
||||
None => { Ok(std::fs::symlink_metadata(&self.path)?) }
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_meta(&mut self) -> HResult<()> {
|
||||
if let Some(_) = self.meta { return Ok(()) }
|
||||
|
||||
let meta = std::fs::metadata(&self.path)?;
|
||||
let meta = std::fs::symlink_metadata(&self.path)?;
|
||||
let color = self.get_color(&meta);
|
||||
|
||||
self.meta = Some(meta);
|
||||
|
|
|
@ -320,7 +320,7 @@ impl Previewer {
|
|||
}
|
||||
|
||||
fn preview_failed(file: &File) -> HResult<WidgetO> {
|
||||
Err(HError::PreviewFailed { file: file.name.clone() })
|
||||
HError::preview_failed(file)
|
||||
}
|
||||
|
||||
fn preview_dir(file: &File,
|
||||
|
@ -397,9 +397,9 @@ impl Previewer {
|
|||
}
|
||||
|
||||
let status = output.status.code()
|
||||
.ok_or(HError::PreviewFailed{file: file.name.clone()})?;
|
||||
.ok_or(HError::preview_failed(file)?);
|
||||
|
||||
if status == 0 || status == 5 && !is_stale(&stale)? { //is_current(&file) {
|
||||
if status == Ok(0) || status == Ok(5) && !is_stale(&stale)? {
|
||||
let output = std::str::from_utf8(&output.stdout)
|
||||
.unwrap()
|
||||
.to_string();
|
||||
|
@ -414,7 +414,7 @@ impl Previewer {
|
|||
textview.animate_slide_up().log();
|
||||
return Ok(Box::new(textview))
|
||||
}
|
||||
Err(HError::PreviewFailed{file: file.name.clone()})
|
||||
HError::preview_failed(file)
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue