jmx/
error.rs

1use std::fmt;
2
3use failure::Backtrace;
4use failure::Context;
5use failure::Fail;
6use j4rs::errors::J4RsError;
7
8
9/// Error information returned by functions in case of errors.
10#[derive(Debug)]
11pub struct Error(Context<ErrorKind>);
12
13impl Error {
14    pub fn kind(&self) -> &ErrorKind {
15        self.0.get_context()
16    }
17}
18
19impl Fail for Error {
20    fn cause(&self) -> Option<&dyn Fail> {
21        self.0.cause()
22    }
23
24    fn backtrace(&self) -> Option<&Backtrace> {
25        self.0.backtrace()
26    }
27}
28
29impl fmt::Display for Error {
30    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
31        fmt::Display::fmt(&self.0, f)
32    }
33}
34
35impl From<Context<ErrorKind>> for Error {
36    fn from(inner: Context<ErrorKind>) -> Error {
37        Error(inner)
38    }
39}
40
41impl From<ErrorKind> for Error {
42    fn from(kind: ErrorKind) -> Error {
43        Error(Context::new(kind))
44    }
45}
46
47impl From<J4RsError> for Error {
48    fn from(e: J4RsError) -> Error {
49        Error(Context::new(ErrorKind::J4RsError(e)))
50    }
51}
52
53
54/// Exhaustive list of possible errors emitted by this crate.
55#[derive(Debug, Fail)]
56pub enum ErrorKind {
57    #[fail(display = "could not cast java object to class '{}'", _0)]
58    JavaCast(String),
59
60    #[fail(display = "could not clone java object")]
61    JavaClone,
62
63    #[fail(display = "could not instantiate java object with class '{}'", _0)]
64    JavaCreateInstance(&'static str),
65
66    #[fail(display = "could not invoke instance method '{}.{}'", _0, _1)]
67    JavaInvoke(String, &'static str),
68
69    #[fail(display = "j4rs has thrown an error '{}'", _0)]
70    J4RsError(J4RsError),
71
72    #[fail(display = "could not invoke static method '{}.{}'", _0, _1)]
73    JavaInvokeStatic(&'static str, &'static str),
74
75    #[fail(display = "could not initialise JVM instance")]
76    JvmInit,
77
78    #[fail(display = "the JMX client is not connected")]
79    NotConnected,
80
81    #[fail(display = "could not cast java object to rust '{}' type", _0)]
82    RustCast(&'static str),
83
84    #[cfg(feature = "thread-support")]
85    #[fail(display = "could not decode mbean attribute value")]
86    WorkerDecode,
87
88    #[cfg(feature = "thread-support")]
89    #[fail(display = "background worker did not send a response")]
90    WorkerNoResponse,
91
92    #[cfg(feature = "thread-support")]
93    #[fail(display = "could not send request to background worker")]
94    WorkerNoSend,
95
96    #[cfg(feature = "thread-support")]
97    #[fail(display = "could not spawn background worker thread")]
98    WorkerSpawn,
99}
100
101
102/// Short form alias for functions returning `Error`s.
103pub type Result<T> = ::std::result::Result<T, Error>;