spark_connect_rs/
errors.rs

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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
//! Defines a [SparkError] for representing failures in various Spark operations.
//! Most of these are wrappers for tonic or arrow error messages
use std::error::Error;
use std::fmt::{Debug, Display, Formatter};
use std::io::Write;

use arrow::error::ArrowError;

use tonic::Code;

#[cfg(feature = "datafusion")]
use datafusion::error::DataFusionError;
#[cfg(feature = "polars")]
use polars::error::PolarsError;

/// Different `Spark` types
#[derive(Debug)]
pub enum SparkError {
    /// Returned when functionality is not yet available.
    Aborted(String),
    AlreadyExists(String),
    AnalysisException(String),
    ArrowError(ArrowError),
    Cancelled(String),
    DataLoss(String),
    DeadlineExceeded(String),
    ExternalError(Box<dyn Error + Send + Sync>),
    FailedPrecondition(String),
    InvalidConnectionUrl(String),
    InvalidArgument(String),
    IoError(String, std::io::Error),
    NotFound(String),
    NotYetImplemented(String),
    PermissionDenied(String),
    ResourceExhausted(String),
    SessionNotSameException(String),
    Unauthenticated(String),
    Unavailable(String),
    Unknown(String),
    Unimplemented(String),
    OutOfRange(String),
}

impl SparkError {
    /// Wraps an external error in an `SparkError`.
    pub fn from_external_error(error: Box<dyn Error + Send + Sync>) -> Self {
        Self::ExternalError(error)
    }
}

impl From<std::io::Error> for SparkError {
    fn from(error: std::io::Error) -> Self {
        SparkError::IoError(error.to_string(), error)
    }
}

impl From<std::str::Utf8Error> for SparkError {
    fn from(error: std::str::Utf8Error) -> Self {
        SparkError::AnalysisException(error.to_string())
    }
}

impl From<std::string::FromUtf8Error> for SparkError {
    fn from(error: std::string::FromUtf8Error) -> Self {
        SparkError::AnalysisException(error.to_string())
    }
}

impl From<ArrowError> for SparkError {
    fn from(error: ArrowError) -> Self {
        SparkError::ArrowError(error)
    }
}

impl From<tonic::Status> for SparkError {
    fn from(status: tonic::Status) -> Self {
        match status.code() {
            Code::Ok => SparkError::AnalysisException(status.message().to_string()),
            Code::Unknown => SparkError::Unknown(status.message().to_string()),
            Code::Aborted => SparkError::Aborted(status.message().to_string()),
            Code::NotFound => SparkError::NotFound(status.message().to_string()),
            Code::Internal => SparkError::AnalysisException(status.message().to_string()),
            Code::DataLoss => SparkError::DataLoss(status.message().to_string()),
            Code::Cancelled => SparkError::Cancelled(status.message().to_string()),
            Code::OutOfRange => SparkError::OutOfRange(status.message().to_string()),
            Code::Unavailable => SparkError::Unavailable(status.message().to_string()),
            Code::AlreadyExists => SparkError::AnalysisException(status.message().to_string()),
            Code::InvalidArgument => SparkError::InvalidArgument(status.message().to_string()),
            Code::DeadlineExceeded => SparkError::DeadlineExceeded(status.message().to_string()),
            Code::Unimplemented => SparkError::Unimplemented(status.message().to_string()),
            Code::Unauthenticated => SparkError::Unauthenticated(status.message().to_string()),
            Code::PermissionDenied => SparkError::PermissionDenied(status.message().to_string()),
            Code::ResourceExhausted => SparkError::ResourceExhausted(status.message().to_string()),
            Code::FailedPrecondition => {
                SparkError::FailedPrecondition(status.message().to_string())
            }
        }
    }
}

impl From<serde_json::Error> for SparkError {
    fn from(value: serde_json::Error) -> Self {
        SparkError::AnalysisException(value.to_string())
    }
}

#[cfg(feature = "datafusion")]
impl From<DataFusionError> for SparkError {
    fn from(_value: DataFusionError) -> Self {
        SparkError::AnalysisException("Error converting to DataFusion DataFrame".to_string())
    }
}

#[cfg(feature = "polars")]
impl From<PolarsError> for SparkError {
    fn from(_value: PolarsError) -> Self {
        SparkError::AnalysisException("Error converting to Polars DataFrame".to_string())
    }
}

impl From<tonic::codegen::http::uri::InvalidUri> for SparkError {
    fn from(value: tonic::codegen::http::uri::InvalidUri) -> Self {
        SparkError::InvalidConnectionUrl(value.to_string())
    }
}

impl From<tonic::transport::Error> for SparkError {
    fn from(value: tonic::transport::Error) -> Self {
        SparkError::InvalidConnectionUrl(value.to_string())
    }
}

impl<W: Write> From<std::io::IntoInnerError<W>> for SparkError {
    fn from(error: std::io::IntoInnerError<W>) -> Self {
        SparkError::IoError(error.to_string(), error.into())
    }
}

impl Display for SparkError {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            SparkError::ExternalError(source) => write!(f, "External error: {}", &source),
            SparkError::AnalysisException(desc) => write!(f, "Analysis error: {desc}"),
            SparkError::IoError(desc, _) => write!(f, "Io error: {desc}"),
            SparkError::ArrowError(desc) => write!(f, "Apache Arrow error: {desc}"),
            SparkError::NotYetImplemented(source) => write!(f, "Not yet implemented: {source}"),
            SparkError::InvalidConnectionUrl(val) => write!(f, "Invalid URL error: {val}"),
            SparkError::SessionNotSameException(val) => {
                write!(f, "Spark Session ID is not the same: {val}")
            }
            SparkError::Aborted(val) => write!(f, "Aborted: {val}"),
            SparkError::AlreadyExists(val) => write!(f, "Already Exists: {val}"),
            SparkError::Cancelled(val) => write!(f, "Cancelled: {val}"),
            SparkError::DataLoss(val) => write!(f, "Data Loss: {val}"),
            SparkError::DeadlineExceeded(val) => write!(f, "Deadline Exceeded: {val}"),
            SparkError::FailedPrecondition(val) => write!(f, "Failed Precondition: {val}"),
            SparkError::InvalidArgument(val) => write!(f, "Invalid Argument: {val}"),
            SparkError::NotFound(val) => write!(f, "Not Found: {val}"),
            SparkError::PermissionDenied(val) => write!(f, "Permission Denied: {val}"),
            SparkError::ResourceExhausted(val) => write!(f, "Resource Exhausted: {val}"),
            SparkError::Unauthenticated(val) => write!(f, "Unauthenicated: {val}"),
            SparkError::Unavailable(val) => write!(f, "Unavailable: {val}"),
            SparkError::Unknown(val) => write!(f, "Unknown: {val}"),
            SparkError::Unimplemented(val) => write!(f, "Unimplemented: {val}"),
            SparkError::OutOfRange(val) => write!(f, "Out Of Range: {val}"),
        }
    }
}

impl Error for SparkError {
    fn source(&self) -> Option<&(dyn Error + 'static)> {
        if let Self::ExternalError(e) = self {
            Some(e.as_ref())
        } else {
            None
        }
    }
}