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
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
//! This file contains defitions for Zome errors and also Zome Results.

use crate::holochain_core_types::error::{HolochainError, RibosomeErrorCode};

use crate::holochain_persistence_api::error::PersistenceError;
use holochain_json_api::{error::JsonError, json::JsonString};
use holochain_json_derive::DefaultJson;
use serde_derive::{Deserialize, Serialize};

use holochain_wasm_utils::memory::allocation::AllocationError;
use std::{error::Error, fmt};

/// Error for DNA developers to use in their Zome code.
/// This does not have to be sent back to Ribosome unless its an InternalError.
#[derive(Debug, Serialize, Deserialize, PartialEq, DefaultJson, Clone)]
pub enum ZomeApiError {
    Internal(String),
    FunctionNotImplemented,
    HashNotFound,
    ValidationFailed(String),
    Timeout(String),
}

impl From<ZomeApiError> for HolochainError {
    fn from(zome_api_error: ZomeApiError) -> Self {
        match zome_api_error {
            ZomeApiError::ValidationFailed(s) => HolochainError::ValidationFailed(s),
            ZomeApiError::Timeout(s) => HolochainError::Timeout(s),
            _ => HolochainError::RibosomeFailed(zome_api_error.to_string()),
        }
    }
}

impl From<ZomeApiError> for String {
    fn from(zome_api_error: ZomeApiError) -> Self {
        zome_api_error.to_string()
    }
}

impl From<HolochainError> for ZomeApiError {
    fn from(holochain_error: HolochainError) -> Self {
        match holochain_error {
            HolochainError::ValidationFailed(s) => ZomeApiError::ValidationFailed(s),
            HolochainError::Timeout(s) => ZomeApiError::Timeout(s),
            _ => ZomeApiError::Internal(holochain_error.to_string()),
        }
    }
}

impl From<PersistenceError> for ZomeApiError {
    fn from(persistence_error: PersistenceError) -> Self {
        let holochain_error: HolochainError = persistence_error.into();
        holochain_error.into()
    }
}

impl From<JsonError> for ZomeApiError {
    fn from(json_error: JsonError) -> Self {
        let holochain_error: HolochainError = json_error.into();
        holochain_error.into()
    }
}

impl From<!> for ZomeApiError {
    fn from(_: !) -> Self {
        unreachable!();
    }
}

impl From<String> for ZomeApiError {
    fn from(s: String) -> ZomeApiError {
        ZomeApiError::Internal(s)
    }
}

impl From<RibosomeErrorCode> for ZomeApiError {
    fn from(ribosome_error_code: RibosomeErrorCode) -> ZomeApiError {
        ZomeApiError::from(ribosome_error_code.to_string())
    }
}

impl From<AllocationError> for ZomeApiError {
    fn from(allocation_error: AllocationError) -> ZomeApiError {
        match allocation_error {
            AllocationError::OutOfBounds => {
                ZomeApiError::Internal("Allocation out of bounds".into())
            }
            AllocationError::ZeroLength => ZomeApiError::Internal("Allocation zero length".into()),
            AllocationError::BadStackAlignment => {
                ZomeApiError::Internal("Allocation out of alignment with stack".into())
            }
            AllocationError::Serialization => {
                ZomeApiError::Internal("Allocation serialization failure".into())
            }
        }
    }
}

impl Error for ZomeApiError {}

impl fmt::Display for ZomeApiError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            ZomeApiError::Internal(msg) => write!(f, "{}", msg),
            ZomeApiError::FunctionNotImplemented => write!(f, "Function not implemented"),
            ZomeApiError::HashNotFound => write!(f, "Hash not found"),
            ZomeApiError::ValidationFailed(msg) => write!(f, "{}", msg),
            ZomeApiError::Timeout(s) => write!(f, "Timeout({})", s),
        }
    }
}

pub type ZomeApiResult<T> = Result<T, ZomeApiError>;

#[cfg(test)]
mod tests {

    use crate::error::{ZomeApiError, ZomeApiResult};
    use holochain_json_api::json::JsonString;

    #[test]
    fn zome_api_result_json_result_round_trip_test() {
        let result: ZomeApiResult<String> = Err(ZomeApiError::FunctionNotImplemented);

        assert_eq!(
            JsonString::from(result),
            JsonString::from_json("{\"Err\":\"FunctionNotImplemented\"}"),
        );
    }
}