Skip to content

Commit

Permalink
Nicer errors
Browse files Browse the repository at this point in the history
  • Loading branch information
troykinsella committed Apr 30, 2024
1 parent 6a06e9f commit 9726c90
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 40 deletions.
23 changes: 0 additions & 23 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ log = "0.4.21"
env_logger = { version = "0.11.3", features = [] }
thiserror = "1.0.58"
anyhow = { version = "1.0.82", features = ["backtrace"] }
ariadne = "0.4.0"
colored = "2.1.0"
pest = { version = "2.7.9" }
pest_derive = { version = "2.7.9", features = ["grammar-extras"] }
Expand Down
30 changes: 25 additions & 5 deletions src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use std::collections::HashMap;
use std::process::exit;

use crate::args::Args;
use crate::error::Result;
use crate::error::{FriggenError, Result};
use crate::friggen::Friggen;
use crate::fs_context::resolve_fs_context;
use crate::print::{OutputPrinter, PrintTheme};
Expand Down Expand Up @@ -48,13 +49,32 @@ impl Cli {
))
}

pub fn run(&self) -> Result<()> {
pub fn run(&self) {
if self.args.version {
println!("{} {}", APP_NAME, VERSION);
return Ok(());
return;
}

let friggen = self.create_friggen()?;
friggen.run()
let friggen = match self.create_friggen() {
Ok(f) => f,
Err(err) => {
eprintln!("{}", err);
exit(1);
}
};

Check warning on line 65 in src/cli.rs

View workflow job for this annotation

GitHub Actions / fmt

Diff in /home/runner/work/friggen/friggen/src/cli.rs
if let Err(err) = friggen.run() {
match err {
FriggenError::TaskError { task: _, exit_code} => {
// Message already printed in task summary, but make sure we:
exit(exit_code)
}
_ => {
eprintln!("{}", err);
exit(1);
}
}

Check warning on line 76 in src/cli.rs

View workflow job for this annotation

GitHub Actions / fmt

Diff in /home/runner/work/friggen/friggen/src/cli.rs
}
}

}
5 changes: 4 additions & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub enum FriggenError {
#[error("friggenfile not found")]
FriggenfileNotFound,

#[error("friggenfile syntax error")]
#[error("friggenfile syntax error:\n{0}")]
FriggenfileSyntaxError(Box<pest::error::Error<Rule>>),

#[error("duplicate task definition: {0}")]
Expand All @@ -24,6 +24,9 @@ pub enum FriggenError {
#[error("cyclic task reference: {0:?}")]
CyclicTaskReference(Vec<String>),

#[error("task failed: {task} ({exit_code})")]
TaskError { task: String, exit_code: i32 },

#[error("io error: {source:?}")]
Io {
#[from]
Expand Down
8 changes: 6 additions & 2 deletions src/friggen.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::borrow::Cow;
use std::collections::HashMap;
use std::process::exit;
use std::time::SystemTime;

use colored::Colorize;
Expand Down Expand Up @@ -61,8 +60,10 @@ impl<'a> Friggen<'a> {
let task_seq = build_task_sequence(&self.tasks, &tasks)?;
log::debug!("sequence: {:?}", task_seq);

let mut last_task: &str = "";
let mut last_code: i32 = 0;
for task_name in task_seq {
last_task = task_name;
last_code = self.run_task(task_name, &tasks, &vars)?;
if last_code != 0 {
break;
Expand All @@ -74,7 +75,10 @@ impl<'a> Friggen<'a> {
.print_timed_header("★ done", start_time);

if last_code != 0 {
exit(last_code);
return Err(FriggenError::TaskError {
task: last_task.to_string(),
exit_code: last_code,
});
}

Ok(())
Expand Down
8 changes: 1 addition & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::process::exit;

use clap::Parser;
use dotenv::dotenv;

Expand All @@ -26,9 +24,5 @@ fn main() {

let args = Args::parse();
let cli = Cli::new(args);
let result = cli.run();
if let Err(err) = result {
log::error!("{}", err);
exit(1)
}
cli.run();
}
2 changes: 1 addition & 1 deletion src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ fn process_script(script: &mut [&str]) {
}

fn find_indent(script: &[&str]) -> usize {
let first_line = script.iter().find(|line| !is_whitespace(line)).unwrap(); // TODO
let first_line = script.iter().find(|line| !is_whitespace(line)).unwrap();
leading_whitespace(first_line)
}

Expand Down

0 comments on commit 9726c90

Please sign in to comment.