Skip to main content

network_communicator/
error.rs

1use curl::Error as CurlError;
2use std::io::Error as IOError;
3use std::fmt::{Formatter,Debug,Display,Error as FormatterError};
4use std::error::Error as TraitError;
5use std::convert::From;
6
7/// Errors during starting download manager or processing request.
8pub enum Error<E> {
9	/// Unable to start thread.
10	ThreadStartError { error: IOError },
11
12	/// Curl error - configuring or processing request.
13	/// First parameter - curl error.
14	Curl { error: CurlError },
15
16	/// Error within event-loop.
17	/// First parameter - description.
18	/// Second parameter - debug message.
19	EventLoop { description: String, debug_message: String },
20
21	/// User defined error while Initializing
22	Initialize { error: E },
23}
24
25impl <E> Debug for Error<E> {
26	#[allow(unused)]
27	fn fmt(&self, f: &mut Formatter) -> Result<(), FormatterError> {
28		match self {
29			&Error::ThreadStartError {ref error} => {
30				write!(f, "Unable to start thread: {:?}", error)
31			},
32			&Error::Curl {ref error} => {
33				write!(f, "Curl error: {:?}", error)
34			},
35			&Error::EventLoop {ref description,ref debug_message} => {
36				write!(f, "Event loop error: {}", debug_message)
37			},
38			&Error::Initialize {..} => {
39				write!(f, "Initialize error")
40			},
41		}
42	}
43}
44
45impl <E> Display for Error<E> {
46	fn fmt(&self, f: &mut Formatter) -> Result<(), FormatterError> {
47		write!(f,"{:?}",self)
48	}
49}
50
51impl <E>TraitError for Error<E> {
52	#[allow(unused)]
53	fn description(&self) -> &str {
54		match self {
55			&Error::ThreadStartError {..} => {
56				"Unable to start thread"
57			},
58			&Error::Curl {..} => {
59				"Curl error"
60			},
61			&Error::EventLoop {ref description,ref debug_message} => {
62				&description
63			},
64			&Error::Initialize {..} => {
65				"Initialize error"
66			},
67		}
68	}
69
70    fn cause(&self) -> Option<&TraitError> {
71    	match self {
72			&Error::ThreadStartError {ref error} => {
73				Some(error)
74			},
75			&Error::Curl {ref error} => {
76				Some(error)
77			},
78			&Error::EventLoop {..} => {
79				None
80			},
81			&Error::Initialize {..} => {
82				None
83			},
84		}
85    }
86}
87
88
89impl <E>From<CurlError> for Error<E> {
90	fn from(error: CurlError) -> Self {
91		Error::Curl {
92			error: error
93		}
94	}
95}
96
97
98// impl <E: !CurlError>From<E> for Error<E> where E: !CurlError {
99// 	fn from(error: E) -> Self {
100// 		Error::Initialize {
101// 			error: error
102// 		}
103// 	}
104// }