plutus_ledger_api/
error.rs

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