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
//! Errors that can be returned by the library
use actix::MailboxError;
use std::io;
use std::path::{Path, PathBuf};

/// Convience wrapper for `Result` returned by fallible functions in the library
pub type Result<T> = std::result::Result<T, Error>;

/// Enum wrapping all possible errors that can be generated by the library
#[derive(Debug, thiserror::Error)]
pub enum Error {
    /// Wraps actix's `MailboxError` error
    #[error("internal gWasm API error: {0}")]
    MailboxError(#[from] MailboxError),

    /// Wraps libstd's `std::io::Error` error
    #[error("gWasm API error: {0}")]
    IOError(#[from] io::Error),

    /// Wraps libstd's `std::io::Error` error providing file context
    #[error("gWasm API error: {1}: {0}")]
    FileError(io::Error, PathBuf),

    /// Wraps Golem's `actix_wamp::Error` error
    #[error("internal Golem error: {0}")]
    WampError(actix_wamp::Error),

    /// Wraps Golem RPC's `golem_rpc_api::Error` error
    #[error("internal Golem error: {0}")]
    GolemRPCError(golem_rpc_api::Error),

    /// Wraps `chrono::ParseError` error
    #[error("error parsing Timeout value: {0}")]
    ChronoError(#[from] chrono::ParseError),

    /// Received Ctrl-C interrupt
    #[error("received Ctrl-C interrupt")]
    KeyboardInterrupt,

    /// Error generated when trying to create a zero [`Timeout`](../timeout/struct.Timeout.html)
    /// value for a Golem Task
    #[error("zero timeout \"00:00:00\" is forbidden")]
    ZeroTimeoutError,

    /// Error when no TaskInfo is received when polling for task progress
    /// in [`poll_task_progress`](../golem/fn.poll_task_progress.html)
    #[error("empty TaskInfo received from Golem")]
    EmptyTaskInfo,

    /// Error when no progress can be extracted from TaskInfo
    #[error("empty progress in TaskInfo")]
    EmptyProgress,

    /// Error when gWasm task was aborted externally
    #[error("task aborted externally")]
    TaskAborted,

    /// Error when gWasm task timed out
    #[error("task timed out")]
    TaskTimedOut,
}

impl From<actix_wamp::Error> for Error {
    fn from(err: actix_wamp::Error) -> Self {
        Self::WampError(err)
    }
}

impl From<golem_rpc_api::Error> for Error {
    fn from(err: golem_rpc_api::Error) -> Self {
        Self::GolemRPCError(err)
    }
}

pub(crate) trait FileContext<T, P> {
    fn file_context(self, path: P) -> Result<T>;
}

impl<T, P: AsRef<Path>> FileContext<T, P> for io::Result<T> {
    fn file_context(self, path: P) -> Result<T> {
        self.map_err(|e| Error::FileError(e, path.as_ref().to_owned()))
    }
}