Skip to main content

anytype_rpc/
error.rs

1//! Errors returned by anytype-rpc gRPC operations.
2
3use snafu::prelude::*;
4
5/// Unified error type for anytype-rpc gRPC operations.
6#[derive(Debug, Snafu)]
7#[snafu(visibility(pub))]
8pub enum AnytypeGrpcError {
9    /// Authentication error.
10    #[snafu(display("Auth error: {source}"))]
11    Auth { source: AuthError },
12
13    /// Configuration error.
14    #[snafu(display("Config error: {source}"))]
15    Config { source: ConfigError },
16
17    /// View operation error.
18    #[snafu(display("View error: {source}"))]
19    View { source: ViewError },
20
21    /// gRPC transport connection error.
22    #[snafu(display("Transport error: {source}"))]
23    Transport { source: tonic::transport::Error },
24}
25
26/// Errors from authentication operations.
27#[derive(Debug, Snafu)]
28#[snafu(visibility(pub))]
29pub enum AuthError {
30    /// gRPC status error from a request.
31    #[snafu(display("Transport error: {source}"))]
32    Status { source: tonic::Status },
33
34    /// Anytype API returned an error response.
35    #[snafu(display("API error ({code}): {description}"))]
36    Api { code: i32, description: String },
37
38    /// Create session returned an empty token.
39    #[snafu(display("Create session returned empty token"))]
40    EmptyToken,
41
42    /// Invalid metadata value for auth token.
43    #[snafu(display("Invalid metadata value: {source}"))]
44    InvalidMetadata {
45        source: tonic::metadata::errors::InvalidMetadataValue,
46    },
47}
48
49/// Errors from configuration operations.
50#[derive(Debug, Snafu)]
51#[snafu(visibility(pub))]
52pub enum ConfigError {
53    /// Config file I/O error.
54    #[snafu(display("I/O error: {source}"))]
55    Io { source: std::io::Error },
56
57    /// Config file parse error.
58    #[snafu(display("Parse error: {source}"))]
59    Parse { source: serde_json::Error },
60
61    /// HOME environment variable not set.
62    #[snafu(display("HOME environment variable not set"))]
63    MissingHome,
64}
65
66/// Errors from view operations.
67#[derive(Debug, Snafu)]
68#[snafu(visibility(pub))]
69pub enum ViewError {
70    /// gRPC status error from a request.
71    #[snafu(display("Transport error: {source}"))]
72    Rpc { source: tonic::Status },
73
74    /// Anytype API returned an error response.
75    #[snafu(display("API error ({code}): {description}"))]
76    ApiResponse { code: i32, description: String },
77
78    /// Object view missing in response.
79    #[snafu(display("Object view missing in response"))]
80    MissingObjectView,
81
82    /// Dataview block not found for view id.
83    #[snafu(display("Dataview block not found for view id {view_id}"))]
84    MissingDataviewBlock { view_id: String },
85
86    /// View id not found.
87    #[snafu(display("View id {view_id} not found"))]
88    MissingView { view_id: String },
89
90    /// View type not supported.
91    #[snafu(display("View id {view_id} is not a supported view (type {actual})"))]
92    NotSupportedView { view_id: String, actual: i32 },
93}
94
95// From impls for AuthError
96impl From<tonic::Status> for AuthError {
97    fn from(source: tonic::Status) -> Self {
98        AuthError::Status { source }
99    }
100}
101
102impl From<tonic::metadata::errors::InvalidMetadataValue> for AuthError {
103    fn from(source: tonic::metadata::errors::InvalidMetadataValue) -> Self {
104        AuthError::InvalidMetadata { source }
105    }
106}
107
108// From impls for ConfigError
109impl From<std::io::Error> for ConfigError {
110    fn from(source: std::io::Error) -> Self {
111        ConfigError::Io { source }
112    }
113}
114
115impl From<serde_json::Error> for ConfigError {
116    fn from(source: serde_json::Error) -> Self {
117        ConfigError::Parse { source }
118    }
119}
120
121// From impls for ViewError
122impl From<tonic::Status> for ViewError {
123    fn from(source: tonic::Status) -> Self {
124        ViewError::Rpc { source }
125    }
126}
127
128// From impls for AnytypeGrpcError
129impl From<AuthError> for AnytypeGrpcError {
130    fn from(source: AuthError) -> Self {
131        AnytypeGrpcError::Auth { source }
132    }
133}
134
135impl From<ConfigError> for AnytypeGrpcError {
136    fn from(source: ConfigError) -> Self {
137        AnytypeGrpcError::Config { source }
138    }
139}
140
141impl From<ViewError> for AnytypeGrpcError {
142    fn from(source: ViewError) -> Self {
143        AnytypeGrpcError::View { source }
144    }
145}
146
147impl From<tonic::transport::Error> for AnytypeGrpcError {
148    fn from(source: tonic::transport::Error) -> Self {
149        AnytypeGrpcError::Transport { source }
150    }
151}