Document and critically review ShellError variants - Ep. 2 (#8326)

Continuation of #8229 

# Description

The `ShellError` enum at the moment is kind of messy. 

Many variants are basic tuple structs where you always have to reference
the implementation with its macro invocation to know which field serves
which purpose.
Furthermore we have both variants that are kind of redundant or either
overly broad to be useful for the user to match on or overly specific
with few uses.

So I set out to start fixing the lacking documentation and naming to
make it feasible to critically review the individual usages and fix
those.
Furthermore we can decide to join or split up variants that don't seem
to be fit for purpose.

**Everyone:** Feel free to add review comments if you spot inconsistent
use of `ShellError` variants.

- Name fields of `SE::IncorrectValue`
- Merge and name fields on `SE::TypeMismatch`
- Name fields on `SE::UnsupportedOperator`
- Name fields on `AssignmentRequires*` and fix doc
- Name fields on `SE::UnknownOperator`
- Name fields on `SE::MissingParameter`
- Name fields on `SE::DelimiterError`
- Name fields on `SE::IncompatibleParametersSingle`

# User-Facing Changes

(None now, end goal more explicit and consistent error messages)

# Tests + Formatting

(No additional tests needed so far)
This commit is contained in:
Stefan Holderbach
2023-03-06 11:31:07 +01:00
committed by GitHub
parent 4ae1b1cc26
commit f7b8f97873
60 changed files with 477 additions and 375 deletions

View File

@ -156,10 +156,10 @@ fn command_lazy(
if columns.len() != new_names.len() {
let value: Value = call.req(engine_state, stack, 1)?;
return Err(ShellError::IncompatibleParametersSingle(
"New name list has different size to column list".into(),
value.span()?,
));
return Err(ShellError::IncompatibleParametersSingle {
msg: "New name list has different size to column list".into(),
span: value.span()?,
});
}
let lazy = lazy.into_polars();

View File

@ -83,10 +83,10 @@ impl Command for ExprIsIn {
let list = values.as_series(call.head)?;
if matches!(list.dtype(), DataType::Object(..)) {
return Err(ShellError::IncompatibleParametersSingle(
"Cannot use a mixed list as argument".into(),
call.head,
));
return Err(ShellError::IncompatibleParametersSingle {
msg: "Cannot use a mixed list as argument".into(),
span: call.head,
});
}
let expr: NuExpression = expr.into_polars().is_in(lit(list)).into();

View File

@ -122,10 +122,10 @@ impl Command for ToLazyGroupBy {
.any(|expr| !matches!(expr, Expr::Column(..)))
{
let value: Value = call.req(engine_state, stack, 0)?;
return Err(ShellError::IncompatibleParametersSingle(
"Expected only Col expressions".into(),
value.span()?,
));
return Err(ShellError::IncompatibleParametersSingle {
msg: "Expected only Col expressions".into(),
span: value.span()?,
});
}
let lazy = NuLazyFrame::try_from_pipeline(input, call.head)?;

View File

@ -195,20 +195,20 @@ impl Command for LazyJoin {
if left_on.len() != right_on.len() {
let right_on: Value = call.req(engine_state, stack, 2)?;
return Err(ShellError::IncompatibleParametersSingle(
"The right column list has a different size to the left column list".into(),
right_on.span()?,
));
return Err(ShellError::IncompatibleParametersSingle {
msg: "The right column list has a different size to the left column list".into(),
span: right_on.span()?,
});
}
// Checking that both list of expressions are made out of col expressions or strings
for (index, list) in &[(1usize, &left_on), (2, &left_on)] {
if list.iter().any(|expr| !matches!(expr, Expr::Column(..))) {
let value: Value = call.req(engine_state, stack, *index)?;
return Err(ShellError::IncompatibleParametersSingle(
"Expected only a string, col expressions or list of strings".into(),
value.span()?,
));
return Err(ShellError::IncompatibleParametersSingle {
msg: "Expected only a string, col expressions or list of strings".into(),
span: value.span()?,
});
}
}

View File

@ -7,9 +7,9 @@ pub fn extract_strings(value: Value) -> Result<Vec<String>, ShellError> {
) {
(Ok(col), Err(_)) => Ok(vec![col]),
(Err(_), Ok(cols)) => Ok(cols),
_ => Err(ShellError::IncompatibleParametersSingle(
"Expected a string or list of strings".into(),
value.span()?,
)),
_ => Err(ShellError::IncompatibleParametersSingle {
msg: "Expected a string or list of strings".into(),
span: value.span()?,
}),
}
}

View File

@ -128,13 +128,13 @@ pub(super) fn compute_between_series(
)),
}
}
_ => Err(ShellError::IncompatibleParametersSingle(
format!(
_ => Err(ShellError::IncompatibleParametersSingle {
msg: format!(
"Operation {} can only be done with boolean values",
operator.item
),
operation_span,
)),
span: operation_span,
}),
},
Operator::Boolean(Boolean::Or) => match lhs.dtype() {
DataType::Boolean => {
@ -157,13 +157,13 @@ pub(super) fn compute_between_series(
)),
}
}
_ => Err(ShellError::IncompatibleParametersSingle(
format!(
_ => Err(ShellError::IncompatibleParametersSingle {
msg: format!(
"Operation {} can only be done with boolean values",
operator.item
),
operation_span,
)),
span: operation_span,
}),
},
_ => Err(ShellError::OperatorMismatch {
op_span: operator.span,

View File

@ -151,10 +151,10 @@ impl NuDataFrame {
}
Axis::Column => {
if self.df.width() != other.df.width() {
return Err(ShellError::IncompatibleParametersSingle(
"Dataframes with different number of columns".into(),
return Err(ShellError::IncompatibleParametersSingle {
msg: "Dataframes with different number of columns".into(),
span,
));
});
}
if !self
@ -163,10 +163,10 @@ impl NuDataFrame {
.iter()
.all(|col| other.df.get_column_names().contains(col))
{
return Err(ShellError::IncompatibleParametersSingle(
"Dataframes with different columns names".into(),
return Err(ShellError::IncompatibleParametersSingle {
msg: "Dataframes with different columns names".into(),
span,
));
});
}
let new_cols = self

View File

@ -76,10 +76,10 @@ fn compute_with_value(
polars::prelude::Expr::Literal(..) => {
with_operator(operator, left, rhs, lhs_span, right.span()?, op)
}
_ => Err(ShellError::TypeMismatch(
"Only literal expressions or number".into(),
right.span()?,
)),
_ => Err(ShellError::TypeMismatch {
err_message: "Only literal expressions or number".into(),
span: right.span()?,
}),
}
}
_ => {