remove unwraps

This commit is contained in:
evan-schott 2024-06-25 14:16:13 -07:00
parent f25ce4cbcc
commit b3d712bb15
5 changed files with 33 additions and 13 deletions

View File

@ -313,4 +313,11 @@ create_messages!(
msg: format!("Invalid public balance for account: {account}"),
help: Some("Make sure the account has enough balance to pay for the deployment.".to_string()),
}
@backtraced
table_render_failed {
args: (error: impl Display),
msg: format!("Failed to render table.\nError: {error}"),
help: None,
}
);

View File

@ -193,4 +193,11 @@ create_messages!(
msg: format!("Invalid field: {field}."),
help: Some("Field element must be numerical string with optional \"field\" suffix.".to_string()),
}
@backtraced
invalid_bound {
args: (bound: impl Display),
msg: format!("Invalid bound: {bound}."),
help: Some("Bound must be a valid u32.".to_string()),
}
);

View File

@ -228,7 +228,7 @@ fn deploy_cost_breakdown(
synthesis_cost: f64,
namespace_cost: f64,
priority_fee: f64,
) {
) -> Result<()> {
println!("\nBase deployment cost for '{}' is {} credits.\n", name.bold(), total_cost);
// Display the cost breakdown in a table.
let data = [
@ -240,6 +240,7 @@ fn deploy_cost_breakdown(
["Total", &format!("{:.6}", total_cost)],
];
let mut out = Vec::new();
text_tables::render(&mut out, data).unwrap();
println!("{}", ::std::str::from_utf8(&out).unwrap());
text_tables::render(&mut out, data).map_err(|err| CliError::table_render_failed(err))?;
println!("{}", ::std::str::from_utf8(&out).map_err(|err| CliError::table_render_failed(err))?);
Ok(())
}

View File

@ -254,13 +254,14 @@ fn handle_execute<A: Aleo>(
// Load the package.
let package = SnarkVMPackage::open(&path)?;
// Convert the inputs.
let inputs = inputs
.iter()
.map(|input| Value::from_str(input).map_err(PackageError::snarkvm_error).unwrap())
.collect::<Vec<Value<A::Network>>>();
let mut parsed_inputs: Vec<Value<A::Network>> = Vec::new();
for input in inputs.iter() {
let value = Value::from_str(input)?;
parsed_inputs.push(value);
}
// Execute the request.
let (response, execution, metrics) = package
.execute::<A, _>(endpoint.to_string(), &private_key, Identifier::try_from(command.name.clone())?, &inputs, rng)
.execute::<A, _>(endpoint.to_string(), &private_key, Identifier::try_from(command.name.clone())?, &parsed_inputs, rng)
.map_err(PackageError::execution_error)?;
let fee = None;
@ -343,7 +344,7 @@ fn load_program_from_network<N: Network>(
},
}
.execute(Context::new(context.path.clone(), context.home.clone(), true)?)?;
let program = SnarkVMProgram::<N>::from_str(&program_src).unwrap();
let program = SnarkVMProgram::<N>::from_str(&program_src)?;
// Return early if the program is already loaded.
if process.contains_program(program.id()) {
@ -368,7 +369,7 @@ fn load_program_from_network<N: Network>(
}
// A helper function to display a cost breakdown of the execution.
fn execution_cost_breakdown(name: &String, total_cost: f64, storage_cost: f64, finalize_cost: f64, priority_fee: f64) {
fn execution_cost_breakdown(name: &String, total_cost: f64, storage_cost: f64, finalize_cost: f64, priority_fee: f64) -> Result<()> {
println!("\nBase execution cost for '{}' is {} credits.\n", name.bold(), total_cost);
// Display the cost breakdown in a table.
let data = [
@ -379,6 +380,7 @@ fn execution_cost_breakdown(name: &String, total_cost: f64, storage_cost: f64, f
["Total", &format!("{:.6}", total_cost)],
];
let mut out = Vec::new();
text_tables::render(&mut out, data).unwrap();
println!("{}", std::str::from_utf8(&out).unwrap());
text_tables::render(&mut out, data).map_err(|err| CliError::table_render_failed(err))?;
println!("{}", std::str::from_utf8(&out).map_err(|err| CliError::table_render_failed(err))?);
Ok(())
}

View File

@ -69,8 +69,11 @@ impl Command for Block {
is_valid_numerical_input(&range[0])?;
is_valid_numerical_input(&range[1])?;
// Parse the range values.
let end = &range[1].parse::<u32>().map_err(|_| UtilError::invalid_bound(&range[1]))?;
let start = &range[0].parse::<u32>().map_err(|_| UtilError::invalid_bound(&range[0]))?;
// Make sure the range is not too large.
if range[1].parse::<u32>().unwrap() - range[0].parse::<u32>().unwrap() > 50 {
if end - start > 50 {
return Err(UtilError::invalid_range().into());
}
format!("blocks?start={}&end={}", range[0], range[1])