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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
// Copyright (c) 2026 pykep-rust contributors
// SPDX-License-Identifier: MPL-2.0
//! Error types shared by the numerical core.
use core::fmt;
/// Error returned by a pykep numerical operation.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum PykepError {
/// A finite input lies outside the mathematical domain.
InvalidInput {
/// Public parameter name.
parameter: &'static str,
/// Human-readable domain requirement.
reason: String,
},
/// A public input is NaN or infinite.
NonFiniteInput {
/// Public parameter name.
parameter: &'static str,
},
/// The requested operation is undefined for the supplied geometry.
SingularGeometry {
/// Operation that detected the singularity.
operation: &'static str,
},
/// An iterative algorithm exhausted its iteration limit.
ConvergenceFailure {
/// Iterative operation that failed.
operation: &'static str,
/// Number of iterations attempted.
iterations: usize,
},
/// A dynamically sized value has an invalid length.
DimensionMismatch {
/// Required number of scalar values.
expected: usize,
/// Supplied number of scalar values.
actual: usize,
},
/// An ephemeris or backend does not implement a requested capability.
UnsupportedCapability {
/// Provider or backend name.
provider: String,
/// Unsupported capability name.
capability: &'static str,
},
/// A finite input produced a value outside the binary64 range.
NumericalOverflow {
/// Numerical operation that overflowed.
operation: &'static str,
},
/// A numerical integrator could not complete a propagation.
IntegrationFailure {
/// Dynamics model being integrated.
model: &'static str,
/// Human-readable failure context.
reason: String,
},
/// A required embedded or external dataset is unavailable or corrupt.
DataUnavailable {
/// Stable dataset name.
dataset: &'static str,
},
}
impl fmt::Display for PykepError {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::InvalidInput { parameter, reason } => {
write!(formatter, "invalid {parameter}: {reason}")
}
Self::NonFiniteInput { parameter } => {
write!(formatter, "{parameter} must be finite")
}
Self::SingularGeometry { operation } => {
write!(formatter, "singular geometry in {operation}")
}
Self::ConvergenceFailure {
operation,
iterations,
} => write!(
formatter,
"{operation} did not converge after {iterations} iterations"
),
Self::DimensionMismatch { expected, actual } => {
write!(
formatter,
"dimension mismatch: expected {expected}, got {actual}"
)
}
Self::UnsupportedCapability {
provider,
capability,
} => write!(formatter, "{provider} does not support {capability}"),
Self::NumericalOverflow { operation } => {
write!(formatter, "floating-point overflow in {operation}")
}
Self::IntegrationFailure { model, reason } => {
write!(formatter, "integration of {model} failed: {reason}")
}
Self::DataUnavailable { dataset } => {
write!(formatter, "required dataset is unavailable: {dataset}")
}
}
}
}
impl std::error::Error for PykepError {}
/// Result returned by fallible pykep operations.
pub type Result<T> = core::result::Result<T, PykepError>;
pub(crate) fn ensure_finite(parameter: &'static str, value: f64) -> Result<()> {
if value.is_finite() {
Ok(())
} else {
Err(PykepError::NonFiniteInput { parameter })
}
}
pub(crate) fn ensure_finite_values(names_and_values: &[(&'static str, f64)]) -> Result<()> {
for &(name, value) in names_and_values {
ensure_finite(name, value)?;
}
Ok(())
}
pub(crate) fn ensure_finite_output(operation: &'static str, value: f64) -> Result<f64> {
if value.is_finite() {
Ok(value)
} else {
Err(PykepError::NumericalOverflow { operation })
}
}
#[cfg(test)]
mod tests {
use super::PykepError;
#[test]
fn errors_have_stable_useful_messages() {
let cases = [
(
PykepError::InvalidInput {
parameter: "x",
reason: "must be positive".into(),
},
"invalid x: must be positive",
),
(
PykepError::NonFiniteInput { parameter: "x" },
"x must be finite",
),
(
PykepError::SingularGeometry { operation: "orbit" },
"singular geometry in orbit",
),
(
PykepError::ConvergenceFailure {
operation: "solver",
iterations: 10,
},
"solver did not converge after 10 iterations",
),
(
PykepError::DimensionMismatch {
expected: 3,
actual: 2,
},
"dimension mismatch: expected 3, got 2",
),
(
PykepError::UnsupportedCapability {
provider: "minimal".into(),
capability: "acceleration",
},
"minimal does not support acceleration",
),
(
PykepError::NumericalOverflow {
operation: "multiply",
},
"floating-point overflow in multiply",
),
(
PykepError::IntegrationFailure {
model: "model",
reason: "step limit".into(),
},
"integration of model failed: step limit",
),
(
PykepError::DataUnavailable { dataset: "series" },
"required dataset is unavailable: series",
),
];
for (error, expected) in cases {
assert_eq!(error.to_string(), expected);
}
}
}