How to use snafu error from other module

for code readability purpose I had to separate out code into two different modules.Common errortype which is used by multiple functions in module_a and few in module_b is now part of module_a (Since only few functions uses this errortype in module_b i don't want to create a new errortype in module_b instead i want to use this existing errortype from module_a).And this errortype derives snafu due to which I can't use this errortype in module b now . I get this below error if i try to do so

error[E0425]: cannot find value `CreateFooSnafu` in this scope
    |             .context(CreateFooSnafu)?;
    |                      ^^^^^^^^^^^^^^^^^^^^^^^ not found in this scope
note: unit struct `crate::mod_a::CreateFooSnafu` exists but is inaccessible
   --> src/mod_b
    | #[derive(Debug, Snafu)]
    |                 ^^^^^ not accessible

How do i solve this

this is considered in contrary to the advised usage by author of snafu. see philosophy of the user guide of snafu, note the "many error types" section.

that being said, snafu do allow this use case, it's just not the default. you'll need the visibility attribute. the following is the example code in the documentation:

#[derive(Debug, Snafu)]
#[snafu(visibility(pub(crate)))] // Sets the default visibility for these context selectors
pub(crate) enum Error {
    IsPubCrate, // Uses the default
    IsPrivate, // Will be private
1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.