prov_cosmwasm_std/errors/
system_error.rs

1use schemars::JsonSchema;
2use serde::{Deserialize, Serialize};
3
4use crate::Binary;
5
6/// SystemError is used for errors inside the VM and is API friendly (i.e. serializable).
7///
8/// This is used on return values for Querier as a nested result: Result<StdResult<T>, SystemError>
9/// The first wrap (SystemError) will trigger if the contract address doesn't exist,
10/// the QueryRequest is malformated, etc. The second wrap will be an error message from
11/// the contract itself.
12///
13/// Such errors are only created by the VM. The error type is defined in the standard library, to ensure
14/// the contract understands the error format without creating a dependency on cosmwasm-vm.
15#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
16#[serde(rename_all = "snake_case")]
17#[non_exhaustive]
18pub enum SystemError {
19    InvalidRequest {
20        error: String,
21        request: Binary,
22    },
23    InvalidResponse {
24        error: String,
25        response: Binary,
26    },
27    NoSuchContract {
28        /// The address that was attempted to query
29        addr: String,
30    },
31    Unknown {},
32    UnsupportedRequest {
33        kind: String,
34    },
35}
36
37impl std::error::Error for SystemError {}
38
39impl std::fmt::Display for SystemError {
40    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
41        match self {
42            SystemError::InvalidRequest { error, request } => write!(
43                f,
44                "Cannot parse request: {} in: {}",
45                error,
46                String::from_utf8_lossy(request)
47            ),
48            SystemError::InvalidResponse { error, response } => write!(
49                f,
50                "Cannot parse response: {} in: {}",
51                error,
52                String::from_utf8_lossy(response)
53            ),
54            SystemError::NoSuchContract { addr } => write!(f, "No such contract: {}", addr),
55            SystemError::Unknown {} => write!(f, "Unknown system error"),
56            SystemError::UnsupportedRequest { kind } => {
57                write!(f, "Unsupported query type: {}", kind)
58            }
59        }
60    }
61}