use std::fmt::Display;
use candid::CandidType;
use serde::Deserialize;
#[derive(CandidType, Deserialize, Debug, Clone)]
pub struct CanisterCallError {
pub canister_id: crate::identity::CanisterId,
pub method: String,
pub message: std::string::String,
}
impl std::fmt::Display for CanisterCallError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"Canister({}) call({}) failed: {}",
self.canister_id.to_text(),
self.method,
self.message
)
}
}
impl std::error::Error for CanisterCallError {}
impl CanisterCallError {
pub fn new<E: Display>(
canister_id: crate::identity::CanisterId,
method: impl Into<String>,
err: E,
) -> Self {
Self {
canister_id,
method: method.into(),
message: err.to_string(),
}
}
pub fn from<E: Display>(method: impl Into<String>, err: E) -> Self {
Self {
canister_id: crate::identity::CanisterId::management_canister(),
method: method.into(),
message: err.to_string(),
}
}
}
pub type CanisterCallResult<T> = Result<T, CanisterCallError>;
pub use super::codes::{CanisterCodeWasm, CanisterInitArg};