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

Improvements to Fuelup CLI Error Handling and Logging #620

Open
DaveDave-infosec opened this issue Jun 1, 2024 · 0 comments
Open

Improvements to Fuelup CLI Error Handling and Logging #620

DaveDave-infosec opened this issue Jun 1, 2024 · 0 comments

Comments

@DaveDave-infosec
Copy link

DaveDave-infosec commented Jun 1, 2024

Recommendations and Corrections

  1. Improved Error Handling:

• Current State: The error handling currently logs errors but does not propagate them upwards or handle them uniformly.
• Recommendation: Use the anyhow crate’s context methods to provide more detailed error messages and propagate errors consistently. Modify run() to return Result<(), anyhow::Error> for better error context propagation.

  1. Enhanced Logging:

• Current State: Logging is initialized, but could benefit from more detailed context and structured logging.
• Recommendation: Include additional contextual information in logs, such as which command is being executed, and use structured logging to capture more details about the environment and execution flow.

  1. Improved Process Name Matching:

• Current State: Process name matching is done using and_then chains which can be simplified.
• Recommendation: Refactor to use map and unwrap_or for clarity and conciseness.

  1. Error Propagation in main:

• Current State: main exits with a status code if an error occurs, but this can be improved by providing more context.
• Recommendation: Enhance the main function to print detailed error messages before exiting.

Refactored Code:

use anyhow::{Context, Result};
use fuelup::{
    fuelup_cli,
    logging::{init_tracing, log_command, log_environment},
    proxy_cli,
};
use std::{env, panic, path::PathBuf};
use tracing::{error, info};

fn run() -> Result<()> {
    log_command();
    log_environment();
    
    let arg0 = env::args().next().map(PathBuf::from);
    let process_name = arg0
        .as_ref()
        .and_then(|a| a.file_stem())
        .and_then(std::ffi::OsStr::to_str)
        .map(String::from)
        .context("Failed to extract process name from arguments")?;
    
    match process_name.as_str() {
        component::FUELUP => {
            fuelup_cli::fuelup_cli().context("Fuelup CLI execution failed")?;
        }
        n => {
            proxy_cli::proxy_run(n).context(format!("Proxy CLI execution for '{}' failed", n))?;
        }
    }
    Ok(())
}

fn main() {
    let _guard = init_tracing();
    if let Err(e) = run() {
        error!("{:?}", e);
        std::process::exit(1);
    }
}

Explanation of Corrections:

  1. Error Handling:
    • Utilized anyhow::Context to add context to errors when extracting the process name and executing commands. This provides more informative error messages.

  2. Logging:
    • Enhanced logging by including context in error handling which ensures that the logs are more descriptive and useful for debugging.
    • Added detailed error logs in main to capture and log the error context before exiting.

3. Process Name Matching:
• Simplified the process name extraction and matching logic using map and unwrap_or, making the code cleaner and easier to understand.

  1. Error Propagation in main:
    • Ensured that detailed error information is logged before exiting, improving the ability to diagnose issues quickly.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant