|
| 1 | +use axum::{Json, http, response::IntoResponse}; |
| 2 | +use snafu::prelude::*; |
| 3 | + |
| 4 | +use crate::schemas::JsonResponse; |
| 5 | +use core_executor::error::ExecutionError; |
| 6 | +use datafusion::arrow::error::ArrowError; |
| 7 | + |
| 8 | +#[derive(Snafu, Debug)] |
| 9 | +#[snafu(visibility(pub(crate)))] |
| 10 | +pub enum DbtError { |
| 11 | + #[snafu(display("Failed to decompress GZip body"))] |
| 12 | + GZipDecompress { source: std::io::Error }, |
| 13 | + |
| 14 | + #[snafu(display("Failed to parse login request"))] |
| 15 | + LoginRequestParse { source: serde_json::Error }, |
| 16 | + |
| 17 | + #[snafu(display("Failed to parse query body"))] |
| 18 | + QueryBodyParse { source: serde_json::Error }, |
| 19 | + |
| 20 | + #[snafu(display("Missing auth token"))] |
| 21 | + MissingAuthToken, |
| 22 | + |
| 23 | + #[snafu(display("Invalid warehouse_id format"))] |
| 24 | + InvalidWarehouseIdFormat { source: uuid::Error }, |
| 25 | + |
| 26 | + #[snafu(display("Missing DBT session"))] |
| 27 | + MissingDbtSession, |
| 28 | + |
| 29 | + #[snafu(display("Invalid auth data"))] |
| 30 | + InvalidAuthData, |
| 31 | + |
| 32 | + #[snafu(display("Feature not implemented"))] |
| 33 | + NotImplemented, |
| 34 | + |
| 35 | + #[snafu(display("Failed to parse row JSON"))] |
| 36 | + RowParse { source: serde_json::Error }, |
| 37 | + |
| 38 | + #[snafu(display("UTF8 error: {source}"))] |
| 39 | + Utf8 { source: std::string::FromUtf8Error }, |
| 40 | + |
| 41 | + #[snafu(display("Arrow error: {source}"))] |
| 42 | + Arrow { source: ArrowError }, |
| 43 | + |
| 44 | + // #[snafu(transparent)] |
| 45 | + // Metastore { |
| 46 | + // source: core_metastore::error::MetastoreError, |
| 47 | + // }, |
| 48 | + #[snafu(transparent)] |
| 49 | + Execution { source: ExecutionError }, |
| 50 | +} |
| 51 | + |
| 52 | +pub type DbtResult<T> = std::result::Result<T, DbtError>; |
| 53 | + |
| 54 | +impl IntoResponse for DbtError { |
| 55 | + fn into_response(self) -> axum::response::Response<axum::body::Body> { |
| 56 | + if let Self::Execution { source } = self { |
| 57 | + return convert_into_response(&source); |
| 58 | + } |
| 59 | + // if let Self::Metastore { source } = self { |
| 60 | + // return source.into_response(); |
| 61 | + // } |
| 62 | + |
| 63 | + let status_code = match &self { |
| 64 | + Self::GZipDecompress { .. } |
| 65 | + | Self::LoginRequestParse { .. } |
| 66 | + | Self::QueryBodyParse { .. } |
| 67 | + | Self::InvalidWarehouseIdFormat { .. } => http::StatusCode::BAD_REQUEST, |
| 68 | + Self::RowParse { .. } |
| 69 | + | Self::Utf8 { .. } |
| 70 | + | Self::Arrow { .. } |
| 71 | + // | Self::Metastore { .. } |
| 72 | + | Self::Execution { .. } |
| 73 | + | Self::NotImplemented { .. } => http::StatusCode::OK, |
| 74 | + Self::MissingAuthToken | Self::MissingDbtSession | Self::InvalidAuthData => { |
| 75 | + http::StatusCode::UNAUTHORIZED |
| 76 | + } |
| 77 | + }; |
| 78 | + |
| 79 | + let message = match &self { |
| 80 | + Self::GZipDecompress { source } => format!("failed to decompress GZip body: {source}"), |
| 81 | + Self::LoginRequestParse { source } => { |
| 82 | + format!("failed to parse login request: {source}") |
| 83 | + } |
| 84 | + Self::QueryBodyParse { source } => format!("failed to parse query body: {source}"), |
| 85 | + Self::InvalidWarehouseIdFormat { source } => format!("invalid warehouse_id: {source}"), |
| 86 | + Self::RowParse { source } => format!("failed to parse row JSON: {source}"), |
| 87 | + Self::MissingAuthToken | Self::MissingDbtSession | Self::InvalidAuthData => { |
| 88 | + "session error".to_string() |
| 89 | + } |
| 90 | + Self::Utf8 { source } => { |
| 91 | + format!("Error encoding UTF8 string: {source}") |
| 92 | + } |
| 93 | + Self::Arrow { source } => { |
| 94 | + format!("Error encoding in Arrow format: {source}") |
| 95 | + } |
| 96 | + Self::NotImplemented => "feature not implemented".to_string(), |
| 97 | + // Self::Metastore { source } => source.to_string(), |
| 98 | + Self::Execution { source } => source.to_string(), |
| 99 | + }; |
| 100 | + |
| 101 | + let body = Json(JsonResponse { |
| 102 | + success: false, |
| 103 | + message: Some(message), |
| 104 | + // TODO: On error data field contains details about actual error |
| 105 | + // {'data': {'internalError': False, 'unredactedFromSecureObject': False, 'errorCode': '002003', 'age': 0, 'sqlState': '02000', 'queryId': '01bb407f-0002-97af-0004-d66e006a69fa', 'line': 1, 'pos': 14, 'type': 'COMPILATION'}} |
| 106 | + data: None, |
| 107 | + code: Some(status_code.as_u16().to_string()), |
| 108 | + }); |
| 109 | + (status_code, body).into_response() |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +fn convert_into_response(error: &ExecutionError) -> axum::response::Response { |
| 114 | + let status_code = match error { |
| 115 | + ExecutionError::RegisterUDF { .. } |
| 116 | + | ExecutionError::RegisterUDAF { .. } |
| 117 | + | ExecutionError::InvalidTableIdentifier { .. } |
| 118 | + | ExecutionError::InvalidSchemaIdentifier { .. } |
| 119 | + | ExecutionError::InvalidFilePath { .. } |
| 120 | + | ExecutionError::InvalidBucketIdentifier { .. } |
| 121 | + | ExecutionError::TableProviderNotFound { .. } |
| 122 | + | ExecutionError::MissingDataFusionSession { .. } |
| 123 | + | ExecutionError::Utf8 { .. } |
| 124 | + | ExecutionError::VolumeNotFound { .. } |
| 125 | + | ExecutionError::ObjectStore { .. } |
| 126 | + | ExecutionError::ObjectAlreadyExists { .. } |
| 127 | + | ExecutionError::UnsupportedFileFormat { .. } |
| 128 | + | ExecutionError::RefreshCatalogList { .. } |
| 129 | + | ExecutionError::UrlParse { .. } |
| 130 | + | ExecutionError::JobError { .. } |
| 131 | + | ExecutionError::UploadFailed { .. } => http::StatusCode::BAD_REQUEST, |
| 132 | + ExecutionError::Arrow { .. } |
| 133 | + | ExecutionError::S3Tables { .. } |
| 134 | + | ExecutionError::Iceberg { .. } |
| 135 | + | ExecutionError::CatalogListDowncast { .. } |
| 136 | + | ExecutionError::CatalogDownCast { .. } |
| 137 | + | ExecutionError::RegisterCatalog { .. } => http::StatusCode::INTERNAL_SERVER_ERROR, |
| 138 | + ExecutionError::DatabaseNotFound { .. } |
| 139 | + | ExecutionError::TableNotFound { .. } |
| 140 | + | ExecutionError::SchemaNotFound { .. } |
| 141 | + | ExecutionError::CatalogNotFound { .. } |
| 142 | + | ExecutionError::Metastore { .. } |
| 143 | + | ExecutionError::DataFusion { .. } |
| 144 | + | ExecutionError::DataFusionQuery { .. } => http::StatusCode::OK, |
| 145 | + }; |
| 146 | + |
| 147 | + let message = match error { |
| 148 | + ExecutionError::DataFusion { source } => format!("DataFusion error: {source}"), |
| 149 | + ExecutionError::DataFusionQuery { source, query } => { |
| 150 | + format!("DataFusion error: {source}, query: {query}") |
| 151 | + } |
| 152 | + ExecutionError::InvalidTableIdentifier { ident } => { |
| 153 | + format!("Invalid table identifier: {ident}") |
| 154 | + } |
| 155 | + ExecutionError::InvalidSchemaIdentifier { ident } => { |
| 156 | + format!("Invalid schema identifier: {ident}") |
| 157 | + } |
| 158 | + ExecutionError::InvalidFilePath { path } => format!("Invalid file path: {path}"), |
| 159 | + ExecutionError::InvalidBucketIdentifier { ident } => { |
| 160 | + format!("Invalid bucket identifier: {ident}") |
| 161 | + } |
| 162 | + ExecutionError::Arrow { source } => format!("Arrow error: {source}"), |
| 163 | + ExecutionError::TableProviderNotFound { table_name } => { |
| 164 | + format!("No Table Provider found for table: {table_name}") |
| 165 | + } |
| 166 | + ExecutionError::MissingDataFusionSession { id } => { |
| 167 | + format!("Missing DataFusion session for id: {id}") |
| 168 | + } |
| 169 | + ExecutionError::Utf8 { source } => format!("Error encoding UTF8 string: {source}"), |
| 170 | + ExecutionError::Metastore { source } => format!("Metastore error: {source}"), |
| 171 | + ExecutionError::DatabaseNotFound { db } => format!("Database not found: {db}"), |
| 172 | + ExecutionError::TableNotFound { table } => format!("Table not found: {table}"), |
| 173 | + ExecutionError::SchemaNotFound { schema } => format!("Schema not found: {schema}"), |
| 174 | + ExecutionError::VolumeNotFound { volume } => format!("Volume not found: {volume}"), |
| 175 | + ExecutionError::ObjectStore { source } => format!("Object store error: {source}"), |
| 176 | + ExecutionError::ObjectAlreadyExists { type_name, name } => { |
| 177 | + format!("Object of type {type_name} with name {name} already exists") |
| 178 | + } |
| 179 | + ExecutionError::UnsupportedFileFormat { format } => { |
| 180 | + format!("Unsupported file format {format}") |
| 181 | + } |
| 182 | + ExecutionError::RefreshCatalogList { source } => { |
| 183 | + format!("Refresh catalog list error: {source}") |
| 184 | + } |
| 185 | + _ => "Internal server error".to_string(), |
| 186 | + }; |
| 187 | + |
| 188 | + let body = Json(JsonResponse { |
| 189 | + success: false, |
| 190 | + message: Some(message), |
| 191 | + // TODO: On error data field contains details about actual error |
| 192 | + // {'data': {'internalError': False, 'unredactedFromSecureObject': False, 'errorCode': '002003', 'age': 0, 'sqlState': '02000', 'queryId': '01bb407f-0002-97af-0004-d66e006a69fa', 'line': 1, 'pos': 14, 'type': 'COMPILATION'}} |
| 193 | + data: None, |
| 194 | + code: Some(status_code.as_u16().to_string()), |
| 195 | + }); |
| 196 | + (status_code, body).into_response() |
| 197 | +} |
0 commit comments