plutus_ledger_api/
error.rs

1use thiserror::Error;
2
3#[derive(Debug, Error)]
4pub enum ConversionError {
5    #[error("ByteString length must be {relation} {expected} but got {got} with value{value_hex}")]
6    InvalidByteStringLength {
7        ctx: String,
8        expected: usize,
9        got: usize,
10        value_hex: String,
11        relation: String,
12    },
13
14    #[error("String cannot be parsed as a hexadecimal value: {value_hex}")]
15    HexDecodeError {
16        value_hex: String,
17        source: hex::FromHexError,
18    },
19
20    #[error(transparent)]
21    ParseError(anyhow::Error),
22}
23
24impl ConversionError {
25    pub fn invalid_bytestring_length(
26        ctx: &str,
27        expected: usize,
28        relation: &str,
29        bytes: &[u8],
30    ) -> Self {
31        ConversionError::InvalidByteStringLength {
32            ctx: ctx.to_string(),
33            expected,
34            got: bytes.len(),
35            relation: relation.to_string(),
36            value_hex: hex::encode(bytes),
37        }
38    }
39
40    pub fn hex_decode_error(err: hex::FromHexError, value_hex: &str) -> Self {
41        ConversionError::HexDecodeError {
42            source: err,
43            value_hex: value_hex.to_string(),
44        }
45    }
46}