kelseyhightower / envconfig

Golang library for managing configuration data from environment variables
MIT License
5.07k stars 382 forks source link

Custom Decoder Error Message #57

Closed zbindenren closed 8 years ago

zbindenren commented 8 years ago

I have the following Type:

type Mode int

const (
    Development Mode = iota
    Production
)

func (m *Mode) Decode(value string) error {
    lower := strings.ToLower(value)
    switch {
    case strings.HasPrefix(lower, "d"):
        *m = Development
    case strings.HasPrefix(lower, "p"):
        *m = Production
    default:
        return fmt.Errorf("'%s' is an unrecognized mode", value)
    }
    return nil
}

If I use the the enviromment varialbe APP_MODE=d and APP_MODE=p everything works as designed.

But if I use APP_MODE=t I get the error:

panic: envconfig.Process: assigning MULE_MODE to Mode: converting 't' to type main.Mode

But I would prefer the error message from the Decode function, like:

panic: envconfig.Process: assigning MULE_MODE to Mode: 't' s an unrecognized mode

Is that somehow possible?

teepark commented 8 years ago

I think it's a positive thing that we standardize the error coming out of Process to be a ParseError for all type/deserialization failures. How about if we add an error field to the ParseError struct for the underlying cause?