-
-
Save sravantit25/5792e085331ea868b0b7a9e2b2d6fe74 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//An example to show error handling in Rust | |
use deltalake::{DeltaTableError, DeltaTable,SchemaDataType}; | |
use std::error::Error; | |
use deltalake::operations::create::CreateBuilder; | |
use futures::executor; | |
//unwrap – panics on error (don't do this!) | |
pub async fn open_delta_table_example1(path: &str) { | |
let table = deltalake::open_table(path).await.unwrap(); | |
println!("The table info is: {}",table); | |
} | |
use deltalake::error_handling_example; | |
#[tokio::main] | |
async fn main() { | |
let table_path = "data_tables/gold_table"; | |
let table = error_handling_example::open_delta_table_example1(table_path).await; | |
} | |
//expect – convey a custom message on error and panic | |
pub async fn open_delta_table_example2(path: &str) { | |
let table = deltalake::open_table(path) | |
.await | |
.expect("Sorry there, but we really need that Delta table before you go ahead"); | |
println!("The table info is: {}",table); | |
} | |
//match – use pattern matching with error propagation | |
pub async fn open_delta_table_example3(path: &str) -> Result<DeltaTable, DeltaTableError> { | |
let table_open_result = deltalake::open_table(path) | |
.await; | |
let delta_table = match table_open_result { | |
Ok(table) => Ok(table), | |
Err(error) => Err(error), | |
}; | |
delta_table | |
} | |
#[tokio::main] | |
async fn main() { | |
let table_path = "data_tables/non_existing_gold_table"; | |
let table = error_handling_example::open_delta_table_example3(table_path).await; | |
match table { | |
Ok(table) => println!("The table version is: {}", table.version()), | |
Err(error) => println!("Oh uh, couldn't get to that table!: {}", error) | |
} | |
} | |
//? - propagates the error to the called function | |
pub async fn open_delta_table_example4(path: &str) -> Result<DeltaTable, DeltaTableError> { | |
let delta_table = deltalake::open_table(path).await?; | |
Ok(delta_table) | |
} | |
//unwrap_or_else - will fallback to a default in case of error | |
pub async fn open_delta_table_example5(path: &str) { | |
let table_open_result = deltalake::open_table(path) | |
.await; | |
let delta_table = table_open_result.unwrap_or_else(|error| { | |
println!("uh oh, could not find that table. Never mind, let's create one!"); | |
let empty_delta_table = executor::block_on(create_table(path)); | |
match empty_delta_table { | |
Ok(val) => val, | |
Err(error) => panic!("no way can go ahead now :["), | |
} | |
}); | |
println!("The table info is: {}",delta_table); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment