j4rs/
errors.rs

1// Copyright 2018 astonbitecode
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use std::convert::Infallible;
16use std::env::VarError;
17use std::error::Error;
18use std::ffi::NulError;
19use std::io;
20use std::sync::mpsc::RecvError;
21use std::sync::{PoisonError, TryLockError};
22use std::{fmt, result};
23
24use fs_extra;
25use serde_json;
26
27use futures::channel::oneshot::Canceled;
28
29pub type Result<T> = result::Result<T, J4RsError>;
30
31pub(crate) fn opt_to_res<T>(opt: Option<T>) -> Result<T> {
32    opt.ok_or(J4RsError::RustError(format!(
33        "Option was found None while converting to result"
34    )))
35}
36
37#[allow(unused)]
38pub(crate) fn res_to_opt<T>(res: Result<T>) -> Option<T> {
39    if res.is_err() {
40        None
41    } else {
42        Some(res.unwrap())
43    }
44}
45
46#[derive(Debug, PartialEq, Eq, Clone)]
47pub enum J4RsError {
48    GeneralError(String),
49    JavaError(String),
50    JniError(String),
51    RustError(String),
52    ParseError(String),
53    Timeout,
54}
55
56impl fmt::Display for J4RsError {
57    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
58        match self {
59            &J4RsError::GeneralError(ref message) => write!(f, "{}", message),
60            &J4RsError::JavaError(ref message) => write!(f, "{}", message),
61            &J4RsError::JniError(ref message) => write!(f, "{}", message),
62            &J4RsError::RustError(ref message) => write!(f, "{}", message),
63            &J4RsError::ParseError(ref message) => write!(f, "{}", message),
64            &J4RsError::Timeout => write!(f, "Timeout"),
65        }
66    }
67}
68
69impl Error for J4RsError {
70    fn description(&self) -> &str {
71        match *self {
72            J4RsError::GeneralError(_) => "A general error occured",
73            J4RsError::JavaError(_) => "An error coming from Java occured",
74            J4RsError::JniError(_) => "A JNI error occured",
75            J4RsError::RustError(_) => "An error coming from Rust occured",
76            J4RsError::ParseError(_) => "A parsing error occured",
77            J4RsError::Timeout => "Timeout",
78        }
79    }
80}
81
82impl From<NulError> for J4RsError {
83    fn from(err: NulError) -> J4RsError {
84        J4RsError::JniError(format!("{:?}", err))
85    }
86}
87
88impl From<io::Error> for J4RsError {
89    fn from(err: io::Error) -> J4RsError {
90        J4RsError::GeneralError(format!("{:?}", err))
91    }
92}
93
94impl From<serde_json::Error> for J4RsError {
95    fn from(err: serde_json::Error) -> J4RsError {
96        J4RsError::ParseError(format!("{:?}", err))
97    }
98}
99
100impl From<fs_extra::error::Error> for J4RsError {
101    fn from(err: fs_extra::error::Error) -> J4RsError {
102        J4RsError::GeneralError(format!("{:?}", err))
103    }
104}
105
106impl<T> From<TryLockError<T>> for J4RsError {
107    fn from(err: TryLockError<T>) -> J4RsError {
108        J4RsError::GeneralError(format!("{:?}", err))
109    }
110}
111
112impl<T> From<PoisonError<T>> for J4RsError {
113    fn from(err: PoisonError<T>) -> J4RsError {
114        J4RsError::GeneralError(format!("{:?}", err))
115    }
116}
117
118impl From<Infallible> for J4RsError {
119    fn from(err: Infallible) -> J4RsError {
120        J4RsError::RustError(format!("{:?}", err))
121    }
122}
123
124impl From<RecvError> for J4RsError {
125    fn from(err: RecvError) -> J4RsError {
126        J4RsError::RustError(format!("{:?}", err))
127    }
128}
129
130impl From<VarError> for J4RsError {
131    fn from(err: VarError) -> J4RsError {
132        J4RsError::RustError(format!("{:?}", err))
133    }
134}
135
136impl From<Canceled> for J4RsError {
137    fn from(err: Canceled) -> J4RsError {
138        J4RsError::RustError(format!("{:?}", err))
139    }
140}