Skip to content

Commit

Permalink
Handle errors a bit more gracefully in the CLI
Browse files Browse the repository at this point in the history
Noticed an overflow error right after I pushed 0.2.0 🤦
  • Loading branch information
rtyler committed Dec 20, 2020
1 parent f176c4d commit 37122ab
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 39 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "jdp"
version = "0.2.0"
version = "0.2.1"
authors = ["R. Tyler Croy <[email protected]>"]
edition = "2018"
description = "A Rust-native parser for Jenkins Declarative Pipeline"
Expand Down
32 changes: 25 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ extern crate pest;
extern crate pest_derive;

use pest::error::Error as PestError;
use pest::error::ErrorVariant;
use pest::Parser;
use std::path::PathBuf;

Expand All @@ -14,16 +15,33 @@ pub fn parse_file(path: &PathBuf) -> Result<(), pest::error::Error<Rule>> {
use std::fs::File;
use std::io::Read;

let mut file = File::open(path).expect(&format!("Failed to open {:?}", path));
let mut contents = String::new();
file.read_to_string(&mut contents)
.expect("Failed to read file into string");

parse_pipeline_string(&contents)
match File::open(path) {
Ok(mut file) => {
let mut contents = String::new();

if let Err(e) = file.read_to_string(&mut contents) {
return Err(PestError::new_from_pos(
ErrorVariant::CustomError {
message: format!("{}", e),
},
pest::Position::from_start(""),
));
} else {
return parse_pipeline_string(&contents);
}
}
Err(e) => {
return Err(PestError::new_from_pos(
ErrorVariant::CustomError {
message: format!("{}", e),
},
pest::Position::from_start(""),
));
}
}
}

pub fn parse_pipeline_string(buffer: &str) -> Result<(), PestError<Rule>> {
use pest::error::ErrorVariant;
let mut parser = PipelineParser::parse(Rule::pipeline, buffer)?;

let mut agents = false;
Expand Down
74 changes: 44 additions & 30 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ struct CheckOpts {
file: std::path::PathBuf,
}

/// The number of lines of context to show for errors
const LINES_OF_CONTEXT: usize = 4;

fn main() {
pretty_env_logger::init();
let opts = JdpOptions::parse_args_default_or_exit();
Expand All @@ -45,41 +48,52 @@ fn main() {
use std::fs::File;
use std::io::{BufRead, BufReader};

let file = File::open(&checkopts.file).expect("Failed to read the file again?");
let lines: Vec<String> = BufReader::new(file).lines().map(|l| l.unwrap()).collect();
if checkopts.file.is_file() {
let file = File::open(&checkopts.file).expect("Failed to reopen file");
let lines: Vec<String> =
BufReader::new(file).lines().map(|l| l.unwrap()).collect();

let filename = checkopts.file.as_path().to_string_lossy();
println!("\n{}", filename);
for _ in 0..filename.len() {
print!("-");
}
println!("");
let filename = checkopts.file.as_path().to_string_lossy();
println!("\n{}", filename);
for _ in 0..filename.len() {
print!("-");
}
println!("");

match error.line_col {
Pos((line, column)) => {
let start_line = std::cmp::max(0, line - 4);
match error.line_col {
Pos((line, column)) => {
let start_line = if line < LINES_OF_CONTEXT {
0
} else {
line - LINES_OF_CONTEXT
};

for n in start_line..line {
println!("{}: {}", n, lines[n]);
}
// Just a little spacer for the error
print!(" ");
for _ in 0..column {
print!("-");
}
println!("^");
}
Span(start, end) => {
let start_line = std::cmp::max(0, start.0 - 4);
for n in start_line..start.0 {
println!("{}: {}", n, lines[n]);
for n in start_line..line {
println!("{}: {}", n, lines[n]);
}
// Just a little spacer for the error
print!(" ");
for _ in 0..column {
print!("-");
}
println!("^");
}
// Just a little spacer for the error
print!(" ");
for _ in 0..(end.1 - 2) {
print!("-");
Span(start, end) => {
let start_line = if start.0 < LINES_OF_CONTEXT {
0
} else {
start.0 - LINES_OF_CONTEXT
};
for n in start_line..start.0 {
println!("{}: {}", n, lines[n]);
}
// Just a little spacer for the error
print!(" ");
for _ in 0..(end.1 - 2) {
print!("-");
}
println!("^");
}
println!("^");
}
}

Expand Down

0 comments on commit 37122ab

Please sign in to comment.