gremlin_client/
error.rs

1use crate::structure::GValue;
2
3use thiserror::Error;
4
5#[cfg(feature = "async_gremlin")]
6use mobc;
7
8#[allow(clippy::large_enum_variant)]
9#[derive(Debug, Error)]
10pub enum GremlinError {
11    #[error("data store disconnected: {0}")]
12    Generic(String),
13
14    #[error(transparent)]
15    WebSocket(tungstenite::error::Error),
16
17    #[error(transparent)]
18    Pool(#[from] r2d2::Error),
19
20    #[error("Got wrong type {0:?}")]
21    WrongType(GValue),
22
23    #[error("Cast error: {0}")]
24    Cast(String),
25
26    #[error("JSON error: {0}")]
27    Json(String),
28
29    #[error("Request error: {0:?} ")]
30    Request((i16, String)),
31
32    #[error(transparent)]
33    Serde(#[from] serde_json::Error),
34
35    #[cfg(feature = "async_gremlin")]
36    #[error(transparent)]
37    WebSocketAsync(#[from] async_tungstenite::tungstenite::Error),
38    #[cfg(feature = "async_gremlin")]
39    #[error(transparent)]
40    ChannelSend(#[from] futures::channel::mpsc::SendError),
41    #[error(transparent)]
42    Uuid(#[from] uuid::Error),
43}
44
45#[cfg(feature = "async_gremlin")]
46impl From<mobc::Error<GremlinError>> for GremlinError {
47    fn from(e: mobc::Error<GremlinError>) -> GremlinError {
48        match e {
49            mobc::Error::Inner(e) => e,
50            mobc::Error::BadConn => {
51                GremlinError::Generic(String::from("Async pool bad connection"))
52            }
53            mobc::Error::Timeout => GremlinError::Generic(String::from("Async pool timeout")),
54        }
55    }
56}
57
58#[cfg(not(feature = "async_gremlin"))]
59impl From<tungstenite::error::Error> for GremlinError {
60    fn from(e: tungstenite::error::Error) -> GremlinError {
61        let error = match e {
62            tungstenite::error::Error::AlreadyClosed => tungstenite::error::Error::AlreadyClosed,
63            tungstenite::error::Error::ConnectionClosed => {
64                tungstenite::error::Error::ConnectionClosed
65            }
66            _ => return GremlinError::Generic(format!("Error from ws {}", e)),
67        };
68        GremlinError::WebSocket(error)
69    }
70}