Skip to main content

printobserver_core/
error.rs

1//! The one error type core answers its callers with.
2
3use printobserver_printer_api::PrinterError;
4use printobserver_supervisor_api::SupervisorError;
5use printobserver_types::PrintId;
6use printobserver_vision_api::VisionError;
7
8use crate::store::StoreError;
9
10/// Why core could not do what it was asked.
11///
12/// A port's own error is carried rather than flattened into a message, because
13/// a caller that has somewhere to put a failure needs to know which port failed
14/// and how — the store's initial event append in particular, which is the one
15/// failure core cannot record anywhere and so answers to its caller.
16#[derive(Debug, Clone, PartialEq, Eq)]
17pub enum CoreError {
18    /// The store failed, in its own words.
19    Store(StoreError),
20    /// The printer failed, in its own words.
21    Printer(PrinterError),
22    /// The vision port failed, in its own words.
23    Vision(VisionError),
24    /// The supervising agent's harness failed, in its own words.
25    Supervisor(SupervisorError),
26    /// There is no print by that identifier.
27    NoSuchPrint {
28        /// The identifier nothing was found for.
29        print_id: PrintId,
30    },
31    /// A value this crate computed is not representable: an instant past what
32    /// a timestamp holds, or an event payload that would not render as JSON.
33    Unrepresentable {
34        /// What could not be represented.
35        detail: String,
36    },
37}
38
39impl From<printobserver_types::serde_json::Error> for CoreError {
40    fn from(error: printobserver_types::serde_json::Error) -> Self {
41        Self::Unrepresentable {
42            detail: format!("an event payload would not render: {error}"),
43        }
44    }
45}
46
47impl core::fmt::Display for CoreError {
48    fn fmt(&self, formatter: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
49        match self {
50            Self::Store(error) => write!(formatter, "the store failed: {error}"),
51            Self::Printer(error) => write!(formatter, "the printer failed: {error}"),
52            Self::Vision(error) => write!(formatter, "the vision port failed: {error}"),
53            Self::Supervisor(error) => write!(formatter, "the supervisor failed: {error}"),
54            Self::NoSuchPrint { print_id } => write!(formatter, "there is no print {print_id}"),
55            Self::Unrepresentable { detail } => {
56                write!(formatter, "a value is not representable: {detail}")
57            }
58        }
59    }
60}
61
62impl core::error::Error for CoreError {}
63
64impl From<StoreError> for CoreError {
65    fn from(error: StoreError) -> Self {
66        Self::Store(error)
67    }
68}