1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Copyright 2023 Polyphene.
// SPDX-License-Identifier: Apache-2.0, MIT

/// Kythera lib errors.

#[derive(Debug, thiserror::Error)]
pub enum Error {
    #[error("Constructor execution failed for actor: {name}")]
    Constructor {
        name: String,
        #[source]
        source: Option<Box<dyn std::error::Error + Sync + Send>>,
    },
    #[error("Setup execution failed for actor: {name}")]
    Setup {
        name: String,
        #[source]
        source: Option<Box<dyn std::error::Error + Sync + Send>>,
    },
    #[error("{msg}")]
    MissingActor { msg: String },
    #[error("Could not set Actor: {name} on the BlockStore: {source}")]
    SettingActor {
        name: String,
        #[source]
        source: Box<dyn std::error::Error + Sync + Send>,
    },
    #[error("{msg}")]
    StateTree { msg: String },
    #[error("{msg}")]
    Tester {
        msg: String,
        #[source]
        source: Option<Box<Error>>,
    },
    #[error("{msg}")]
    Validator {
        msg: String,
        #[source]
        source: Box<dyn std::error::Error + Sync + Send>,
    },
}

/// Helper trait for adding custom messages to inner Fvm errors.
pub trait WrapFVMError<T> {
    /// Wrap the source `Error` with an `Error::SettingActor`.
    fn setting_err(self, name: &str) -> Result<T, Error>;

    /// Wrap the source `Error` with an `Error::Validator`.
    fn validator_err(self, msg: &str) -> Result<T, Error>;
}

impl<T, E> WrapFVMError<T> for Result<T, E>
where
    E: Into<Box<dyn std::error::Error + Send + Sync + 'static>>,
{
    fn setting_err(self, name: &str) -> Result<T, Error> {
        self.map_err(|err| Error::SettingActor {
            name: name.into(),
            source: err.into(),
        })
    }

    fn validator_err(self, msg: &str) -> Result<T, Error> {
        self.map_err(|err| Error::Validator {
            msg: msg.into(),
            source: err.into(),
        })
    }
}