Work on integrating error reporting using the ariadne crate

This commit is contained in:
2026-02-28 20:30:12 -05:00
parent 1b63eff783
commit ef0f66efaa
18 changed files with 763 additions and 540 deletions

View File

@@ -1,6 +1,8 @@
use ariadne::{Fmt, Label, Span};
use crate::{
jobs::JobBldr,
libsh::error::{ShErr, ShErrKind, ShResult},
libsh::error::{ShErr, ShErrKind, ShResult, next_color},
parse::{NdRule, Node},
prelude::*,
state::{self},
@@ -10,6 +12,7 @@ use super::setup_builtin;
pub fn cd(node: Node, job: &mut JobBldr) -> ShResult<()> {
let span = node.get_span();
let src = span.source();
let NdRule::Command {
assignments: _,
argv,
@@ -17,22 +20,28 @@ pub fn cd(node: Node, job: &mut JobBldr) -> ShResult<()> {
else {
unreachable!()
};
let cd_span = argv.first().unwrap().span.clone();
let (argv, _) = setup_builtin(Some(argv), job, None)?;
let argv = argv.unwrap();
let new_dir = if let Some((arg, _)) = argv.into_iter().next() {
PathBuf::from(arg)
let (new_dir,arg_span) = if let Some((arg, span)) = argv.into_iter().next() {
(PathBuf::from(arg),Some(span))
} else {
PathBuf::from(env::var("HOME").unwrap())
(PathBuf::from(env::var("HOME").unwrap()),None)
};
if !new_dir.exists() {
return Err(ShErr::full(
ShErrKind::ExecFail,
format!("cd: No such file or directory '{}'", new_dir.display()),
span,
));
let color = next_color();
let mut err = ShErr::new(
ShErrKind::ExecFail,
span.clone(),
).with_label(src.clone(), Label::new(cd_span.clone()).with_color(color).with_message("Failed to change directory"));
if let Some(span) = arg_span {
let color = next_color();
err = err.with_label(src.clone(), Label::new(span).with_color(color).with_message(format!("No such file or directory '{}'", new_dir.display().fg(color))));
}
return Err(err);
}
if !new_dir.is_dir() {

View File

@@ -1,3 +1,4 @@
use ariadne::Label;
use nix::{errno::Errno, unistd::execvpe};
use crate::{
@@ -41,7 +42,14 @@ pub fn exec_builtin(node: Node, io_stack: &mut IoStack, job: &mut JobBldr) -> Sh
// execvpe only returns on error
let cmd_str = cmd.to_str().unwrap().to_string();
match e {
Errno::ENOENT => Err(ShErr::full(ShErrKind::CmdNotFound(cmd_str), "", span)),
Errno::ENOENT => Err(
ShErr::full(ShErrKind::CmdNotFound, "", span.clone())
.with_label(
span.span_source().clone(),
Label::new(span.clone())
.with_message(format!("exec: command not found: {}", cmd_str))
)
),
_ => Err(ShErr::full(ShErrKind::Errno(e), format!("{e}"), span)),
}
}

View File

@@ -61,11 +61,10 @@ impl FromStr for UnaryOp {
"-t" => Ok(Self::Terminal),
"-n" => Ok(Self::NonNull),
"-z" => Ok(Self::Null),
_ => Err(ShErr::Simple {
kind: ShErrKind::SyntaxErr,
msg: "Invalid test operator".into(),
notes: vec![],
}),
_ => Err(ShErr::simple(
ShErrKind::SyntaxErr,
"Invalid test operator",
)),
}
}
}
@@ -98,11 +97,10 @@ impl FromStr for TestOp {
"-ge" => Ok(Self::IntGe),
"-le" => Ok(Self::IntLe),
_ if TEST_UNARY_OPS.contains(&s) => Ok(Self::Unary(s.parse::<UnaryOp>()?)),
_ => Err(ShErr::Simple {
kind: ShErrKind::SyntaxErr,
msg: "Invalid test operator".into(),
notes: vec![],
}),
_ => Err(ShErr::simple(
ShErrKind::SyntaxErr,
"Invalid test operator",
)),
}
}
}
@@ -140,12 +138,11 @@ pub fn double_bracket_test(node: Node) -> ShResult<bool> {
let operand = operand.expand()?.get_words().join(" ");
conjunct_op = conjunct;
let TestOp::Unary(op) = TestOp::from_str(operator.as_str())? else {
return Err(ShErr::Full {
kind: ShErrKind::SyntaxErr,
msg: "Invalid unary operator".into(),
notes: vec![],
span: err_span,
});
return Err(ShErr::full(
ShErrKind::SyntaxErr,
"Invalid unary operator",
err_span,
));
};
match op {
UnaryOp::Exists => {
@@ -248,12 +245,11 @@ pub fn double_bracket_test(node: Node) -> ShResult<bool> {
let test_op = operator.as_str().parse::<TestOp>()?;
match test_op {
TestOp::Unary(_) => {
return Err(ShErr::Full {
kind: ShErrKind::SyntaxErr,
msg: "Expected a binary operator in this test call; found a unary operator".into(),
notes: vec![],
span: err_span,
});
return Err(ShErr::full(
ShErrKind::SyntaxErr,
"Expected a binary operator in this test call; found a unary operator",
err_span,
));
}
TestOp::StringEq => {
let pattern = crate::expand::glob_to_regex(rhs.trim(), true);
@@ -269,12 +265,11 @@ pub fn double_bracket_test(node: Node) -> ShResult<bool> {
| TestOp::IntGe
| TestOp::IntLe
| TestOp::IntEq => {
let err = ShErr::Full {
kind: ShErrKind::SyntaxErr,
msg: format!("Expected an integer with '{}' operator", operator.as_str()),
notes: vec![],
span: err_span.clone(),
};
let err = ShErr::full(
ShErrKind::SyntaxErr,
format!("Expected an integer with '{}' operator", operator),
err_span.clone(),
);
let Ok(lhs) = lhs.trim().parse::<i32>() else {
return Err(err);
};

View File

@@ -117,8 +117,7 @@ pub fn zoltraak(node: Node, io_stack: &mut IoStack, job: &mut JobBldr) -> ShResu
"zoltraak: Attempted to destroy root directory '/'",
)
.with_note(
Note::new("If you really want to do this, you can use the --no-preserve-root flag")
.with_sub_notes(vec!["Example: 'zoltraak --no-preserve-root /'"]),
"If you really want to do this, you can use the --no-preserve-root flag"
),
);
}
@@ -181,8 +180,7 @@ fn annihilate(path: &str, flags: ZoltFlags) -> ShResult<()> {
format!("zoltraak: '{path}' is a directory"),
)
.with_note(
Note::new("Use the '-r' flag to recursively shred directories")
.with_sub_notes(vec!["Example: 'zoltraak -r directory'"]),
"Use the '-r' flag to recursively shred directories"
),
);
}