uutils / uutils/coreutils

UIoError: Allow more use cases

Open
#11,453 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Rust
Stars
24.1k
Forks
2k
Avg merge
1d 5h
Merged PRs (30d)
365

Description

When using UIoError, the exit code is always 1.
It is also not possible to create a UIoError manually, as the fields of the struct are private. Is there a reason for this?

This probably goes to @tertsdiepraam.

Allow OsString

The context is usually a path, which is a OsString. Conversion to String (lossy) should only be done for the display.
context: Option<String> -> context: Option<OsString>

Allow instance

When creating a UIoError with new, only the error.kind is used. This will not print the match, as the match is limited to cases which have the raw_os_error(). So it is not possible to create a UIoError to use the display functionality.

This is an issue when propagating errors, e.g. MyError(io::Error) and wanting to display the standard error message of NotFound.

In this case I only have &error and cannot use into() as it is not defined for ref.

Therefore: make the struct fields public or create a new new() where we can pass the io:error itself.

Allow Exit Code

It would be nice, if it carried the exit code, then I could return UIoError instead of MyError(UIoError).

pub struct UIoError {
    pub context: Option<String>,
    pub inner: std::io::Error,
    pub code: i32,
}

impl UError for UIoError {
    fn code(&self) -> i32 {
        self.code
    }
}

impl UIoError {
    #[allow(clippy::new_ret_no_self)]
    /// Create a new `UIoError` with a given exit code and message.
    pub fn new_with_code<S: Into<String>>(
        error: std::io::Error,
        context: S,
        code: i32,
    ) -> Box<dyn UError> {
        Box::new(Self {
            context: Some(context.into()),
            inner: error,
            code,
        })
    }
}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the UIoError definition and its UError implementation, then trace how its context, inner io::Error, and exit code are constructed and displayed. Evaluate the requested OsString support, instance-preserving constructor, and configurable exit code against existing APIs. Done means the supported use cases work without wrapping UIoError in another error type and the relevant behavior is covered by tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
cli
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.