Skip to content

Commit 5594aae

Browse files
omkar-fossion-elgreco
authored andcommitted
fix: replace panics with proper errors
Signed-off-by: Omkar P <[email protected]>
1 parent f3e1ad0 commit 5594aae

File tree

2 files changed

+51
-26
lines changed

2 files changed

+51
-26
lines changed

crates/catalog-unity/src/lib.rs

Lines changed: 49 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use crate::models::{
2323
};
2424

2525
use deltalake_core::data_catalog::DataCatalogResult;
26-
use deltalake_core::{DataCatalog, DataCatalogError, DeltaResult, DeltaTableBuilder, Path};
26+
use deltalake_core::{DataCatalog, DataCatalogError, DeltaResult, DeltaTableBuilder, DeltaTableError, Path};
2727

2828
use crate::client::retry::*;
2929
use deltalake_core::storage::{
@@ -70,6 +70,13 @@ pub enum UnityCatalogError {
7070
header_error: InvalidHeaderValue,
7171
},
7272

73+
/// Invalid Table URI
74+
#[error("Invalid Unity Catalog Table URI: {table_uri}")]
75+
InvalidTableURI {
76+
/// Table URI
77+
table_uri: String
78+
},
79+
7380
/// Unknown configuration key
7481
#[error("Missing configuration key: {0}")]
7582
MissingConfiguration(String),
@@ -78,6 +85,10 @@ pub enum UnityCatalogError {
7885
#[error("Failed to get a credential from UnityCatalog client configuration.")]
7986
MissingCredential,
8087

88+
/// Temporary Credentials Fetch Failure
89+
#[error("Unable to get temporary credentials from Unity Catalog.")]
90+
TemporaryCredentialsFetchFailure,
91+
8192
#[error("Azure CLI error: {message}")]
8293
AzureCli {
8394
/// Error description
@@ -90,6 +101,13 @@ pub enum UnityCatalogError {
90101
#[cfg(feature = "datafusion")]
91102
#[error("Datafusion error: {0}")]
92103
DatafusionError(#[from] datafusion_common::DataFusionError),
104+
105+
/// A generic error from a source
106+
#[error("An error occurred in catalog: {source}")]
107+
Generic {
108+
/// Error message
109+
source: Box<dyn std::error::Error + Send + Sync + 'static>,
110+
},
93111
}
94112

95113
impl From<ErrorResponse> for UnityCatalogError {
@@ -101,6 +119,14 @@ impl From<ErrorResponse> for UnityCatalogError {
101119
}
102120
}
103121

122+
impl From<DataCatalogError> for UnityCatalogError {
123+
fn from(value: DataCatalogError) -> Self {
124+
UnityCatalogError::Generic {
125+
source: Box::new(value),
126+
}
127+
}
128+
}
129+
104130
impl From<UnityCatalogError> for DataCatalogError {
105131
fn from(value: UnityCatalogError) -> Self {
106132
DataCatalogError::Generic {
@@ -110,6 +136,14 @@ impl From<UnityCatalogError> for DataCatalogError {
110136
}
111137
}
112138

139+
impl From<UnityCatalogError> for DeltaTableError {
140+
fn from(value: UnityCatalogError) -> Self {
141+
DeltaTableError::GenericError {
142+
source: Box::new(value),
143+
}
144+
}
145+
}
146+
113147
/// Configuration options for unity catalog client
114148
pub enum UnityCatalogConfigKey {
115149
/// Url of a Databricks workspace
@@ -457,24 +491,21 @@ impl UnityCatalogBuilder {
457491
) -> Result<(String, HashMap<String, String>), UnityCatalogError> {
458492
let uri_parts: Vec<&str> = table_uri[5..].split('.').collect();
459493
if uri_parts.len() != 3 {
460-
panic!("Invalid Unity Catalog URI: {}", table_uri);
494+
return Err(UnityCatalogError::InvalidTableURI {
495+
table_uri: table_uri.to_string()
496+
});
461497
}
462498

463499
let catalog_id = uri_parts[0];
464500
let database_name = uri_parts[1];
465501
let table_name = uri_parts[2];
466502

467-
let unity_catalog = match UnityCatalogBuilder::from_env().build() {
468-
Ok(uc) => uc,
469-
Err(_e) => panic!("Unable to build Unity Catalog."),
470-
};
471-
let storage_location = match unity_catalog
472-
.get_table_storage_location(Some(catalog_id.to_string()), database_name, table_name)
473-
.await
474-
{
475-
Ok(s) => s,
476-
Err(err) => panic!("Unable to find the table's storage location. {}", err),
477-
};
503+
let unity_catalog = UnityCatalogBuilder::from_env().build()?;
504+
let storage_location = unity_catalog.get_table_storage_location(
505+
Some(catalog_id.to_string()),
506+
database_name,
507+
table_name
508+
).await?;
478509
let temp_creds_res = unity_catalog
479510
.get_temp_table_credentials(catalog_id, database_name, table_name)
480511
.await?;
@@ -483,7 +514,7 @@ impl UnityCatalogBuilder {
483514
temp_creds.get_credentials().unwrap()
484515
}
485516
TableTempCredentialsResponse::Error(_error) => {
486-
panic!("Unable to get temporary credentials from Unity Catalog.")
517+
return Err(UnityCatalogError::TemporaryCredentialsFetchFailure)
487518
}
488519
};
489520
Ok((storage_location, credentials))
@@ -768,20 +799,13 @@ impl ObjectStoreFactory for UnityCatalogFactory {
768799
) -> DeltaResult<(ObjectStoreRef, Path)> {
769800
use futures::executor::block_on;
770801

771-
let result = block_on(UnityCatalogBuilder::get_uc_location_and_token(
802+
let (table_path, temp_creds) = block_on(
803+
UnityCatalogBuilder::get_uc_location_and_token(
772804
table_uri.as_str(),
773-
));
774-
775-
let (table_path, temp_creds) = match result {
776-
Ok(tup) => tup,
777-
Err(_err) => panic!("Unable to get UC location and token."),
778-
};
805+
))?;
779806

780807
let mut storage_options = options.0.clone();
781-
782-
if !temp_creds.is_empty() {
783-
storage_options.extend(temp_creds);
784-
}
808+
storage_options.extend(temp_creds);
785809

786810
let mut builder =
787811
DeltaTableBuilder::from_uri(&table_path).with_io_runtime(IORuntime::default());

python/tests/test_unity_catalog.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import os
22

3+
from deltalake._internal import DeltaError
34
import pytest
45

56
from deltalake import DeltaTable
@@ -40,7 +41,7 @@ def test_uc_read_deltatable_failing():
4041
# @TODO: Currently, this will fail when used with Unity Catalog OSS
4142
# mock APIs. Need to add support for slightly different response payloads
4243
# of Unity Catalog OSS.
43-
with pytest.raises(BaseException):
44+
with pytest.raises(DeltaError):
4445
dt = DeltaTable("uc://unity.default.testtable")
4546
assert dt.is_deltatable(dt.table_uri), True
4647
expected = {

0 commit comments

Comments
 (0)