Better error handling using do (#5890)

* adds `capture-errors` flag for `do`

* adds `get-type` core command to get type

* fmt

* add tests in example

* fmt

* fix tests

* manually revert previous changes related to `get-type`

* adds method to check for error name using `into string`

* fix clippy
This commit is contained in:
pwygab
2022-06-30 09:01:34 +08:00
committed by GitHub
parent 6ee13126f7
commit a0db4ce747
8 changed files with 62 additions and 4 deletions

View File

@ -724,6 +724,10 @@ impl From<Box<dyn std::error::Error + Send + Sync>> for ShellError {
}
}
pub fn into_code(err: &ShellError) -> Option<String> {
err.code().map(|code| code.to_string())
}
pub fn did_you_mean(possibilities: &[String], tried: &str) -> Option<String> {
let mut possible_matches: Vec<_> = possibilities
.iter()

View File

@ -92,6 +92,9 @@ pub enum SyntaxShape {
/// A record value
Record,
/// An error value
Error,
/// A custom shape with custom completion logic
Custom(Box<SyntaxShape>, DeclId),
}
@ -112,6 +115,7 @@ impl SyntaxShape {
SyntaxShape::Filesize => Type::Filesize,
SyntaxShape::FullCellPath => Type::Any,
SyntaxShape::GlobPattern => Type::String,
SyntaxShape::Error => Type::Error,
SyntaxShape::ImportPattern => Type::Any,
SyntaxShape::Int => Type::Int,
SyntaxShape::List(x) => {
@ -168,6 +172,7 @@ impl Display for SyntaxShape {
SyntaxShape::Signature => write!(f, "signature"),
SyntaxShape::Expression => write!(f, "expression"),
SyntaxShape::Boolean => write!(f, "bool"),
SyntaxShape::Error => write!(f, "error"),
SyntaxShape::Custom(x, _) => write!(f, "custom<{}>", x),
}
}