spark_connect/
error.rs

1use crate::client::ClientError;
2
3use core::fmt;
4use std::error::Error;
5
6/// Wraps application errors into a common SparkError enum.
7#[derive(Debug)]
8pub struct SparkError {
9    pub(crate) kind: SparkErrorKind,
10}
11
12impl SparkError {
13    pub(crate) fn new(kind: SparkErrorKind) -> Self {
14        SparkError { kind }
15    }
16}
17
18impl fmt::Display for SparkError {
19    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
20        write!(f, "SparkError: {}", self.kind)
21    }
22}
23
24impl Error for SparkError {
25	fn source(&self) -> Option<&(dyn Error + 'static)> {
26		Some(&self.kind)
27	}
28}
29
30impl From<ClientError> for SparkError {
31    fn from(error: ClientError) -> Self {
32        SparkError::new(SparkErrorKind::Client(error))
33    }
34}
35
36#[derive(Debug)]
37pub(crate) enum SparkErrorKind {
38    Client(ClientError),
39    InvalidConnectionUri { source: http::uri::InvalidUri, uri: String },
40    Transport(tonic::transport::Error)
41}
42
43impl fmt::Display for SparkErrorKind {
44    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
45        match self {
46            Self::Client(e) => write!(f, "Client error: {}", e),
47            Self::InvalidConnectionUri { uri, .. } => write!(f, "Connection URI is invalid: '{uri}'"),
48            Self::Transport(e) => write!(f, "Tonic transport error: {}", e)
49        }
50    }
51}
52
53impl Error for SparkErrorKind {
54	fn source(&self) -> Option<&(dyn Error + 'static)> {
55		match self {
56			Self::Client(source) => Some(source),
57			Self::InvalidConnectionUri { source, .. } => Some(source),
58			Self::Transport(source) => Some(source),
59		}
60	}
61}