agent_tk/
error.rs

1use yaaral::time::Elapsed;
2
3/// Error enum for agent_tk
4#[derive(thiserror::Error, Debug)]
5#[allow(missing_docs)]
6pub enum Error
7{
8  #[error("UTF8 Error: '{0}'")]
9  Utf8Error(#[from] std::str::Utf8Error),
10  #[error("MQTT Connection Error: '{0}'")]
11  #[cfg(feature = "mqtt")]
12  MQTTConnectionError(#[from] mqtt_channel::Error),
13  #[error("MQTT Service Error: '{0}'")]
14  #[cfg(feature = "mqtt")]
15  MQTTServiceError(#[from] mqtt_service::Error),
16  #[error("Capability '{0}' is unknown.")]
17  UnknownCapability(&'static str),
18  #[error("Capability '{0}' already defined.")]
19  DuplicateCapability(&'static str),
20  #[error("An error occured during transport: '{0}'.")]
21  TransportError(String),
22  #[error("Invalid task type: {0}.")]
23  InvalidTaskType(String),
24  #[error("Unknown task type: {0}.")]
25  UnknownTaskType(String),
26  #[error("State '{0}' is unknown.")]
27  UnknownState(&'static str),
28  #[error("State '{0}' already defined.")]
29  DuplicateState(&'static str),
30  #[error("The execution failed: {0}.")]
31  ExecutionFailed(#[from] Box<Error>),
32  #[error("The task '{0}' cannot be executed.")]
33  CannotExecute(String),
34  #[error("No executor.")]
35  NoExecutor(),
36  #[error("Invalid collection: {0}.")]
37  InvalidCollection(String),
38  #[error("Unknown value in collection {0} with key {1}.")]
39  UnknownValue(String, String),
40  #[error("Serialization error: {0}")]
41  JsonSerializationError(#[from] serde_json::Error),
42  #[error("Serialization error: {0}")]
43  CborSerializationError(#[from] ciborium::ser::Error<std::io::Error>),
44  #[error("Deserialization error: {0}")]
45  DeserializationError(String),
46  #[error("Mutex error: {0}")]
47  MutexError(String),
48  #[error("Runtime error: {0}")]
49  Runtime(String),
50  #[error("Invalid UUID: {0} with error {1}")]
51  InvalidUuid(String, String),
52  #[error("Goals cannot be executed.")]
53  CannotExecuteGoals,
54  #[error("Futures spawn error {0}")]
55  FutureSpawnError(<crate::Runtime as yaaral::TaskInterface>::SpawnError),
56  #[error("Future timed out {0:?}")]
57  TimeoutElapsed(<crate::Runtime as yaaral::time::TimeInterface>::Elapsed),
58  #[error("Unimplemented: {0}.")]
59  Unimplemented(&'static str),
60  #[error("InternalError: {0}.")]
61  InternalError(&'static str),
62}
63
64/// Extension trait for mapping spawn error from runtime
65pub trait SpawnErrorResultExt
66{
67  /// Result value
68  type T;
69  /// Map spawn error
70  fn map_spawn_error(self) -> Result<Self::T, Error>;
71}
72
73impl<T> SpawnErrorResultExt for Result<T, <crate::Runtime as yaaral::TaskInterface>::SpawnError>
74{
75  type T = T;
76  fn map_spawn_error(self) -> Result<Self::T, Error>
77  {
78    self.map_err(Error::FutureSpawnError)
79  }
80}
81
82/// Extension trait for mapping elapsed error from runtime
83pub trait ElapsedErrorResultExt
84{
85  /// Result value
86  type T;
87  /// Map elapsed error
88  fn map_elapsed_error(self) -> Result<Self::T, Error>;
89}
90
91impl<T> ElapsedErrorResultExt
92  for Result<T, <crate::Runtime as yaaral::time::TimeInterface>::Elapsed>
93{
94  type T = T;
95  fn map_elapsed_error(self) -> Result<Self::T, Error>
96  {
97    self.map_err(Error::TimeoutElapsed)
98  }
99}
100
101impl<'a, T> From<std::sync::PoisonError<std::sync::MutexGuard<'a, T>>> for Error
102{
103  fn from(value: std::sync::PoisonError<std::sync::MutexGuard<'a, T>>) -> Self
104  {
105    Error::MutexError(value.to_string())
106  }
107}
108
109impl<'a, T> From<std::sync::PoisonError<std::sync::RwLockWriteGuard<'a, T>>> for Error
110{
111  fn from(value: std::sync::PoisonError<std::sync::RwLockWriteGuard<'a, T>>) -> Self
112  {
113    Error::MutexError(value.to_string())
114  }
115}
116
117impl From<yaaral::Infallible> for Error
118{
119  fn from(value: yaaral::Infallible) -> Self
120  {
121    Error::Unimplemented("yaaral::Infallible should never trigger.")
122  }
123}