1use failure::*;
2use std::io;
3
4use std::result;
5use std::sync::mpsc::{RecvError, SendError};
6
7pub type Result<T> = result::Result<T, ArtilleryError>;
9
10#[derive(Fail, Debug)]
11pub enum ArtilleryError {
12 #[fail(display = "Artillery :: Orphan Node Error: {}", _0)]
14 OrphanNode(String),
15 #[fail(display = "Artillery :: I/O error occurred: {}", _0)]
16 Io(io::Error),
17 #[fail(display = "Artillery :: Cluster Message Decode Error: {}", _0)]
18 ClusterMessageDecode(String),
19 #[fail(display = "Artillery :: Message Send Error: {}", _0)]
20 Send(String),
21 #[fail(display = "Artillery :: Message Receive Error: {}", _0)]
22 Receive(String),
23 #[fail(display = "Artillery :: Unexpected Error: {}", _0)]
24 Unexpected(String),
25 #[fail(display = "Artillery :: Decoding Error: {}", _0)]
26 Decoding(String),
27 #[fail(display = "Artillery :: Numeric Cast Error: {}", _0)]
28 NumericCast(String),
29}
30
31impl From<io::Error> for ArtilleryError {
32 fn from(e: io::Error) -> Self {
33 ArtilleryError::Io(e)
34 }
35}
36
37impl From<serde_json::error::Error> for ArtilleryError {
38 fn from(e: serde_json::error::Error) -> Self {
39 ArtilleryError::ClusterMessageDecode(e.to_string())
40 }
41}
42
43impl<T> From<std::sync::mpsc::SendError<T>> for ArtilleryError {
44 fn from(e: SendError<T>) -> Self {
45 ArtilleryError::Send(e.to_string())
46 }
47}
48
49impl From<std::sync::mpsc::RecvError> for ArtilleryError {
50 fn from(e: RecvError) -> Self {
51 ArtilleryError::Receive(e.to_string())
52 }
53}
54
55impl From<std::str::Utf8Error> for ArtilleryError {
56 fn from(e: std::str::Utf8Error) -> Self {
57 ArtilleryError::Decoding(e.to_string())
58 }
59}
60
61impl From<std::num::TryFromIntError> for ArtilleryError {
62 fn from(e: std::num::TryFromIntError) -> Self {
63 ArtilleryError::NumericCast(e.to_string())
64 }
65}
66
67#[macro_export]
68macro_rules! bail {
69 ($kind:expr, $e:expr) => {
70 return Err($kind($e.to_owned()));
71 };
72 ($kind:expr, $fmt:expr, $($arg:tt)+) => {
73 return Err($kind(format!($fmt, $($arg)+).to_owned()));
74 };
75}