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
use std::num::TryFromIntError;

use bb8::RunError;
#[cfg(feature = "temporal")]
use chrono::ParseError;
use derive_builder::UninitializedFieldError;

#[derive(thiserror::Error, Debug)]
pub enum Error {
    #[error("spanner client error: {0}")]
    Client(String),
    #[error("configuration error: {0}")]
    Config(String),
    #[error("codec error: {0}")]
    Codec(String),
    #[error("authentication error: {0}")]
    Auth(#[from] gcp_auth::Error),

    #[error("transport error: {0}")]
    TransportError(#[from] tonic::transport::Error),

    #[error("unexpected gRPC status: {0}")]
    Status(#[from] tonic::Status),
}

impl From<RunError<Error>> for Error {
    fn from(value: RunError<Error>) -> Self {
        match value {
            RunError::User(error) => error,
            RunError::TimedOut => Error::Client("timeout while obtaining new session".to_string()),
        }
    }
}

impl From<TryFromIntError> for Error {
    fn from(value: TryFromIntError) -> Self {
        Error::Codec(format!("{}", value))
    }
}

impl From<UninitializedFieldError> for Error {
    fn from(value: UninitializedFieldError) -> Self {
        Error::Config(format!("{}", value))
    }
}

#[cfg(feature = "temporal")]
impl From<ParseError> for Error {
    fn from(p: ParseError) -> Self {
        Error::Codec(format!("unexpected date or datetime format: {}", p))
    }
}

#[cfg(feature = "json")]
impl From<serde_json::Error> for Error {
    fn from(err: serde_json::Error) -> Self {
        Error::Codec(format!("unexpected json value: {}", err))
    }
}