Migrate to thiserror (#7177)

* Migrate to thiserror

* Discourage the use of other modules' Result alias

`io::Result` set a bad precedent. Don't import other `Result`
aliases.
This commit is contained in:
Greg Fitzgerald
2019-12-02 15:42:05 -07:00
committed by GitHub
parent f9df17d8d0
commit 6796b08909
20 changed files with 122 additions and 146 deletions

View File

@@ -18,6 +18,7 @@ serde_derive = "1.0.103"
solana-logger = { path = "../../logger", version = "0.22.0" }
solana-metrics = { path = "../../metrics", version = "0.22.0" }
solana-sdk = { path = "../../sdk", version = "0.22.0" }
thiserror = "1.0"
[lib]
crate-type = ["lib", "cdylib"]

View File

@@ -17,13 +17,21 @@ use solana_sdk::{
system_instruction,
sysvar::{self, clock::Clock, slot_hashes::SlotHashes, Sysvar},
};
use thiserror::Error;
/// Reasons the stake might have had an error
#[derive(Debug, Clone, PartialEq, FromPrimitive, ToPrimitive)]
#[derive(Error, Debug, Clone, PartialEq, FromPrimitive, ToPrimitive)]
pub enum VoteError {
#[error("vote already recorded or not in slot hashes history")]
VoteTooOld,
#[error("vote slots do not match bank history")]
SlotsMismatch,
#[error("vote hash does not match bank hash")]
SlotHashMismatch,
#[error("vote has no slots, invalid")]
EmptySlots,
}
impl<E> DecodeError<E> for VoteError {
@@ -32,22 +40,6 @@ impl<E> DecodeError<E> for VoteError {
}
}
impl std::fmt::Display for VoteError {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(
f,
"{}",
match self {
VoteError::VoteTooOld => "vote already recorded or not in slot hashes history",
VoteError::SlotsMismatch => "vote slots do not match bank history",
VoteError::SlotHashMismatch => "vote hash does not match bank hash",
VoteError::EmptySlots => "vote has no slots, invalid",
}
)
}
}
impl std::error::Error for VoteError {}
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
pub enum VoteInstruction {
/// Initialize the VoteState for this `vote account`