Skip to main content

forest/chain/store/
errors.rs

1// Copyright 2019-2026 ChainSafe Systems
2// SPDX-License-Identifier: Apache-2.0, MIT
3
4use std::{borrow::Cow, fmt::Debug};
5
6use crate::blocks::CreateTipsetError;
7use crate::prelude::*;
8use cid::Error as CidErr;
9use fil_actors_shared::fvm_ipld_amt::Error as AmtErr;
10use fvm_ipld_encoding::Error as EncErr;
11use thiserror::Error;
12use tokio::task::JoinError;
13
14/// Chain error
15#[derive(Debug, Error)]
16pub enum Error {
17    /// Key was not found
18    #[error("Invalid tipset: {0}")]
19    UndefinedKey(String),
20    /// Key not found in database
21    #[error("{0} not found")]
22    NotFound(Cow<'static, str>),
23    /// Error originating constructing blockchain structures
24    #[error(transparent)]
25    Blockchain(#[from] CreateTipsetError),
26    /// Error originating from encoding arbitrary data
27    #[error("{0}")]
28    Encoding(String),
29    /// Error originating from Cid creation
30    #[error(transparent)]
31    Cid(#[from] CidErr),
32    /// Amt error
33    #[error("State error: {0}")]
34    State(Cow<'static, str>),
35    /// Requested height is a null round (no tipset), reported when resolving with
36    /// [`super::index::ResolveNullTipset::Fail`]. The Eth layer translates this into its own
37    /// Lotus-compatible message, so this internal phrasing is intentionally distinct.
38    #[error("null round at epoch {0}")]
39    NullRound(ChainEpoch),
40    #[error("lookback height {lookback_height} is at or after base height {base_height}")]
41    LookbackHeightOverflow {
42        lookback_height: ChainEpoch,
43        base_height: ChainEpoch,
44    },
45    /// Other chain error
46    #[error("{0}")]
47    Other(String),
48}
49
50impl From<EncErr> for Error {
51    fn from(e: EncErr) -> Error {
52        Error::Encoding(e.to_string())
53    }
54}
55
56impl From<AmtErr> for Error {
57    fn from(e: AmtErr) -> Error {
58        Error::state(e.to_string())
59    }
60}
61
62impl From<String> for Error {
63    fn from(e: String) -> Self {
64        Error::Other(e)
65    }
66}
67
68impl From<anyhow::Error> for Error {
69    fn from(e: anyhow::Error) -> Self {
70        Error::Other(format!("{e:#}"))
71    }
72}
73
74impl From<std::io::Error> for Error {
75    fn from(e: std::io::Error) -> Self {
76        Error::Other(e.to_string())
77    }
78}
79
80impl<T> From<flume::SendError<T>> for Error {
81    fn from(e: flume::SendError<T>) -> Self {
82        Error::Other(e.to_string())
83    }
84}
85
86impl From<JoinError> for Error {
87    fn from(e: JoinError) -> Self {
88        Error::Other(format!("failed joining on tokio task: {e}"))
89    }
90}
91
92impl Error {
93    pub fn state(msg: impl Into<Cow<'static, str>>) -> Self {
94        Self::State(msg.into())
95    }
96}