tycho_execution/encoding/
errors.rs

1use std::{io, str::Utf8Error};
2
3use thiserror::Error;
4use tycho_common::simulation::errors::SimulationError;
5
6/// Represents the outer-level, user-facing errors of the tycho-execution encoding package.
7///
8/// `EncodingError` encompasses all possible errors that can occur in the package,
9/// wrapping lower-level errors in a user-friendly way for easier handling and display.
10/// Variants:
11/// - `InvalidInput`: Indicates that the encoding has failed due to bad input parameters.
12/// - `FatalError`: There is problem with the application setup.
13/// - `RecoverableError`: Indicates that the encoding has failed with a recoverable error. Retrying
14///   at a later time may succeed. It may have failed due to a temporary issue, such as a network
15///   problem.
16#[derive(Error, Debug, PartialEq)]
17pub enum EncodingError {
18    #[error("Invalid input: {0}")]
19    InvalidInput(String),
20    #[error("Fatal error: {0}")]
21    FatalError(String),
22    #[error("Recoverable error: {0}")]
23    RecoverableError(String),
24    #[error("Not implemented: {0}")]
25    NotImplementedError(String),
26}
27
28impl From<io::Error> for EncodingError {
29    fn from(err: io::Error) -> Self {
30        EncodingError::FatalError(err.to_string())
31    }
32}
33
34impl From<serde_json::Error> for EncodingError {
35    fn from(err: serde_json::Error) -> Self {
36        EncodingError::FatalError(err.to_string())
37    }
38}
39
40impl From<Utf8Error> for EncodingError {
41    fn from(err: Utf8Error) -> Self {
42        EncodingError::FatalError(err.to_string())
43    }
44}
45
46impl From<SimulationError> for EncodingError {
47    fn from(err: SimulationError) -> Self {
48        match err {
49            SimulationError::FatalError(err_msg) => EncodingError::FatalError(err_msg),
50            SimulationError::InvalidInput(err_msg, ..) => EncodingError::InvalidInput(err_msg),
51            SimulationError::RecoverableError(error_msg) => {
52                EncodingError::RecoverableError(error_msg)
53            }
54        }
55    }
56}