rust-lang-deprecated / error-chain

Error boilerplate for Rust
Apache License 2.0
728 stars 111 forks source link

Use feature specific links in error_chain! macro #297

Closed Maggie262 closed 3 years ago

Maggie262 commented 3 years ago

Hello, I have a question about the usage of error_chain.

I have a sub-module which is featured by my_error, and I need to link it to the upper module's error_chain! macro.

The following is my code:

#[macro_use]                                                                                         
extern crate error_chain;                                                                            

#[cfg(feature = "my_error")]                                                                         
mod my_error {                                                                                       
    pub mod errors {                                                                                 
        error_chain! {                                                                               
            errors {                                                                                 
                Overflow(num: u64) {                                                                 
                    display("Overflows, num is {}", num)                                             
                }                                                                                    
            }                                                                                        
        }                                                                                            
    }                                                                                                
}                                                                                                    

pub mod errors {                                                                                     
    error_chain! {                                                                                   
        links {                                                                                      
            MyError(super::my_error::errors::Error, super::my_error::errors::ErrorKind);             
        }                                                                                            
    }                                                                                                
}

And the relevant content in Cargo.toml:

[dependencies]                                                                                       
error-chain = "0.12.4"                                                                               

[features]                                                                                           
my_error = [] 

when I compile the crate with cargo build --features my_error, there is no error. But when I compile with cargo build, the error is as follows:

error[E0433]: failed to resolve: maybe a missing crate `my_error`?
  --> src/main.rs:21:28
   |
21 |             MyError(super::my_error::errors::Error, super::my_error::errors::ErrorKind);
   |                            ^^^^^^^^ maybe a missing crate `my_error`?

error[E0433]: failed to resolve: maybe a missing crate `my_error`?
  --> src/main.rs:21:60
   |
21 |             MyError(super::my_error::errors::Error, super::my_error::errors::ErrorKind);
   |                                                            ^^^^^^^^ maybe a missing crate `my_error`?

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0433`.
error: could not compile `error_chain_feature`.

I want to keep using the my_error feature, but how could I fix the problem?

Thanks for your response!