From 943bf5951a60c40404acdad553c349849fa3b143 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20HUBERT=20/=20PALO-IT?= Date: Thu, 23 Feb 2023 11:48:50 +0100 Subject: [PATCH] better era error messages --- mithril-aggregator/src/runtime/error.rs | 3 --- mithril-common/src/era/era_reader.rs | 6 +++++- mithril-common/src/era/supported_era.rs | 15 ++++++++++++++- 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/mithril-aggregator/src/runtime/error.rs b/mithril-aggregator/src/runtime/error.rs index c0dc29c0d1c..36c7dc82b1d 100644 --- a/mithril-aggregator/src/runtime/error.rs +++ b/mithril-aggregator/src/runtime/error.rs @@ -60,9 +60,6 @@ pub enum RuntimeError { #[error("signer registration error: {0}")] SignerRegistration(#[from] SignerRegistrationError), - #[error("era not supported: {0}")] - EraNotSupported(String), - #[error("general error: {0}")] General(Box), } diff --git a/mithril-common/src/era/era_reader.rs b/mithril-common/src/era/era_reader.rs index d5cf1c7c43d..20f69a8c257 100644 --- a/mithril-common/src/era/era_reader.rs +++ b/mithril-common/src/era/era_reader.rs @@ -57,6 +57,7 @@ impl EraEpochToken { /// software. pub fn get_current_supported_era(&self) -> Result { SupportedEra::from_str(&self.current_era.name) + .map_err(|_| UnsupportedEraError::new(&self.current_era.name)) } /// Return the [EraMarker] of the current Era. @@ -75,7 +76,10 @@ impl EraEpochToken { /// for upgrade. pub fn get_next_supported_era(&self) -> Result, UnsupportedEraError> { match self.next_era.as_ref() { - Some(marker) => Ok(Some(SupportedEra::from_str(&marker.name)?)), + Some(marker) => Ok(Some( + SupportedEra::from_str(&marker.name) + .map_err(|_| UnsupportedEraError::new(&self.current_era.name))?, + )), None => Ok(None), } } diff --git a/mithril-common/src/era/supported_era.rs b/mithril-common/src/era/supported_era.rs index 25c66f1f65e..c6a2992af00 100644 --- a/mithril-common/src/era/supported_era.rs +++ b/mithril-common/src/era/supported_era.rs @@ -1,9 +1,22 @@ use serde::{Deserialize, Serialize}; use strum::IntoEnumIterator; use strum_macros::{Display, EnumIter, EnumString}; +use thiserror::Error; /// Error related to [SupportedEra] String parsing implementation. -pub type UnsupportedEraError = strum::ParseError; +#[derive(Debug, Error)] +#[error("Unsupported Era '{era}'.")] +pub struct UnsupportedEraError { + era: String, +} + +impl UnsupportedEraError { + pub fn new(era: &str) -> Self { + Self { + era: era.to_owned(), + } + } +} /// The era that the software is running or will run #[derive(