heim_process/
errors.rs

1use std::error;
2use std::fmt;
3use std::io;
4use std::result;
5
6use heim_common::Error;
7
8use crate::Pid;
9
10/// A specialized `Result` type for process-related routines.
11pub type ProcessResult<T> = result::Result<T, ProcessError>;
12
13/// Error which might happen during the process information fetching.
14#[derive(Debug)]
15pub enum ProcessError {
16    /// Process with this pid does not exists.
17    NoSuchProcess(Pid),
18    /// Might be returned when querying zombie process on Unix systems.
19    ZombieProcess(Pid),
20    /// Not enough permissions to query the process information.
21    AccessDenied(Pid),
22    /// Data loading failure.
23    Load(Error),
24
25    #[doc(hidden)]
26    __Nonexhaustive,
27}
28
29impl fmt::Display for ProcessError {
30    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
31        match self {
32            ProcessError::NoSuchProcess(pid) => {
33                f.write_fmt(format_args!("Process {} does not exists", pid))
34            }
35            ProcessError::ZombieProcess(pid) => {
36                f.write_fmt(format_args!("Process {} is zombie", pid))
37            }
38            ProcessError::AccessDenied(pid) => {
39                f.write_fmt(format_args!("Access denied for process {}", pid))
40            }
41            ProcessError::Load(e) => fmt::Display::fmt(e, f),
42            _ => unreachable!(),
43        }
44    }
45}
46
47impl error::Error for ProcessError {
48    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
49        match self {
50            ProcessError::Load(e) => Some(e),
51            _ => None,
52        }
53    }
54}
55
56impl From<Error> for ProcessError {
57    fn from(e: Error) -> Self {
58        ProcessError::Load(e)
59    }
60}
61
62impl From<io::Error> for ProcessError {
63    fn from(e: io::Error) -> Self {
64        ProcessError::from(Error::from(e))
65    }
66}