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