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    /// Other chain error
35    #[error("{0}")]
36    Other(String),
37}
38
39impl From<EncErr> for Error {
40    fn from(e: EncErr) -> Error {
41        Error::Encoding(e.to_string())
42    }
43}
44
45impl From<AmtErr> for Error {
46    fn from(e: AmtErr) -> Error {
47        Error::state(e.to_string())
48    }
49}
50
51impl From<String> for Error {
52    fn from(e: String) -> Self {
53        Error::Other(e)
54    }
55}
56
57impl From<anyhow::Error> for Error {
58    fn from(e: anyhow::Error) -> Self {
59        Error::Other(format!("{e:#}"))
60    }
61}
62
63impl From<std::io::Error> for Error {
64    fn from(e: std::io::Error) -> Self {
65        Error::Other(e.to_string())
66    }
67}
68
69impl<T> From<flume::SendError<T>> for Error {
70    fn from(e: flume::SendError<T>) -> Self {
71        Error::Other(e.to_string())
72    }
73}
74
75impl From<JoinError> for Error {
76    fn from(e: JoinError) -> Self {
77        Error::Other(format!("failed joining on tokio task: {e}"))
78    }
79}
80
81impl Error {
82    pub fn state(msg: impl Into<Cow<'static, str>>) -> Self {
83        Self::State(msg.into())
84    }
85}