It would be very nice and consistent with the modern ways of processing errors if viper's errors would follow the Unwrap/Is interfaces from the standard errors
package.
Describe the solution you'd like to see This would allow the error checking to look like this:
if errors.Is(err, viper.ConfigFileNotFoundError{}) {
// do something when there is no config file
} else if err != nil {
// do something when there is a "real" error
}
var actual *viper.ConfigFileNotFoundError
if errors.As(err, &actual) {
// Do something more meaningful!
fmt.Printf("File: %s\n", actual.name)
}
I know it could be a matter of taste but since go introduced these funcs and interfaces, it would be nice to have them
Comment From: github-actions[bot]
👋 Thanks for reporting!
A maintainer will take a look at your issue shortly. 👀
In the meantime: We are working on Viper v2 and we would love to hear your thoughts about what you like or don't like about Viper, so we can improve or fix those issues.
⏰ If you have a couple minutes, please take some time and share your thoughts: https://forms.gle/R6faU74qPRPAzchZ9
📣 If you've already given us your feedback, you can still help by spreading the news, either by sharing the above link or telling people about this on Twitter:
https://twitter.com/sagikazarmark/status/1306904078967074816
Thank you! ❤️
Comment From: debarshiray
```go var actual *viper.ConfigFileNotFoundError
if errors.As(err, &actual) { // Do something more meaningful! fmt.Printf("File: %s\n", actual.name) } ```
As far as I can see, this variation works:
var actual viper.ConfigFileNotFoundError
if errors.As(err, &actual) {
// Do something more meaningful!
fmt.Printf("File: %s\n", actual.name)
}
Note that actual
is not a pointer, because ConfigFileNotFoundError
implements the error
interface using a non-pointer receiver:
func (fnfe ConfigFileNotFoundError) Error() string {
return fmt.Sprintf("Config File %q Not Found in %q", fnfe.name, fnfe.locations)
}
Comment From: debarshiray
Maybe this can be closed?
Comment From: ysulyma
@debarshiray the variation works for errors.As()
, not errors.Is()
, so the issue should remain open