holofuel_types/
error.rs

1//! Custom error types for HoloFuel transactions
2use crate::fraction::Fraction;
3use crate::fuel::Fuel;
4use hdi::prelude::*;
5use std::fmt;
6
7#[derive(thiserror::Error, Debug, Clone)]
8pub enum FuelError {
9    Range(String),                      // Fuel range exceeded
10    FractionOverflow((Fuel, Fraction)), // Overflow in Fuel * Fraction
11    // LimitExceeded((Limit, String, Delta)), // An account Limit was exceeded by Fuel value, by the given transaction Delta
12    // AgentDenied(Limit), // All transactions w/ counterparty are denied
13    Generic(String), // we want to avoid this one if at all possible and add a new error variant instead
14}
15
16// All FuelError display logic is in one place which is here
17impl fmt::Display for FuelError {
18    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
19        match self {
20            FuelError::Range(s) => write!(f, "HoloFuel Range Error: {}", s),
21            FuelError::FractionOverflow((fuel, fraction)) => {
22                write!(f, "HoloFuel overflow in ♓{} * {}", fuel, fraction)
23            }
24            // FuelError::LimitExceeded((limit, total, change)) => write!(
25            //     f,
26            //     "HoloFuel Limit {} exceeded with duration total ♓{}, by tx. ♓{}, on {:?}",
27            //     limit, total, change.1, change.0
28            // ),
29            // FuelError::AgentDenied(limit) => write!(
30            //     f,
31            //     "HoloFuel Limit {} denies any counterparty transaction",
32            //     limit
33            // ),
34            FuelError::Generic(s) => write!(f, "HoloFuel Error: {}", s),
35        }
36    }
37}
38
39pub type FuelResult<T> = Result<T, FuelError>;
40impl From<FuelError> for WasmError {
41    fn from(c: FuelError) -> Self {
42        wasm_error!(WasmErrorInner::Guest(format!("{:?}", c)))
43    }
44}