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

@@ -17,6 +17,7 @@ num-traits = "0.2"
serde = "1.0.103"
serde_derive = "1.0.103"
solana-sdk = { path = "../../sdk", version = "0.22.0" }
thiserror = "1.0"
[dev-dependencies]
solana-runtime = { path = "../../runtime", version = "0.22.0" }

View File

@@ -10,9 +10,11 @@ use solana_sdk::{
pubkey::Pubkey,
system_instruction,
};
use thiserror::Error;
#[derive(Debug, Clone, PartialEq, FromPrimitive, ToPrimitive)]
#[derive(Error, Debug, Clone, PartialEq, FromPrimitive, ToPrimitive)]
pub enum BudgetError {
#[error("destination missing")]
DestinationMissing,
}
@@ -22,19 +24,6 @@ impl<T> DecodeError<T> for BudgetError {
}
}
impl std::fmt::Display for BudgetError {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(
f,
"{}",
match self {
BudgetError::DestinationMissing => "destination missing",
}
)
}
}
impl std::error::Error for BudgetError {}
/// An instruction to progress the smart contract.
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
pub enum BudgetInstruction {

View File

@@ -15,6 +15,7 @@ serde_derive = "1.0.103"
solana-sdk = { path = "../../sdk", version = "0.22.0" }
num-derive = "0.3"
num-traits = "0.2"
thiserror = "1.0"
[dev-dependencies]
solana-runtime = { path = "../../runtime", version = "0.22.0" }

View File

@@ -5,9 +5,11 @@ use solana_sdk::{
pubkey::Pubkey,
system_instruction,
};
use thiserror::Error;
#[derive(Debug, Clone, PartialEq, FromPrimitive, ToPrimitive)]
#[derive(Error, Debug, Clone, PartialEq, FromPrimitive, ToPrimitive)]
pub enum OwnableError {
#[error("incorrect error")]
IncorrectOwner,
}
@@ -17,19 +19,6 @@ impl<T> DecodeError<T> for OwnableError {
}
}
impl std::fmt::Display for OwnableError {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(
f,
"{}",
match self {
OwnableError::IncorrectOwner => "incorrect owner",
}
)
}
}
impl std::error::Error for OwnableError {}
fn initialize_account(account_pubkey: &Pubkey, owner_pubkey: &Pubkey) -> Instruction {
let keys = vec![AccountMeta::new(*account_pubkey, false)];
Instruction::new(crate::id(), &owner_pubkey, keys)

View File

@@ -21,6 +21,7 @@ solana-metrics = { path = "../../metrics", version = "0.22.0" }
solana-sdk = { path = "../../sdk", version = "0.22.0" }
solana-vote-program = { path = "../vote", version = "0.22.0" }
solana-config-program = { path = "../config", version = "0.22.0" }
thiserror = "1.0"
[lib]
crate-type = ["lib", "cdylib"]

View File

@@ -15,33 +15,32 @@ use solana_sdk::{
self, clock::Clock, rent::Rent, rewards::Rewards, stake_history::StakeHistory, 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 StakeError {
#[error("not enough credits to redeem")]
NoCreditsToRedeem,
#[error("lockup has not yet expired")]
LockupInForce,
#[error("stake already deactivated")]
AlreadyDeactivated,
#[error("one re-delegation permitted per epoch")]
TooSoonToRedelegate,
#[error("split amount is more than is staked")]
InsufficientStake,
}
impl<E> DecodeError<E> for StakeError {
fn type_of() -> &'static str {
"StakeError"
}
}
impl std::fmt::Display for StakeError {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
StakeError::NoCreditsToRedeem => write!(f, "not enough credits to redeem"),
StakeError::LockupInForce => write!(f, "lockup has not yet expired"),
StakeError::AlreadyDeactivated => write!(f, "stake already deactivated"),
StakeError::TooSoonToRedelegate => write!(f, "one re-delegation permitted per epoch"),
StakeError::InsufficientStake => write!(f, "split amount is more than is staked"),
}
}
}
impl std::error::Error for StakeError {}
#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
pub enum StakeInstruction {

View File

@@ -18,6 +18,7 @@ serde = "1.0.103"
serde_derive = "1.0.103"
solana-sdk = { path = "../../sdk", version = "0.22.0" }
solana-config-program = { path = "../config", version = "0.22.0" }
thiserror = "1.0"
[dev-dependencies]
solana-runtime = { path = "../../runtime", version = "0.22.0" }

View File

@@ -9,10 +9,14 @@ use solana_sdk::{
pubkey::Pubkey,
system_instruction,
};
use thiserror::Error;
#[derive(Debug, Clone, PartialEq, FromPrimitive)]
#[derive(Error, Debug, Clone, PartialEq, FromPrimitive)]
pub enum VestError {
#[error("destination missing")]
DestinationMissing,
#[error("unauthorized")]
Unauthorized,
}

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`