Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: error output on scannerctl syntax check #1845

Merged
merged 1 commit into from
Feb 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion rust/src/nasl/syntax/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ pub enum ErrorKind {
#[error("{kind}")]
pub struct SyntaxError {
/// A human readable reason why this error is returned
kind: ErrorKind,
pub kind: ErrorKind,
pub(crate) line: u32,
pub(crate) file: String,
}
Expand Down
16 changes: 11 additions & 5 deletions rust/src/nasl/syntax/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -436,11 +436,7 @@ impl Token {

impl Display for Token {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{}:{} {}",
self.line_column.0, self.line_column.1, self.category
)
write!(f, "'{}'", self.category,)
}
}

Expand All @@ -450,6 +446,16 @@ impl Token {
&self.category
}

/// Returns the line number of the token
pub fn line(&self) -> usize {
self.line_column.0
}

/// Returns the column number of the token
pub fn column(&self) -> usize {
self.line_column.1
}

/// Returns true when an Token is faulty
///
/// A Token is faulty when it is a syntactical error like
Expand Down
11 changes: 10 additions & 1 deletion rust/src/scannerctl/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
//
// SPDX-License-Identifier: GPL-2.0-or-later WITH x11vnc-openssl-exception

use std::fmt::Display;
use std::path::{Path, PathBuf};

use feed::VerifyError;
Expand Down Expand Up @@ -62,12 +63,20 @@ impl CliErrorKind {
}

#[derive(Debug, thiserror::Error)]
#[error("{kind} ({filename})")]
pub struct CliError {
pub filename: String,
pub kind: CliErrorKind,
}

impl Display for CliError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
if !self.filename.is_empty() {
write!(f, "{}: ", self.filename)?;
}
write!(f, "{}", self.kind)
}
}

impl CliError {
pub fn load_error(err: std::io::Error, path: &Path) -> Self {
Self {
Expand Down
69 changes: 39 additions & 30 deletions rust/src/scannerctl/syntax/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,6 @@ use walkdir::WalkDir;

use crate::{CliError, CliErrorKind};

fn read_errors<P: AsRef<Path>>(path: P) -> Result<Vec<SyntaxError>, CliErrorKind> {
let code = load_non_utf8_path(path.as_ref())?;
Ok(parse(&code)
.filter_map(|r| match r {
Ok(_) => None,
Err(err) => Some(err),
})
.collect())
}

fn read<P: AsRef<Path>>(path: P) -> Result<Vec<Result<Statement, SyntaxError>>, CliErrorKind> {
let code = load_non_utf8_path(path.as_ref())?;
Ok(parse(&code).collect())
Expand All @@ -28,28 +18,47 @@ fn read<P: AsRef<Path>>(path: P) -> Result<Vec<Result<Statement, SyntaxError>>,
fn print_results(path: &Path, verbose: bool) -> Result<usize, CliError> {
let mut errors = 0;

if verbose {
println!("# {path:?}");
let results = read(path).map_err(|kind| CliError {
kind,
filename: format!("{path:?}"),
})?;
for r in results {
match r {
Ok(stmt) => println!("{stmt:?}"),
Err(err) => eprintln!("{err}"),
}
let print_error = |err: &SyntaxError| {
if let Some(token) = err.as_token() {
eprintln!(
"{}:{}:{}: {}",
path.to_string_lossy(),
token.line(),
token.column(),
err.kind
)
} else {
eprintln!("{}:{}", path.to_string_lossy(), err)
}
} else {
let err = read_errors(path).map_err(|kind| CliError {
kind,
filename: format!("{path:?}"),
})?;
if !err.is_empty() {
eprintln!("# Error in {path:?}");
};
let print_stmt = |stmt: Statement| {
println!(
"{}:{}:{}: {}",
path.to_string_lossy(),
stmt.as_token().line(),
stmt.as_token().column(),
stmt
)
};

let results = read(path).map_err(|kind| CliError {
kind,
filename: path.to_string_lossy().to_string(),
})?;
for r in results {
match r {
Ok(stmt) if verbose => print_stmt(stmt),
Ok(_) => {}
Err(err) => {
// when we run in interactive mode we should print a new line to
// not interfere with the count display.
if errors == 0 {
eprintln!()
}
errors += 1;
print_error(&err)
}
}
errors += err.len();
err.iter().for_each(|r| eprintln!("{r}"));
}
Ok(errors)
}
Expand Down
Loading