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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
//! This module contains Error type definitions that are used throughout Holochain, and the Ribosome in particular,
//! which is responsible for mounting and running instances of DNA, and executing WASM code.

use self::JsonError::*;
use crate::json::*;
use futures::channel::oneshot::Canceled as FutureCanceled;
use serde_json::Error as SerdeError;
use std::{
    error::Error,
    fmt,
    io::{self, Error as IoError},
    option::NoneError,
};

//--------------------------------------------------------------------------------------------------
// JsonError
//--------------------------------------------------------------------------------------------------

#[derive(
    Clone, Debug, PartialEq, Eq, Serialize, Deserialize, DefaultJson, Hash, PartialOrd, Ord,
)]
pub enum JsonError {
    ErrorGeneric(String),
    IoError(String),
    SerializationError(String),
}

impl JsonError {
    pub fn new(msg: &str) -> JsonError {
        JsonError::ErrorGeneric(msg.to_string())
    }
}

pub type JsonResult<T> = Result<T, JsonError>;

impl fmt::Display for JsonError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            ErrorGeneric(err_msg) => write!(f, "{}", err_msg),
            SerializationError(err_msg) => write!(f, "{}", err_msg),
            IoError(err_msg) => write!(f, "{}", err_msg),
        }
    }
}

impl Error for JsonError {}

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

impl From<String> for JsonError {
    fn from(error: String) -> Self {
        JsonError::new(&error)
    }
}

impl From<&'static str> for JsonError {
    fn from(error: &str) -> Self {
        JsonError::new(error)
    }
}

/// standard strings for std io errors
fn reason_for_io_error(error: &IoError) -> String {
    match error.kind() {
        io::ErrorKind::InvalidData => format!("contains invalid data: {}", error),
        io::ErrorKind::PermissionDenied => format!("missing permissions to read: {}", error),
        _ => format!("unexpected error: {}", error),
    }
}

impl<T> From<::std::sync::PoisonError<T>> for JsonError {
    fn from(error: ::std::sync::PoisonError<T>) -> Self {
        JsonError::ErrorGeneric(format!("sync poison error: {}", error))
    }
}

impl From<IoError> for JsonError {
    fn from(error: IoError) -> Self {
        JsonError::IoError(reason_for_io_error(&error))
    }
}

impl From<SerdeError> for JsonError {
    fn from(error: SerdeError) -> Self {
        JsonError::SerializationError(error.to_string())
    }
}

impl From<base64::DecodeError> for JsonError {
    fn from(error: base64::DecodeError) -> Self {
        JsonError::ErrorGeneric(format!("base64 decode error: {}", error.to_string()))
    }
}

impl From<std::str::Utf8Error> for JsonError {
    fn from(error: std::str::Utf8Error) -> Self {
        JsonError::ErrorGeneric(format!("std::str::Utf8Error error: {}", error.to_string()))
    }
}

impl From<FutureCanceled> for JsonError {
    fn from(_: FutureCanceled) -> Self {
        JsonError::ErrorGeneric("Failed future".to_string())
    }
}

impl From<NoneError> for JsonError {
    fn from(_: NoneError) -> Self {
        JsonError::ErrorGeneric("Expected Some and got None".to_string())
    }
}

impl From<hcid::HcidError> for JsonError {
    fn from(error: hcid::HcidError) -> Self {
        JsonError::ErrorGeneric(format!("{:?}", error))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    // a test function that returns our error result
    fn raises_json_error(yes: bool) -> JsonResult<()> {
        if yes {
            Err(JsonError::new("borked"))
        } else {
            Ok(())
        }
    }

    #[test]
    /// test that we can convert an error to a string
    fn to_string() {
        let err = JsonError::new("foo");
        assert_eq!("foo", err.to_string());
    }

    #[test]
    /// test that we can convert an error to valid JSON
    fn test_to_json() {
        let err = JsonError::new("foo");
        assert_eq!(
            JsonString::from_json("{\"ErrorGeneric\":\"foo\"}"),
            JsonString::from(err),
        );
    }

    #[test]
    /// smoke test new errors
    fn can_instantiate() {
        let err = JsonError::new("borked");

        assert_eq!(JsonError::ErrorGeneric("borked".to_string()), err);
    }

    #[test]
    /// test errors as a result and destructuring
    fn can_raise_json_error() {
        let err = raises_json_error(true).expect_err("should return an error when yes=true");

        match err {
            JsonError::ErrorGeneric(msg) => assert_eq!(msg, "borked"),
            _ => panic!("raises_json_error should return an ErrorGeneric"),
        };
    }

    #[test]
    /// test errors as a returned result
    fn can_return_result() {
        let result = raises_json_error(false);

        assert!(result.is_ok());
    }

    #[test]
    /// show Error implementation for JsonError
    fn error_test() {
        for (input, output) in vec![
            (JsonError::ErrorGeneric(String::from("foo")), "foo"),
            (JsonError::SerializationError(String::from("foo")), "foo"),
            (JsonError::IoError(String::from("foo")), "foo"),
        ] {
            assert_eq!(output, &input.to_string());
        }
    }

}