use dusk_core::TxPreconditionError;
use dusk_core::transfer::PANIC_NONCE_NOT_READY;
use piecrust::Error;
#[derive(Debug, thiserror::Error)]
pub enum ExecutionError {
#[error("Failed refund: {0}")]
FailedRefund(Error),
#[error("Nonce not ready to be used yet")]
NotReady,
#[error("Precondition error: {0}")]
Precondition(String),
#[error("Unspendable: {0}")]
Unspendable(Error),
}
impl ExecutionError {
pub fn precondition<T: ToString>(msg: T) -> Self {
Self::Precondition(msg.to_string())
}
pub fn from_spend_and_execute(inner: Error) -> Self {
if let Error::Panic(val) = &inner
&& val == PANIC_NONCE_NOT_READY
{
Self::NotReady
} else {
Self::Unspendable(inner)
}
}
pub fn failed_refund(inner: Error) -> Self {
Self::FailedRefund(inner)
}
}
impl From<TxPreconditionError> for ExecutionError {
fn from(err: TxPreconditionError) -> Self {
ExecutionError::Precondition(err.legacy_to_string())
}
}