use crate::conductor::error::ConductorError;
use crate::conductor::interface::error::InterfaceError;
use crate::conductor::CellError;
use crate::core::ribosome::error::RibosomeError;
use crate::core::workflow::error::WorkflowError;
use holo_hash::DnaHash;
use holochain_sqlite::error::DatabaseError;
use holochain_state::source_chain::SourceChainError;
use holochain_state::workspace::WorkspaceError;
use holochain_types::prelude::*;
use holochain_zome_types::cell::CellId;
use mr_bundle::error::MrBundleError;
use serde::de::DeserializeOwned;
use thiserror::Error;
#[derive(Error, Debug)]
pub enum ConductorApiError {
#[error("The Dna for this Cell is not installed in the conductor! DnaHash: {0}")]
DnaMissing(DnaHash),
#[error(
"A Cell attempted to use an CellConductorApi it was not given.\nAPI CellId: {api_cell_id:?}\nInvocation CellId: {call_cell_id:?}"
)]
ZomeCallCellMismatch {
api_cell_id: CellId,
call_cell_id: CellId,
},
#[error("Conductor returned an error while using a ConductorApi: {0:?}")]
ConductorError(#[from] ConductorError),
#[error("Io error while using a Interface Api: {0:?}")]
Io(#[from] std::io::Error),
#[error("Serialization error while using a InterfaceApi: {0:?}")]
SerializationError(#[from] SerializationError),
#[error(transparent)]
DatabaseError(#[from] DatabaseError),
#[error(transparent)]
WorkspaceError(#[from] WorkspaceError),
#[error(transparent)]
WorkflowError(#[from] WorkflowError),
#[error("ZomeError: {0}")]
ZomeError(#[from] holochain_zome_types::zome::error::ZomeError),
#[error("DnaError: {0}")]
DnaError(#[from] holochain_types::dna::DnaError),
#[error("The Dna file path provided was invalid")]
DnaReadError(String),
#[error("KeystoreError: {0}")]
KeystoreError(#[from] holochain_keystore::KeystoreError),
#[error(transparent)]
CellError(#[from] CellError),
#[error(transparent)]
AppError(#[from] AppError),
#[error("An error occurred in the interface: {0:?}")]
InterfaceError(#[from] InterfaceError),
#[error(transparent)]
SourceChainError(#[from] SourceChainError),
#[error(transparent)]
AppBundleError(#[from] AppBundleError),
#[error(transparent)]
MrBundleError(#[from] MrBundleError),
#[error(transparent)]
JsonDumpError(#[from] serde_json::Error),
#[error(transparent)]
StateQueryError(#[from] holochain_state::query::StateQueryError),
#[error(transparent)]
StateMutationError(#[from] holochain_state::mutations::StateMutationError),
#[error(transparent)]
RusqliteError(#[from] rusqlite::Error),
#[error(transparent)]
ChcError(#[from] ChcError),
#[error(transparent)]
RibosomeError(#[from] crate::core::ribosome::error::RibosomeError),
#[error("Other: {0}")]
Other(Box<dyn std::error::Error + Send + Sync>),
}
impl ConductorApiError {
pub fn other(e: impl Into<Box<dyn std::error::Error + Send + Sync>>) -> Self {
Self::Other(e.into())
}
}
impl From<one_err::OneErr> for ConductorApiError {
fn from(e: one_err::OneErr) -> Self {
Self::other(e)
}
}
#[derive(Error, Debug)]
pub enum SerializationError {
#[error(transparent)]
Bytes(#[from] holochain_serialized_bytes::SerializedBytesError),
#[error(transparent)]
Uuid(#[from] uuid::parser::ParseError),
}
pub type ConductorApiResult<T> = Result<T, ConductorApiError>;
pub use holochain_conductor_api::ExternalApiWireError;
impl From<ConductorApiError> for ExternalApiWireError {
fn from(err: ConductorApiError) -> Self {
match err {
ConductorApiError::DnaReadError(e) => ExternalApiWireError::DnaReadError(e),
e => ExternalApiWireError::internal(e),
}
}
}
impl From<SerializationError> for ExternalApiWireError {
fn from(e: SerializationError) -> Self {
ExternalApiWireError::Deserialization(format!("{:?}", e))
}
}
impl From<RibosomeError> for ExternalApiWireError {
fn from(e: RibosomeError) -> Self {
ExternalApiWireError::RibosomeError(e.to_string())
}
}
pub fn zome_call_response_to_conductor_api_result<T: DeserializeOwned + std::fmt::Debug>(
zcr: ZomeCallResponse,
) -> ConductorApiResult<T> {
match zcr {
ZomeCallResponse::Ok(bytes) => Ok(bytes.decode().map_err(SerializationError::from)?),
other => Err(ConductorApiError::other(format!("{:?}", other))),
}
}