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
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
 * SPDX-License-Identifier: Apache-2.0
 */

use std::error;
use std::fmt;

/// Contract Error is what the contract will return to indicate an error
/// Typically this would be for contract developers to use to mark that a failure
/// has occured, and give information about what that means in the context
/// of their implementation
#[derive(Debug)]
pub struct ContractError {
    msg: String,
    ledger_error: Option<LedgerError>,
}

impl std::convert::From<String> for ContractError {
    fn from(msg: String) -> Self {
        Self {
            msg,
            ledger_error: Option::None,
        }
    }
}

impl std::convert::From<(String,LedgerError)> for ContractError {
    fn from((msg,err): (String, LedgerError)) -> Self {
        Self {
            msg,
            ledger_error: Some(err),
        }
    }
}

impl std::convert::From<LedgerError> for ContractError {
    fn from(ledger_error: LedgerError) -> Self {
        Self {
            msg: "Error caused by LedgerError".to_string(),
            ledger_error: Some(ledger_error),
        }
    }
}

impl fmt::Display for ContractError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match &self.ledger_error {
            Some(le) => write!(f, "{} caused by {}", self.msg, le),
            None => write!(f, "{}", self.msg),
        }
    }
}

impl error::Error for ContractError {
    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
        Some(self)
    }
}

/// Ledger Error is returned by the API calls made against the ledger
/// api, eg, if a state can be found
#[derive(Debug)]
pub struct LedgerError {
    msg: String,
}

impl error::Error for LedgerError {
    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
        Some(self)
    }
}

impl std::convert::From<String> for LedgerError {
    fn from(msg: String) -> Self {
        Self { msg }
    }
}

impl fmt::Display for LedgerError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.msg)
    }
}