tracing icon indicating copy to clipboard operation
tracing copied to clipboard

Bubble up error for RollingFileAppender::new

Open danielSanchezQ opened this issue 3 years ago • 4 comments

Motivation

RollingAppender::new uses expect to handle any InnerAppender error:

RollingFileAppender {
    inner: InnerAppender::new(
        directory.as_ref(),
        file_name_prefix.as_ref(),
        rotation,
        Utc::now(),
    ).expect("Failed to create appender"),
}

I found it may be annoying to get a panic and not being able to handle it for outer error handling systems API users may have.

In specific I have been using it when bootstrapping a server. We have some specific return codes depending on what bootstrapping error happened. For solving it I had to manually check some file-related data (like if the path is malformed) before using any RollingAppender related call.

Solution

Bubble-up the derivated error and handle in the most outer scope.

pub fn new(
        rotation: Rotation,
        directory: impl AsRef<Path>,
        file_name_prefix: impl AsRef<Path>,
    ) -> io::Result<RollingFileAppender> {
        Ok(RollingFileAppender {
            inner: InnerAppender::new(
                directory.as_ref(),
                file_name_prefix.as_ref(),
                rotation,
                Utc::now(),
            )?,
        })
    }

danielSanchezQ avatar Dec 14 '20 14:12 danielSanchezQ

I updated the unwraps with a custom expect message. I understand that is a breaking change and that it will have to wait until the next breaking release of tracing-appender.

danielSanchezQ avatar Dec 15 '20 11:12 danielSanchezQ

Maybe with a rebase, this is still something that could be merged.

bryangarza avatar May 06 '22 00:05 bryangarza

@bryangarza I rebased and updated the code 😄

danielSanchezQ avatar May 06 '22 09:05 danielSanchezQ

FYI @hawkw @davidbarsky this one is rebased now

bryangarza avatar May 06 '22 19:05 bryangarza