Document and critically review ShellError variants - Ep. 3 (#8340)

Continuation of #8229 and #8326

# 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.

# Call to action

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

# User-Facing Changes

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

# Tests + Formatting

(No additional tests needed so far)

# Commits (so far)

- Remove `ShellError::FeatureNotEnabled`
- Name fields on `SE::ExternalNotSupported`
- Name field on `SE::InvalidProbability`
- Name fields on `SE::NushellFailed` variants
- Remove unused `SE::NushellFailedSpannedHelp`
- Name field on `SE::VariableNotFoundAtRuntime`
- Name fields on `SE::EnvVarNotFoundAtRuntime`
- Name fields on `SE::ModuleNotFoundAtRuntime`
- Remove usused `ModuleOrOverlayNotFoundAtRuntime`
- Name fields on `SE::OverlayNotFoundAtRuntime`
- Name field on `SE::NotFound`
This commit is contained in:
Stefan Holderbach
2023-03-06 18:33:09 +01:00
committed by GitHub
parent 4898750fc1
commit 62575c9a4f
72 changed files with 1193 additions and 1048 deletions

View File

@ -98,12 +98,12 @@ impl CallExt for Call {
let result = eval_expression(engine_state, stack, expr)?;
FromValue::from_value(&result)
} else if self.positional_len() == 0 {
Err(ShellError::AccessEmptyContent(self.head))
Err(ShellError::AccessEmptyContent { span: self.head })
} else {
Err(ShellError::AccessBeyondEnd(
self.positional_len() - 1,
self.head,
))
Err(ShellError::AccessBeyondEnd {
max_idx: self.positional_len() - 1,
span: self.head,
})
}
}
@ -117,12 +117,12 @@ impl CallExt for Call {
let result = eval_expression(engine_state, stack, expr)?;
FromValue::from_value(&result)
} else if self.parser_info.is_empty() {
Err(ShellError::AccessEmptyContent(self.head))
Err(ShellError::AccessEmptyContent { span: self.head })
} else {
Err(ShellError::AccessBeyondEnd(
self.parser_info.len() - 1,
self.head,
))
Err(ShellError::AccessBeyondEnd {
max_idx: self.parser_info.len() - 1,
span: self.head,
})
}
}
}

View File

@ -77,18 +77,12 @@ pub fn convert_env_values(engine_state: &mut EngineState, stack: &Stack) -> Opti
}
} else {
error = error.or_else(|| {
Some(ShellError::NushellFailedHelp(
"Last active overlay not found in permanent state.".into(),
"This error happened during the conversion of environment variables from strings to Nushell values.".into(),
))
Some(ShellError::NushellFailedHelp { msg: "Last active overlay not found in permanent state.".into(), help: "This error happened during the conversion of environment variables from strings to Nushell values.".into() })
});
}
} else {
error = error.or_else(|| {
Some(ShellError::NushellFailedHelp(
"Last active overlay not found in stack.".into(),
"This error happened during the conversion of environment variables from strings to Nushell values.".into(),
))
Some(ShellError::NushellFailedHelp { msg: "Last active overlay not found in stack.".into(), help: "This error happened during the conversion of environment variables from strings to Nushell values.".into() })
});
}
@ -122,22 +116,22 @@ pub fn env_to_string(
match std::env::join_paths(paths) {
Ok(p) => Ok(p.to_string_lossy().to_string()),
Err(_) => Err(ShellError::EnvVarNotAString(
env_name.to_string(),
value.span()?,
)),
Err(_) => Err(ShellError::EnvVarNotAString {
envvar_name: env_name.to_string(),
span: value.span()?,
}),
}
}
_ => Err(ShellError::EnvVarNotAString(
env_name.to_string(),
value.span()?,
)),
_ => Err(ShellError::EnvVarNotAString {
envvar_name: env_name.to_string(),
span: value.span()?,
}),
}
} else {
Err(ShellError::EnvVarNotAString(
env_name.to_string(),
value.span()?,
))
Err(ShellError::EnvVarNotAString {
envvar_name: env_name.to_string(),
span: value.span()?,
})
}
}
},
@ -156,7 +150,7 @@ pub fn env_to_strings(
Ok(val_str) => {
env_vars_str.insert(env_name, val_str);
}
Err(ShellError::EnvVarNotAString(..)) => {} // ignore non-string values
Err(ShellError::EnvVarNotAString { .. }) => {} // ignore non-string values
Err(e) => return Err(e),
}
}
@ -214,16 +208,16 @@ pub fn path_str(
#[cfg(windows)]
match stack.get_env_var(engine_state, ENV_PATH_NAME_SECONDARY) {
Some(v) => Ok((ENV_PATH_NAME_SECONDARY, v)),
None => Err(ShellError::EnvVarNotFoundAtRuntime(
ENV_PATH_NAME_SECONDARY.to_string(),
None => Err(ShellError::EnvVarNotFoundAtRuntime {
envvar_name: ENV_PATH_NAME_SECONDARY.to_string(),
span,
)),
}),
}
#[cfg(not(windows))]
Err(ShellError::EnvVarNotFoundAtRuntime(
ENV_PATH_NAME.to_string(),
Err(ShellError::EnvVarNotFoundAtRuntime {
envvar_name: ENV_PATH_NAME.to_string(),
span,
))
})
}
}?;

View File

@ -197,7 +197,7 @@ fn eval_external(
) -> Result<PipelineData, ShellError> {
let decl_id = engine_state
.find_decl("run-external".as_bytes(), &[])
.ok_or(ShellError::ExternalNotSupported(head.span))?;
.ok_or(ShellError::ExternalNotSupported { span: head.span })?;
let command = engine_state.get_decl(decl_id);
@ -260,12 +260,12 @@ pub fn eval_expression(
}),
Expr::ValueWithUnit(e, unit) => match eval_expression(engine_state, stack, e)? {
Value::Int { val, .. } => Ok(compute(val, unit.item, unit.span)),
x => Err(ShellError::CantConvert(
"unit value".into(),
x.get_type().to_string(),
e.span,
None,
)),
x => Err(ShellError::CantConvert {
to_type: "unit value".into(),
from_type: x.get_type().to_string(),
span: e.span,
help: None,
}),
},
Expr::Range(from, next, to, operator) => {
let from = if let Some(f) = from {
@ -473,9 +473,9 @@ pub fn eval_expression(
lhs.upsert_data_at_cell_path(&cell_path.tail, rhs)?;
if is_env {
if cell_path.tail.is_empty() {
return Err(ShellError::CannotReplaceEnv(
cell_path.head.span,
));
return Err(ShellError::CannotReplaceEnv {
span: cell_path.head.span,
});
}
// The special $env treatment: for something like $env.config.history.max_size = 2000,
@ -1251,11 +1251,11 @@ fn check_subexp_substitution(mut input: PipelineData) -> Result<PipelineData, Sh
Some(stderr_stream) => stderr_stream.into_string().map(|s| s.item)?,
};
if failed_to_run {
Err(ShellError::ExternalCommand(
"External command failed".to_string(),
stderr_msg,
Err(ShellError::ExternalCommand {
label: "External command failed".to_string(),
help: stderr_msg,
span,
))
})
} else {
// we've captured stderr message, but it's running success.
// So we need to re-print stderr message out.