Skip to main content

docbox_management_interface/
error.rs

1use std::{
2    error::Error,
3    fmt::{Debug, Display},
4};
5
6#[derive(Debug, thiserror::Error)]
7pub enum ManagementError {
8    /// Error indicating the target service behind the management layer does
9    /// not support the requested operation (i.e long running operation attempt on a serverless management runner)
10    #[error("the target docbox service does not support the requested management operation")]
11    UnsupportedOperation,
12
13    /// Failed to serialize the response message from a dynamically handled command
14    #[error("failed to serialize command response")]
15    SerializeResponse(serde_json::Error),
16
17    /// Any other service specific error
18    #[error(transparent)]
19    Service(#[from] DynServiceError),
20}
21
22pub trait DocboxServiceError: Error + Send + Sync + 'static {
23    /// Provides the reason message to use in the error response
24    fn reason(&self) -> String {
25        self.to_string()
26    }
27
28    /// Provides the full type name for the actual error type thats been
29    /// erased by dynamic typing (For better error source clarity)
30    fn type_name(&self) -> &str {
31        std::any::type_name::<Self>()
32    }
33}
34
35pub struct DynServiceError {
36    inner: Box<dyn DocboxServiceError>,
37}
38
39impl Debug for DynServiceError {
40    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
41        f.debug_tuple(self.inner.type_name())
42            .field(&self.inner)
43            .finish()
44    }
45}
46
47impl Display for DynServiceError {
48    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
49        Display::fmt(&self.inner, f)
50    }
51}
52
53impl Error for DynServiceError {
54    fn cause(&self) -> Option<&dyn Error> {
55        Some(self.inner.as_ref())
56    }
57}
58
59impl<E> From<E> for DynServiceError
60where
61    E: DocboxServiceError,
62{
63    fn from(value: E) -> Self {
64        DynServiceError {
65            inner: Box::new(value),
66        }
67    }
68}