summaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authorLinus Färnstrand <linus@mullvad.net>2017-02-08 11:49:11 +0100
committerLinus Färnstrand <linus@mullvad.net>2017-02-08 11:51:34 +0100
commit63fd15950cabd10a35c840ba7a8694e33b7e07e1 (patch)
tree3330edd7ed16dfc01c58a5cdc25a24df57ecbc98 /src
parentfa39b2d2354cc6de2aa9b15379b1c45b31b6f822 (diff)
downloadmullvadvpn-63fd15950cabd10a35c840ba7a8694e33b7e07e1.tar.xz
mullvadvpn-63fd15950cabd10a35c840ba7a8694e33b7e07e1.zip
Convert process::monitor module to use error-chain
Diffstat (limited to 'src')
-rw-r--r--src/process/monitor.rs68
1 files changed, 17 insertions, 51 deletions
diff --git a/src/process/monitor.rs b/src/process/monitor.rs
index e8d32a7973..3553f783cf 100644
--- a/src/process/monitor.rs
+++ b/src/process/monitor.rs
@@ -1,11 +1,22 @@
-use std::error::Error;
-use std::fmt;
use std::io;
use std::process::{ChildStdout, ChildStderr};
use std::sync::{Arc, Mutex};
use std::thread;
+error_chain! {
+ errors {
+ /// The transition could not be made because the state machine was not in a state that
+ /// could transition to the desired state.
+ InvalidState {
+ description("Invalid state for desired transition")
+ }
+ }
+ foreign_links {
+ Io(::std::io::Error) #[doc = "The monitor state transition failed because of an IO error"];
+ }
+}
+
/// Trait for objects that represent child processes that `ChildMonitor` can monitor
pub trait MonitoredChild: Clone + Send + 'static {
/// Waits for the child to exit completely, returning if the child exited cleanly or not.
@@ -31,49 +42,6 @@ pub trait ChildSpawner: Send + 'static {
}
-/// Type alias for results of transitions in the `ChildMonitor` state machine.
-pub type TransitionResult<T> = Result<T, TransitionError>;
-
-/// Error type for transitions in the `ChildMonitor` state machine.
-#[derive(Debug)]
-pub enum TransitionError {
- /// The transition could not be made because the state machine was not in a state that could
- /// transition to the desired state.
- InvalidState,
-
- /// The transition failed because of an `io::Error`.
- IoError(io::Error),
-}
-
-impl From<io::Error> for TransitionError {
- fn from(error: io::Error) -> Self {
- TransitionError::IoError(error)
- }
-}
-
-impl fmt::Display for TransitionError {
- fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
- fmt.write_str(self.description())
- }
-}
-
-impl Error for TransitionError {
- fn description(&self) -> &str {
- match *self {
- TransitionError::InvalidState => "Invalid state for desired transition",
- TransitionError::IoError(..) => "Transition failed due to IO error",
- }
- }
-
- fn cause(&self) -> Option<&Error> {
- match *self {
- TransitionError::IoError(ref e) => Some(e),
- _ => None,
- }
- }
-}
-
-
enum State<C: MonitoredChild> {
Stopped,
Running(RunningState<C>),
@@ -104,9 +72,7 @@ impl<S: ChildSpawner> ChildMonitor<S> {
/// Starts the child process and begins to monitor it. `listener` will be called as soon as the
/// child process exits.
- pub fn start<L>(&mut self,
- listener: L)
- -> TransitionResult<(Option<ChildStdout>, Option<ChildStderr>)>
+ pub fn start<L>(&mut self, listener: L) -> Result<(Option<ChildStdout>, Option<ChildStderr>)>
where L: FnMut(bool) + Send + 'static
{
let mut state_lock = self.state.lock().unwrap();
@@ -120,7 +86,7 @@ impl<S: ChildSpawner> ChildMonitor<S> {
});
Ok(io)
} else {
- Err(TransitionError::InvalidState)
+ Err(ErrorKind::InvalidState.into())
}
}
@@ -139,13 +105,13 @@ impl<S: ChildSpawner> ChildMonitor<S> {
}
/// Sends a kill signal to the child process.
- pub fn stop(&self) -> TransitionResult<()> {
+ pub fn stop(&self) -> Result<()> {
let state_lock = self.state.lock().unwrap();
if let State::Running(ref running_state) = *state_lock {
running_state.child.kill()?;
Ok(())
} else {
- Err(TransitionError::InvalidState)
+ Err(ErrorKind::InvalidState.into())
}
}
}