threadpool_executor/
error.rs1use std::{
2 any::Any,
3 fmt::{Debug, Display, Result},
4};
5
6#[derive(Debug)]
7pub enum ErrorKind {
8 PoolEnded,
9 ResultAlreadyTaken,
10 TimeOut,
11 TaskRejected,
12 TaskCancelled,
13 TaskRunning,
14 Panic,
15}
16
17#[derive(Debug)]
18pub struct ExecutorError {
19 kind: ErrorKind,
20 message: String,
21 cause: Option<Box<dyn Any + Send>>,
22}
23
24impl Display for ErrorKind {
25 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result {
26 let msg = match self {
27 ErrorKind::PoolEnded => "PoolEnded",
28 ErrorKind::ResultAlreadyTaken => "ResultAlreadyTaken",
29 ErrorKind::TaskRejected => "TaskRejected",
30 ErrorKind::TaskCancelled => "TaskCancelled",
31 ErrorKind::TaskRunning => "TaskStarted",
32 ErrorKind::TimeOut => "TimeOUt",
33 ErrorKind::Panic => "Panic",
34 };
35 write!(f, "{:?}", msg)
36 }
37}
38
39impl ExecutorError {
40 pub(crate) fn new(kind: ErrorKind, message: String) -> ExecutorError {
41 ExecutorError {
42 kind,
43 message,
44 cause: None,
45 }
46 }
47
48 pub(crate) fn with_cause(
49 kind: ErrorKind,
50 message: String,
51 cause: Box<dyn Any + Send>,
52 ) -> ExecutorError {
53 ExecutorError {
54 kind,
55 message,
56 cause: Some(cause),
57 }
58 }
59
60 pub fn kind(&self) -> &ErrorKind {
61 &self.kind
62 }
63
64 pub fn message(&self) -> &String {
65 &self.message
66 }
67
68 pub fn cause(&self) -> &Option<Box<dyn Any + Send>> {
69 &self.cause
70 }
71}
72
73impl Display for ExecutorError {
74 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result {
75 write!(
76 f,
77 "ExecutorError [kind: {}, message: {}]",
78 self.kind, self.message
79 )
80 }
81}