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
pub use anyhow::{anyhow, bail, Context as AnyContext, Error as AnyError, Result as AnyResult};
use cosmwasm_std::{WasmMsg, WasmQuery};
use thiserror::Error;

#[derive(Debug, Error, PartialEq, Eq)]
pub enum Error {
    #[error("Empty attribute key. Value: {value}")]
    EmptyAttributeKey { value: String },

    #[error("Empty attribute value. Key: {key}")]
    EmptyAttributeValue { key: String },

    #[error("Attribute key starts with reserved prefix _: {0}")]
    ReservedAttributeKey(String),

    #[error("Event type too short: {0}")]
    EventTypeTooShort(String),

    #[error("Unsupported wasm query: {0:?}")]
    UnsupportedWasmQuery(WasmQuery),

    #[error("Unsupported wasm message: {0:?}")]
    UnsupportedWasmMsg(WasmMsg),

    #[error("code id: invalid")]
    InvalidCodeId,

    #[error("code id {0}: no such code")]
    UnregisteredCodeId(u64),

    #[error("Contract with this address already exists: {0}")]
    DuplicatedContractAddress(String),
}

impl Error {
    pub fn empty_attribute_key(value: impl Into<String>) -> Self {
        Self::EmptyAttributeKey {
            value: value.into(),
        }
    }

    pub fn empty_attribute_value(key: impl Into<String>) -> Self {
        Self::EmptyAttributeValue { key: key.into() }
    }

    pub fn reserved_attribute_key(key: impl Into<String>) -> Self {
        Self::ReservedAttributeKey(key.into())
    }

    pub fn event_type_too_short(ty: impl Into<String>) -> Self {
        Self::EventTypeTooShort(ty.into())
    }

    pub fn duplicated_contract_address(address: impl Into<String>) -> Self {
        Self::DuplicatedContractAddress(address.into())
    }
}