summaryrefslogtreecommitdiffstats
path: root/src/error.rs
diff options
context:
space:
mode:
authorJiayi Zhao <jeff.no.zhao@gmail.com>2019-06-29 22:19:47 -0400
committerJiayi Zhao <jeff.no.zhao@gmail.com>2019-06-29 22:23:31 -0400
commit65f0f33a564d91bd8126ce684c02f245e5ea9a1f (patch)
treebc9bb5d877cee131dc7870c5f9f5d0c71898efbd /src/error.rs
parent58f6428e2aead58b7f930008b294ce56df59e3cb (diff)
rework error system
- JoshutoErrorKind now envelops all possible errors by Joshuto - JoshutoError behaves like std::io::Error - add JoshutoResult
Diffstat (limited to 'src/error.rs')
-rw-r--r--src/error.rs101
1 files changed, 81 insertions, 20 deletions
diff --git a/src/error.rs b/src/error.rs
index b762ce8..963964d 100644
--- a/src/error.rs
+++ b/src/error.rs
@@ -1,32 +1,93 @@
-pub enum JoshutoError {
- IO(std::io::Error),
- Keymap(KeymapError),
-}
+use std::io::ErrorKind;
+
+#[derive(Copy, Clone, Debug)]
+pub enum JoshutoErrorKind {
+ IONotFound,
+ IOPermissionDenied,
+ IOConnectionRefused,
+ IOConnectionReset,
+ IOConnectionAborted,
+ IONotConnected,
+ IOAddrInUse,
+ IOAddrNotAvailable,
+ IOBrokenPipe,
+ IOAlreadyExists,
+ IOWouldBlock,
+ IOInvalidInput,
+
+ // also used for invalid arguments
+ IOInvalidData,
+ IOTimedOut,
+ IOWriteZero,
+ IOInterrupted,
+ IOOther,
+ IOUnexpectedEof,
+
+ // environment variable not found
+ EnvVarNotFound,
-/*
-pub enum KeymapErrorKind {
- Parse,
- UnknownArgument,
+ ParseError,
UnknownCommand,
}
-*/
-pub struct KeymapError {
- pub command: Option<&'static str>,
- pub error: String,
+impl std::convert::From<ErrorKind> for JoshutoErrorKind {
+ fn from(err: ErrorKind) -> Self {
+ match err {
+ ErrorKind::NotFound => JoshutoErrorKind::IONotFound,
+ ErrorKind::PermissionDenied => JoshutoErrorKind::IOPermissionDenied,
+ ErrorKind::ConnectionRefused => JoshutoErrorKind::IOConnectionRefused,
+ ErrorKind::ConnectionReset => JoshutoErrorKind::IOConnectionReset,
+ ErrorKind::ConnectionAborted => JoshutoErrorKind::IOConnectionAborted,
+ ErrorKind::NotConnected => JoshutoErrorKind::IONotConnected,
+ ErrorKind::AddrInUse => JoshutoErrorKind::IOAddrInUse,
+ ErrorKind::AddrNotAvailable => JoshutoErrorKind::IOAddrNotAvailable,
+ ErrorKind::BrokenPipe => JoshutoErrorKind::IOBrokenPipe,
+ ErrorKind::AlreadyExists => JoshutoErrorKind::IOAlreadyExists,
+ ErrorKind::WouldBlock => JoshutoErrorKind::IOWouldBlock,
+ ErrorKind::InvalidInput => JoshutoErrorKind::IOInvalidInput,
+ ErrorKind::InvalidData => JoshutoErrorKind::IOInvalidData,
+ ErrorKind::TimedOut => JoshutoErrorKind::IOTimedOut,
+ ErrorKind::WriteZero => JoshutoErrorKind::IOWriteZero,
+ ErrorKind::Interrupted => JoshutoErrorKind::IOInterrupted,
+ ErrorKind::UnexpectedEof => JoshutoErrorKind::IOUnexpectedEof,
+ ErrorKind::Other => JoshutoErrorKind::IOOther,
+ _ => JoshutoErrorKind::IOOther,
+ }
+ }
+}
+
+pub struct JoshutoError {
+ _kind: JoshutoErrorKind,
+ _cause: String,
}
-impl KeymapError {
- pub fn new(command: Option<&'static str>, error: String) -> Self {
- KeymapError { command, error }
+impl JoshutoError {
+ pub fn new(_kind: JoshutoErrorKind, _cause: String) -> Self {
+ JoshutoError { _kind, _cause }
+ }
+
+ pub fn kind(&self) -> JoshutoErrorKind {
+ self._kind
+ }
+
+ pub fn cause(&self) -> &str {
+ self._cause.as_str()
}
}
-impl std::fmt::Display for KeymapError {
- fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
- match self.command {
- Some(s) => write!(f, "{}: {}", s, self.error),
- None => write!(f, "{}", self.error),
+impl std::string::ToString for JoshutoError {
+ fn to_string(&self) -> String {
+ self._cause.clone()
+ }
+}
+
+impl std::convert::From<std::io::Error> for JoshutoError {
+ fn from(err: std::io::Error) -> Self {
+ JoshutoError {
+ _kind: JoshutoErrorKind::from(err.kind()),
+ _cause: err.to_string(),
}
}
}
+
+pub type JoshutoResult<T> = Result<T, JoshutoError>;