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

@ -119,12 +119,12 @@ fn convert_nujson_to_value(value: &nu_json::Value, span: Span) -> Value {
nu_json::Value::U64(u) => {
if *u > i64::MAX as u64 {
Value::Error {
error: ShellError::CantConvert(
"i64 sized integer".into(),
"value larger than i64".into(),
error: ShellError::CantConvert {
to_type: "i64 sized integer".into(),
from_type: "value larger than i64".into(),
span,
None,
),
help: None,
},
}
} else {
Value::Int {
@ -182,12 +182,12 @@ fn convert_string_to_value(string_input: String, span: Span) -> Result<Value, Sh
)],
))
}
x => Err(ShellError::CantConvert(
format!("structured json data ({x})"),
"string".into(),
x => Err(ShellError::CantConvert {
to_type: format!("structured json data ({x})"),
from_type: "string".into(),
span,
None,
)),
help: None,
}),
},
}
}

View File

@ -106,12 +106,12 @@ pub fn convert_string_to_value(string_input: String, span: Span) -> Result<Value
match result {
Ok(value) => Ok(convert_toml_to_value(&value, span)),
Err(err) => Err(ShellError::CantConvert(
"structured toml data".into(),
"string".into(),
Err(err) => Err(ShellError::CantConvert {
to_type: "structured toml data".into(),
from_type: "string".into(),
span,
Some(err.to_string()),
)),
help: Some(err.to_string()),
}),
}
}

View File

@ -95,7 +95,12 @@ fn writer_to_string(writer: Writer<Vec<u8>>) -> Result<String, Box<dyn Error>> {
}
fn make_conversion_error(type_from: &str, span: &Span) -> ShellError {
ShellError::CantConvert(type_from.to_string(), "string".to_string(), *span, None)
ShellError::CantConvert {
to_type: type_from.to_string(),
from_type: "string".to_string(),
span: *span,
help: None,
}
}
fn to_string_tagged_value(
@ -174,12 +179,12 @@ pub fn to_delimited_data(
}
Ok(x)
}
Err(_) => Err(ShellError::CantConvert(
format_name.into(),
value.get_type().to_string(),
value.span().unwrap_or(span),
None,
)),
Err(_) => Err(ShellError::CantConvert {
to_type: format_name.into(),
from_type: value.get_type().to_string(),
span: value.span().unwrap_or(span),
help: None,
}),
}?;
Ok(Value::string(output, span).into_pipeline_data())
}

View File

@ -70,12 +70,12 @@ impl Command for ToJson {
}
.into_pipeline_data()),
_ => Ok(Value::Error {
error: ShellError::CantConvert(
"JSON".into(),
value.get_type().to_string(),
error: ShellError::CantConvert {
to_type: "JSON".into(),
from_type: value.get_type().to_string(),
span,
None,
),
help: None,
},
}
.into_pipeline_data()),
}

View File

@ -118,7 +118,12 @@ fn toml_into_pipeline_data(
}
.into_pipeline_data()),
_ => Ok(Value::Error {
error: ShellError::CantConvert("TOML".into(), value_type.to_string(), span, None),
error: ShellError::CantConvert {
to_type: "TOML".into(),
from_type: value_type.to_string(),
span,
help: None,
},
}
.into_pipeline_data()),
}

View File

@ -185,12 +185,12 @@ fn to_xml(
};
Ok(Value::string(s, head).into_pipeline_data())
}
Err(_) => Err(ShellError::CantConvert(
"XML".into(),
value_type.to_string(),
head,
None,
)),
Err(_) => Err(ShellError::CantConvert {
to_type: "XML".into(),
from_type: value_type.to_string(),
span: head,
help: None,
}),
}
}

View File

@ -111,7 +111,12 @@ fn to_yaml(input: PipelineData, head: Span) -> Result<PipelineData, ShellError>
}
.into_pipeline_data()),
_ => Ok(Value::Error {
error: ShellError::CantConvert("YAML".into(), value.get_type().to_string(), head, None),
error: ShellError::CantConvert {
to_type: "YAML".into(),
from_type: value.get_type().to_string(),
span: head,
help: None,
},
}
.into_pipeline_data()),
}