|
| 1 | +use std::{backtrace::Backtrace, sync::Arc}; |
| 2 | + |
| 3 | +use datafusion::{arrow::error::ArrowError, error::DataFusionError}; |
| 4 | +use pg_srv::{ |
| 5 | + protocol::{self, ErrorResponse}, |
| 6 | + ProtocolError, |
| 7 | +}; |
| 8 | + |
| 9 | +use crate::{compile::CompilationError, transport::SpanId, CubeError}; |
| 10 | + |
| 11 | +#[derive(thiserror::Error, Debug)] |
| 12 | +pub enum ConnectionError { |
| 13 | + #[error("CubeError: {0}")] |
| 14 | + Cube(CubeError, Option<Arc<SpanId>>), |
| 15 | + #[error("DataFusionError: {0}")] |
| 16 | + DataFusion(DataFusionError, Option<Arc<SpanId>>), |
| 17 | + #[error("ArrowError: {0}")] |
| 18 | + Arrow(ArrowError, Option<Arc<SpanId>>), |
| 19 | + #[error("CompilationError: {0}")] |
| 20 | + CompilationError(CompilationError, Option<Arc<SpanId>>), |
| 21 | + #[error("ProtocolError: {0}")] |
| 22 | + Protocol(ProtocolError, Option<Arc<SpanId>>), |
| 23 | +} |
| 24 | + |
| 25 | +impl ConnectionError { |
| 26 | + /// Return Backtrace from any variant of Enum |
| 27 | + pub fn backtrace(&self) -> Option<&Backtrace> { |
| 28 | + match &self { |
| 29 | + ConnectionError::Cube(e, _) => e.backtrace(), |
| 30 | + ConnectionError::CompilationError(e, _) => e.backtrace(), |
| 31 | + ConnectionError::Protocol(e, _) => e.backtrace(), |
| 32 | + ConnectionError::DataFusion(_, _) | ConnectionError::Arrow(_, _) => None, |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + /// Converts Error to protocol::ErrorResponse which is usefully for writing response to the client |
| 37 | + pub fn to_error_response(self) -> protocol::ErrorResponse { |
| 38 | + match self { |
| 39 | + ConnectionError::Cube(e, _) => Self::cube_to_error_response(&e), |
| 40 | + ConnectionError::DataFusion(e, _) => Self::df_to_error_response(&e), |
| 41 | + ConnectionError::Arrow(e, _) => Self::arrow_to_error_response(&e), |
| 42 | + ConnectionError::CompilationError(e, _) => { |
| 43 | + fn to_error_response(e: CompilationError) -> protocol::ErrorResponse { |
| 44 | + match e { |
| 45 | + CompilationError::Internal(_, _, _) => protocol::ErrorResponse::error( |
| 46 | + protocol::ErrorCode::InternalError, |
| 47 | + e.to_string(), |
| 48 | + ), |
| 49 | + CompilationError::User(_, _) => protocol::ErrorResponse::error( |
| 50 | + protocol::ErrorCode::InvalidSqlStatement, |
| 51 | + e.to_string(), |
| 52 | + ), |
| 53 | + CompilationError::Unsupported(_, _) => protocol::ErrorResponse::error( |
| 54 | + protocol::ErrorCode::FeatureNotSupported, |
| 55 | + e.to_string(), |
| 56 | + ), |
| 57 | + CompilationError::Fatal(_, _) => protocol::ErrorResponse::fatal( |
| 58 | + protocol::ErrorCode::InternalError, |
| 59 | + e.to_string(), |
| 60 | + ), |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + to_error_response(e) |
| 65 | + } |
| 66 | + ConnectionError::Protocol(e, _) => e.to_error_response(), |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + pub fn with_span_id(self, span_id: Option<Arc<SpanId>>) -> Self { |
| 71 | + match self { |
| 72 | + ConnectionError::Cube(e, _) => ConnectionError::Cube(e, span_id), |
| 73 | + ConnectionError::DataFusion(e, _) => ConnectionError::DataFusion(e, span_id), |
| 74 | + ConnectionError::Arrow(e, _) => ConnectionError::Arrow(e, span_id), |
| 75 | + ConnectionError::CompilationError(e, _) => { |
| 76 | + ConnectionError::CompilationError(e, span_id) |
| 77 | + } |
| 78 | + ConnectionError::Protocol(e, _) => ConnectionError::Protocol(e, span_id), |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + pub fn span_id(&self) -> Option<Arc<SpanId>> { |
| 83 | + match self { |
| 84 | + ConnectionError::Cube(_, span_id) => span_id.clone(), |
| 85 | + ConnectionError::DataFusion(_, span_id) => span_id.clone(), |
| 86 | + ConnectionError::Arrow(_, span_id) => span_id.clone(), |
| 87 | + ConnectionError::CompilationError(_, span_id) => span_id.clone(), |
| 88 | + ConnectionError::Protocol(_, span_id) => span_id.clone(), |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + fn cube_to_error_response(e: &CubeError) -> protocol::ErrorResponse { |
| 93 | + let message = e.to_string(); |
| 94 | + // Remove `Error: ` prefix that can come from JS |
| 95 | + let message = if let Some(message) = message.strip_prefix("Error: ") { |
| 96 | + message.to_string() |
| 97 | + } else { |
| 98 | + message |
| 99 | + }; |
| 100 | + protocol::ErrorResponse::error(protocol::ErrorCode::InternalError, message) |
| 101 | + } |
| 102 | + |
| 103 | + fn df_to_error_response(e: &DataFusionError) -> protocol::ErrorResponse { |
| 104 | + match e { |
| 105 | + DataFusionError::ArrowError(arrow_err) => { |
| 106 | + return Self::arrow_to_error_response(arrow_err); |
| 107 | + } |
| 108 | + DataFusionError::External(err) => { |
| 109 | + if let Some(cube_err) = err.downcast_ref::<CubeError>() { |
| 110 | + return Self::cube_to_error_response(cube_err); |
| 111 | + } |
| 112 | + } |
| 113 | + _ => {} |
| 114 | + } |
| 115 | + protocol::ErrorResponse::error( |
| 116 | + protocol::ErrorCode::InternalError, |
| 117 | + format!("Post-processing Error: {}", e), |
| 118 | + ) |
| 119 | + } |
| 120 | + |
| 121 | + fn arrow_to_error_response(e: &ArrowError) -> protocol::ErrorResponse { |
| 122 | + match e { |
| 123 | + ArrowError::ExternalError(err) => { |
| 124 | + if let Some(df_err) = err.downcast_ref::<DataFusionError>() { |
| 125 | + return Self::df_to_error_response(df_err); |
| 126 | + } |
| 127 | + if let Some(cube_err) = err.downcast_ref::<CubeError>() { |
| 128 | + return Self::cube_to_error_response(cube_err); |
| 129 | + } |
| 130 | + } |
| 131 | + _ => {} |
| 132 | + } |
| 133 | + protocol::ErrorResponse::error( |
| 134 | + protocol::ErrorCode::InternalError, |
| 135 | + format!("Post-processing Error: {}", e), |
| 136 | + ) |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +impl From<CubeError> for ConnectionError { |
| 141 | + fn from(e: CubeError) -> Self { |
| 142 | + ConnectionError::Cube(e, None) |
| 143 | + } |
| 144 | +} |
| 145 | + |
| 146 | +impl From<CompilationError> for ConnectionError { |
| 147 | + fn from(e: CompilationError) -> Self { |
| 148 | + ConnectionError::CompilationError(e, None) |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +impl From<ProtocolError> for ConnectionError { |
| 153 | + fn from(e: ProtocolError) -> Self { |
| 154 | + ConnectionError::Protocol(e, None) |
| 155 | + } |
| 156 | +} |
| 157 | + |
| 158 | +impl From<tokio::task::JoinError> for ConnectionError { |
| 159 | + fn from(e: tokio::task::JoinError) -> Self { |
| 160 | + ConnectionError::Cube(e.into(), None) |
| 161 | + } |
| 162 | +} |
| 163 | + |
| 164 | +impl From<DataFusionError> for ConnectionError { |
| 165 | + fn from(e: DataFusionError) -> Self { |
| 166 | + ConnectionError::DataFusion(e, None) |
| 167 | + } |
| 168 | +} |
| 169 | + |
| 170 | +impl From<ArrowError> for ConnectionError { |
| 171 | + fn from(e: ArrowError) -> Self { |
| 172 | + ConnectionError::Arrow(e, None) |
| 173 | + } |
| 174 | +} |
| 175 | + |
| 176 | +/// Auto converting for all kind of io:Error to ConnectionError, sugar |
| 177 | +impl From<std::io::Error> for ConnectionError { |
| 178 | + fn from(e: std::io::Error) -> Self { |
| 179 | + ConnectionError::Protocol(e.into(), None) |
| 180 | + } |
| 181 | +} |
| 182 | + |
| 183 | +/// Auto converting for all kind of io:Error to ConnectionError, sugar |
| 184 | +impl From<ErrorResponse> for ConnectionError { |
| 185 | + fn from(e: ErrorResponse) -> Self { |
| 186 | + ConnectionError::Protocol(e.into(), None) |
| 187 | + } |
| 188 | +} |
0 commit comments