near_workspaces/error/
mod.rs

1//! All errors that can occur within workspaces, including but not limited to
2//! the following: IO, RPC, Execution, Sandbox, DataConversion errors.
3
4pub(crate) mod execution;
5mod impls;
6
7use std::borrow::Cow;
8
9use crate::result::ExecutionFailure;
10
11/// A list specifying general categories of NEAR workspace error.
12#[derive(Clone, Debug, Eq, PartialEq, thiserror::Error)]
13#[non_exhaustive]
14pub enum ErrorKind {
15    /// An error occurred while performing an RPC request.
16    #[error("{0}")]
17    Rpc(#[from] RpcErrorCode),
18    /// An error occurred while processing a transaction.
19    #[error("Execution")]
20    Execution,
21    /// An error having to do with running sandbox.
22    #[error("{0}")]
23    Sandbox(#[from] SandboxErrorCode),
24    /// An error from performing IO.
25    #[error("IO")]
26    Io,
27    /// An error from converting data.
28    #[error("DataConversion")]
29    DataConversion,
30    /// An error that cannot be categorized into the other error kinds.
31    #[error("Other")]
32    Other,
33}
34
35#[derive(Debug, thiserror::Error)]
36enum ErrorRepr {
37    #[error("{0}")]
38    Simple(ErrorKind),
39    #[error("{message}")]
40    Message {
41        kind: ErrorKind,
42        message: Cow<'static, str>,
43    },
44    #[error("{kind}")]
45    Custom {
46        kind: ErrorKind,
47        #[source]
48        error: Box<dyn std::error::Error + Send + Sync>,
49    },
50    #[error("{message}")]
51    Full {
52        kind: ErrorKind,
53        message: Cow<'static, str>,
54        #[source]
55        error: Box<dyn std::error::Error + Send + Sync>,
56    },
57    #[error("{error}")]
58    Detailed {
59        kind: ErrorKind,
60        // NOTE: Box to mitigate large size difference between enum variants
61        error: Box<ExecutionFailure>,
62    },
63}
64
65/// Error type that workspaces will make use of for all the errors
66/// returned from this library
67#[derive(Debug)]
68pub struct Error {
69    repr: ErrorRepr,
70}
71
72#[derive(Clone, Debug, Eq, PartialEq, thiserror::Error)]
73#[non_exhaustive]
74pub enum SandboxErrorCode {
75    #[error("Sandbox has already been started")]
76    AlreadyStarted,
77    #[error("Could not initialize sandbox node")]
78    InitFailure,
79    #[error("Could not startup and run sandbox node")]
80    RunFailure,
81    #[error("Sandbox failed to patch state")]
82    PatchStateFailure,
83    #[error("Sandbox failed to fast forward")]
84    FastForwardFailure,
85}
86
87#[derive(Clone, Debug, Eq, PartialEq, thiserror::Error)]
88#[non_exhaustive]
89pub enum RpcErrorCode {
90    #[error("unable to create a new account via helper")]
91    HelperAccountCreationFailure,
92    #[error("failed to connect to rpc service")]
93    ConnectionFailure,
94    #[error("access key was unable to be retrieved")]
95    UnableToRetrieveAccessKey,
96    #[error("unable to broadcast the transaction to the network")]
97    BroadcastTxFailure,
98    #[error("unable to call into a view function")]
99    ViewFunctionFailure,
100    #[error("unable to fulfill the query request")]
101    QueryFailure,
102    #[error("incorrect variant retrieved while querying (maybe a bug in RPC code?)")]
103    QueryReturnedInvalidData,
104}