Refactor error handling with NyxResult and enhance debugging

- Replaced `Result` with `NyxResult` across the codebase for consistent error management.
- Enhanced `NyxError` with new variants and utility conversions for better flexibility.
- Added detailed `tracing::debug` logs in `file.rs` and `walk.rs` for improved traceability.
- Simplified conditionals and improved path handling in `file.rs`.
- Refined severity filtering logic in `scan.rs`.
This commit is contained in:
elipeter 2025-06-23 20:59:49 +02:00
parent 0a66a0ae2d
commit bd788a8373
9 changed files with 81 additions and 43 deletions

View file

@ -1,12 +1,13 @@
use std::{env, fs};
use console::style;
use crate::errors::NyxResult;
use crate::utils::get_project_info;
pub fn handle(
project: Option<String>,
all: bool,
config_dir: &std::path::Path,
) -> Result<(), Box<dyn std::error::Error>> {
) -> NyxResult<()> {
if all {
println!("{}", style("Cleaning all indexes...").cyan().bold());
if config_dir.exists() {

View file

@ -10,12 +10,13 @@ use crate::utils::Config;
use crate::utils::project::get_project_info;
use crate::walk::spawn_senders;
use rayon::prelude::*;
use crate::errors::NyxResult;
pub fn handle(
action: IndexAction,
database_dir: &std::path::Path,
config: &Config,
) -> Result<(), Box<dyn std::error::Error>> {
) -> NyxResult<()> {
match action {
IndexAction::Build { path, force } => {
let build_path = std::path::Path::new(&path).canonicalize()?;
@ -57,13 +58,13 @@ pub fn build_index(
project_path: &std::path::Path,
db_path: &std::path::Path,
config: &Config,
) -> Result<(), Box<dyn std::error::Error>> {
) -> NyxResult<()> {
tracing::debug!("Building index for: {}", project_name);
fs::File::create(db_path)?;
let pool = Indexer::init(db_path)?;
{
let idx = Indexer::from_pool(project_name, &pool).unwrap();
let idx = Indexer::from_pool(project_name, &pool)?;
idx.clear()?;
}
@ -73,9 +74,9 @@ pub fn build_index(
let paths: Vec<_> = rx.into_iter().flatten().collect();
paths.into_par_iter().try_for_each(|path| -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
let issues = crate::commands::scan::run_rules_on_file(&path, config).unwrap();
let mut idx = Indexer::from_pool(project_name, &pool).unwrap();
let file_id = idx.upsert_file(&path).unwrap();
let issues = crate::commands::scan::run_rules_on_file(&path, config)?;
let mut idx = Indexer::from_pool(project_name, &pool)?;
let file_id = idx.upsert_file(&path)?;
let rows: Vec<IssueRow> = issues.iter().map(|d| IssueRow {
rule_id: d.id.as_ref(),
@ -88,9 +89,9 @@ pub fn build_index(
col: d.col as i64,
}).collect();
idx.replace_issues(file_id, rows).unwrap();
idx.replace_issues(file_id, rows)?;
Ok(())
}).unwrap();
})?;
{
let idx = Indexer::from_pool(project_name, &pool)?;

View file

@ -2,11 +2,12 @@ use std::fs;
use bytesize::ByteSize;
use chrono::{DateTime, Local};
use console::style;
use crate::errors::NyxResult;
pub fn handle(
verbose: bool,
database_dir: &std::path::Path,
) -> Result<(), Box<dyn std::error::Error>> {
) -> NyxResult<()> {
println!("{}", style("Indexed projects").blue().bold().underlined());
if !database_dir.exists() {

View file

@ -5,6 +5,7 @@ pub mod clean;
use crate::cli::Commands;
use std::path::Path;
use crate::errors::NyxResult;
use crate::patterns::Severity;
use crate::utils::config::Config;
@ -12,7 +13,7 @@ pub fn handle_command(
command: Commands,
database_dir: &Path,
config: &mut Config
) -> Result<(), Box<dyn std::error::Error>> {
) -> NyxResult<()> {
match command {
Commands::Scan { path, no_index, rebuild_index, format, high_only } => {
if high_only { config.scanner.min_severity = Severity::High };

View file

@ -33,7 +33,7 @@ pub fn handle(
format: String,
database_dir: &Path,
config: &Config,
) -> Result<(), Box<dyn std::error::Error>> {
) -> NyxResult<()> {
let scan_path = Path::new(path).canonicalize()?;
let (project_name, db_path) = get_project_info(&scan_path, database_dir)?;
@ -87,7 +87,7 @@ pub fn handle(
fn scan_filesystem(
root: &Path,
cfg: &Config,
) ->Result<Vec<Diag>, Box<dyn std::error::Error>> {
) -> NyxResult<Vec<Diag>> {
let rx = spawn_senders(root, cfg);
let acc = Mutex::new(Vec::new());
@ -95,10 +95,10 @@ fn scan_filesystem(
.flatten()
.par_bridge()
.try_for_each(|path| {
let mut local = run_rules_on_file(&path, cfg).unwrap();
let mut local = run_rules_on_file(&path, cfg)?;
acc.lock().unwrap().append(&mut local);
Ok::<(), DynError>(())
}).unwrap();
})?;
Ok(acc.into_inner()?)
}
@ -126,7 +126,7 @@ pub fn scan_with_index_parallel(
let mut diags = if needs_scan {
let d = run_rules_on_file(&path, cfg).unwrap_or_default();
let file_id = idx.upsert_file(&path).unwrap();
let file_id = idx.upsert_file(&path).unwrap_or_default();
idx.replace_issues(
file_id,
d.iter().map(|d| IssueRow {