mirror of
https://github.com/elicpeter/nyx.git
synced 2026-06-24 20:28:06 +02:00
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:
parent
0a66a0ae2d
commit
bd788a8373
9 changed files with 81 additions and 43 deletions
|
|
@ -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() {
|
||||
|
|
|
|||
|
|
@ -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)?;
|
||||
|
|
|
|||
|
|
@ -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() {
|
||||
|
|
|
|||
|
|
@ -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 };
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue