-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.rs
More file actions
141 lines (126 loc) · 3.65 KB
/
Copy patherrors.rs
File metadata and controls
141 lines (126 loc) · 3.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
use polars::error::PolarsError;
use std::error::Error;
use std::fmt;
use std::io::Error as IoError;
use crate::fits::FitsReadError;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FitsErrorKind {
NotFound,
ValidationFailed,
Unsupported,
Io,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Retryable {
Permanent,
Temporary,
}
#[derive(Debug)]
pub struct FitsError {
pub kind: FitsErrorKind,
pub retryable: Retryable,
pub message: String,
pub context: Vec<(String, String)>,
pub source: Option<Box<dyn Error + Send + Sync>>,
}
impl FitsError {
pub fn with_context(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
self.context.push((key.into(), value.into()));
self
}
pub fn not_found(message: impl Into<String>) -> Self {
Self {
kind: FitsErrorKind::NotFound,
retryable: Retryable::Permanent,
message: message.into(),
context: Vec::new(),
source: None,
}
}
pub fn validation(message: impl Into<String>) -> Self {
Self {
kind: FitsErrorKind::ValidationFailed,
retryable: Retryable::Permanent,
message: message.into(),
context: Vec::new(),
source: None,
}
}
pub fn unsupported(message: impl Into<String>) -> Self {
Self {
kind: FitsErrorKind::Unsupported,
retryable: Retryable::Permanent,
message: message.into(),
context: Vec::new(),
source: None,
}
}
pub fn io(message: impl Into<String>, source: IoError) -> Self {
Self {
kind: FitsErrorKind::Io,
retryable: Retryable::Temporary,
message: message.into(),
context: Vec::new(),
source: Some(Box::new(source)),
}
}
pub fn polars(message: impl Into<String>, source: PolarsError) -> Self {
Self {
kind: FitsErrorKind::ValidationFailed,
retryable: Retryable::Permanent,
message: message.into(),
context: Vec::new(),
source: Some(Box::new(source)),
}
}
}
impl fmt::Display for FitsError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"[FitsError] kind={:?} retryable={:?}",
self.kind, self.retryable
)?;
for (k, v) in &self.context {
write!(f, " {}={}", k, v)?;
}
write!(f, " message={}", self.message)?;
if let Some(src) = &self.source {
write!(f, " source={}", src)?;
}
Ok(())
}
}
impl Error for FitsError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
self.source
.as_ref()
.map(|b| b.as_ref() as &(dyn Error + 'static))
}
}
impl From<IoError> for FitsError {
fn from(e: IoError) -> Self {
Self::io("IO error", e)
}
}
impl From<PolarsError> for FitsError {
fn from(e: PolarsError) -> Self {
Self::polars("Polars error", e)
}
}
impl From<FitsReadError> for FitsError {
fn from(e: FitsReadError) -> Self {
let (kind, retryable) = match &e {
FitsReadError::Io(_) => (FitsErrorKind::Io, Retryable::Temporary),
FitsReadError::Parse(_) => (FitsErrorKind::ValidationFailed, Retryable::Permanent),
FitsReadError::Unsupported(_) => (FitsErrorKind::Unsupported, Retryable::Permanent),
};
Self {
kind,
retryable,
message: e.to_string(),
context: Vec::new(),
source: Some(Box::new(e)),
}
}
}