Skip to main content

two_devices_one_seat/
error.rs

1//! What a run of the story can fail with.
2
3use core::convert::Infallible;
4use std::path::PathBuf;
5
6use happenstance::{CodecError, CommandError, InvalidQuery, InvalidTag, ProjectionError};
7use happenstance_sqlite::event_store::{SqliteEventStore, SqliteEventStoreError};
8use happenstance_sqlite::projection_store::SqliteProjectionStoreError;
9use happenstance_sync::RemoteSyncPeer;
10use happenstance_sync::runner::{Failure, StepFailure};
11
12use crate::domain::{Device, Place, Refusal};
13use crate::rule::RuleError;
14use crate::uplink::{CentralError, Uplink};
15
16/// What one step failure looks like for a device: its peer is the uplink to
17/// central, and its own log is a SQLite file. The same type whichever rule
18/// central mounts, since every rule's error is a [`RuleError`].
19pub type DeviceStep = StepFailure<RemoteSyncPeer<Uplink>, SqliteEventStore>;
20
21/// What a run of the story can fail with. Every variant names the place or
22/// file it concerns.
23///
24/// The library errors it carries are boxed where they are large, so every
25/// `Result` in this crate stays a pointer wide on its error side; the box
26/// costs one allocation on a path that is already failing.
27#[derive(Debug, thiserror::Error)]
28pub enum StoryError {
29    /// The show's name is not a valid tag value.
30    #[error("the show's name")]
31    Show(#[source] InvalidTag),
32    /// A file's event store did not open.
33    #[error("opening the event store in {file}")]
34    Open {
35        /// The file.
36        file: PathBuf,
37        /// The adapter's error.
38        #[source]
39        source: SqliteEventStoreError,
40    },
41    /// A file's projection store did not open.
42    #[error("opening the projection store in {file}")]
43    OpenModel {
44        /// The file.
45        file: PathBuf,
46        /// The adapter's error.
47        #[source]
48        source: SqliteProjectionStoreError,
49    },
50    /// The application's own read-model table could not be made or read.
51    #[error("the read model in {file}")]
52    ReadModel {
53        /// The file.
54        file: PathBuf,
55        /// The driver's error.
56        #[source]
57        source: rusqlite::Error,
58    },
59    /// A sale did not commit: refused, or the store failed.
60    #[error("{place} could not sell")]
61    Sell {
62        /// Where the sale was attempted.
63        place: Place,
64        /// Why, the refusal included.
65        #[source]
66        source: Box<CommandError<SqliteEventStoreError, Refusal>>,
67    },
68    /// A sale's decision committed and appended nothing. `sell_seat` always
69    /// decides one event or refuses, so this is a broken decision, reported
70    /// rather than assumed away.
71    #[error("{place} decided a sale and appended nothing")]
72    NothingSold {
73        /// Where.
74        place: Place,
75    },
76    /// The show could not be scheduled.
77    #[error("central could not schedule the show")]
78    Schedule(#[source] Box<CommandError<CentralError, Infallible>>),
79    /// Central's rule could not judge what it was asked about.
80    #[error("central could not keep its rule")]
81    Adjudicate(#[source] RuleError),
82    /// A sync step failed.
83    #[error("{device} could not sync")]
84    Sync {
85        /// The device.
86        device: Device,
87        /// The step's failure, with its phase and class.
88        #[source]
89        source: Box<DeviceStep>,
90    },
91    /// A sync took every step it was allowed and still was not synced.
92    #[error("{device} was not synced after {steps} steps")]
93    NotSynced {
94        /// The device.
95        device: Device,
96        /// The steps it took.
97        steps: usize,
98    },
99    /// The show's query could not be derived.
100    #[error("the show's query")]
101    Query(#[from] InvalidQuery),
102    /// A log could not be read.
103    #[error("reading the log at {place}")]
104    Read {
105        /// Where.
106        place: Place,
107        /// The adapter's error.
108        #[source]
109        source: SqliteEventStoreError,
110    },
111    /// An event in a log did not decode.
112    #[error("decoding the log at {place}")]
113    Decode {
114        /// Where.
115        place: Place,
116        /// The codec's error.
117        #[source]
118        source: CodecError,
119    },
120    /// A read model could not be brought up to date.
121    #[error("projecting at {place}")]
122    Project {
123        /// Where.
124        place: Place,
125        /// The runner's error.
126        #[source]
127        source: Box<
128            ProjectionError<
129                SqliteEventStoreError,
130                SqliteProjectionStoreError,
131                SqliteProjectionStoreError,
132            >,
133        >,
134    },
135    /// Something the story claims did not happen: the story stops rather than
136    /// print a transcript that is not true.
137    #[error("the story's claim failed: {claim}")]
138    Contradicted {
139        /// The claim, as the transcript would have made it.
140        claim: &'static str,
141    },
142    /// The transcript could not be written.
143    #[error("writing the story")]
144    Write(#[from] std::io::Error),
145}
146
147impl StoryError {
148    /// The decision's own refusal, if a sale was refused.
149    #[must_use]
150    pub fn refusal(&self) -> Option<&Refusal> {
151        match self {
152            Self::Sell { source, .. } => match source.as_ref() {
153                CommandError::Refused(refusal) => Some(refusal),
154                _ => None,
155            },
156            _ => None,
157        }
158    }
159
160    /// The phase and class of a failed sync step, if this is one.
161    #[must_use]
162    pub fn failure(&self) -> Option<Failure> {
163        match self {
164            Self::Sync { source, .. } => Some(source.failure()),
165            _ => None,
166        }
167    }
168}