chiark / gitweb /
Provide AggregatedIE
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Wed, 17 Feb 2021 22:27:38 +0000 (22:27 +0000)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Wed, 17 Feb 2021 22:53:41 +0000 (22:53 +0000)
Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
src/error.rs

index 3244cde12a0b29488774dbd9ddc552ef86d40a6c..68a2b58817b603ecd4bac4a725b1f66a9007dca7 100644 (file)
@@ -53,6 +53,8 @@ pub enum InternalError {
   Anyhow(#[from] anyhow::Error),
   #[error("Game contains only partial data for player, or account missing")]
   PartialPlayerData,
+  #[error("Multiple errors occurred where only one could be reported")]
+  Aggregated,
 }
 
 #[derive(Error)]
@@ -154,6 +156,30 @@ pub type StartupError = anyhow::Error;
 
 pub use OnlineError::{NoClient,NoPlayer};
 
+pub enum AggregatedIE {
+  Ok,
+  One(InternalError),
+  Many,
+}
+
+impl AggregatedIE {
+  pub fn new() -> Self { Self::Ok }
+  pub fn record(&mut self, e: InternalError) {
+    error!("error occurred in aggregating-errors contest: {}", &e);
+    *self = match self {
+      Self::Ok => Self::One(e),
+      _ => Self::Many,
+    }
+  }
+  pub fn ok(self) -> Result<(),IE> {
+    match self {
+      Self::Ok => Ok(()),
+      Self::One(e) => Err(e),
+      Self::Many => Err(IE::Aggregated),
+    }
+  }
+}
+
 #[derive(Error,Debug)]
 pub enum InstanceLockError {
   GameCorrupted,