mirror of
https://github.com/nushell/nushell.git
synced 2024-11-22 08:23:24 +01:00
a little better error handling
This commit is contained in:
parent
61345c48b4
commit
bc9a230e5e
@ -188,9 +188,11 @@ fn groups_to_table(
|
||||
record_map.insert("count".to_string(), Value::int(items.len() as i64, span));
|
||||
|
||||
if let Some(sum_col) = maybe_sum_column.clone() {
|
||||
let (sum_col_name, sum) = sum_celllpath(sum_col, &items, span, true);
|
||||
match sum_celllpath(sum_col, &items, span) {
|
||||
Ok((sum_col_name, sum)) => {
|
||||
// add sum
|
||||
record_map.insert(sum_col_name.clone() + "_sum", Value::float(sum, span));
|
||||
record_map
|
||||
.insert(sum_col_name.clone() + "_sum", Value::float(sum, span));
|
||||
let avg = if !items.is_empty() {
|
||||
sum / items.len() as f64
|
||||
} else {
|
||||
@ -199,6 +201,12 @@ fn groups_to_table(
|
||||
// add avg
|
||||
record_map.insert(sum_col_name + "_avg", Value::float(avg, span));
|
||||
}
|
||||
Err(err) => {
|
||||
// It seems a little odd to be adding an error to the record
|
||||
record_map.insert("error".to_string(), Value::error(err, span));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Value::record(record_map, span)
|
||||
})
|
||||
@ -207,7 +215,7 @@ fn groups_to_table(
|
||||
)
|
||||
}
|
||||
|
||||
fn sum_celllpath(column: Value, items: &[Value], span: Span, is_sum: bool) -> (String, f64) {
|
||||
fn sum_celllpath(column: Value, items: &[Value], span: Span) -> Result<(String, f64), ShellError> {
|
||||
if let Value::CellPath { val, .. } = column {
|
||||
let sum: f64 = items
|
||||
.iter()
|
||||
@ -219,14 +227,12 @@ fn sum_celllpath(column: Value, items: &[Value], span: Span, is_sum: bool) -> (S
|
||||
.unwrap_or(0.0)
|
||||
})
|
||||
.sum();
|
||||
(val.to_column_name(), sum)
|
||||
Ok((val.to_column_name(), sum))
|
||||
} else {
|
||||
eprintln!("sum_col type: {:#?}", column.get_type());
|
||||
if is_sum {
|
||||
("sum".to_string(), 0.0f64)
|
||||
} else {
|
||||
("avg".to_string(), 0.0f64)
|
||||
}
|
||||
Err(ShellError::TypeMismatch {
|
||||
err_message: format!("Only CellPath's are allowed. Found {}.", column.get_type()),
|
||||
span,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user