{"action":"opened","issue":{"url":"https://api.github.com/repos/spf13/viper/issues/554","repository_url":"https://api.github.com/repos/spf13/viper","labels_url":"https://api.github.com/repos/spf13/viper/issues/554/labels{/name}","comments_url":"https://api.github.com/repos/spf13/viper/issues/554/comments","events_url":"https://api.github.com/repos/spf13/viper/issues/554/events","html_url":"https://github.com/spf13/viper/issues/554","id":350066935,"node_id":"MDU6SXNzdWUzNTAwNjY5MzU=","number":554,"title":"[RFC] Bubble-up parsing / casting errors to the user classes","user":{"login":"xvello","id":6241083,"node_id":"MDQ6VXNlcjYyNDEwODM=","avatar_url":"https://avatars2.githubusercontent.com/u/6241083?v=4","gravatar_id":"","url":"https://api.github.com/users/xvello","html_url":"https://github.com/xvello","followers_url":"https://api.github.com/users/xvello/followers","following_url":"https://api.github.com/users/xvello/following{/other_user}","gists_url":"https://api.github.com/users/xvello/gists{/gist_id}","starred_url":"https://api.github.com/users/xvello/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/xvello/subscriptions","organizations_url":"https://api.github.com/users/xvello/orgs","repos_url":"https://api.github.com/users/xvello/repos","events_url":"https://api.github.com/users/xvello/events{/privacy}","received_events_url":"https://api.github.com/users/xvello/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2018-08-13T14:58:44Z","updated_at":"2018-08-13T14:58:44Z","closed_at":null,"author_association":"NONE","body":"## The issue\r\n\r\nWe had several users confused with the syntax changing based on the underlying type (`[]string` -> space-separated fields, `map[string]string` -> inline json) or botching the inline json syntax.\r\n\r\nCurrently, Viper offers no facility to alert us when a configuration lookup triggered a parsing / casting error. This makes debugging issues hard without asking for a full dump of the enviroment, that might contain sensitive information.\r\n\r\n[The cast API](https://godoc.org/github.com/spf13/cast) optionally exposes errors, for example for `map[string]string`, two methods are available:\r\n- func ToStringMapString(i interface{}) map[string]string\r\n- func ToStringMapStringE(i interface{}) (map[string]string, error)\r\n\r\nWe would like to add support for bubbling-up viper's errors to the user class, so we can handle the error appropriately. As there are several ways to do this, I'm listing them here for discussion, having a preference for the first one though.\r\n\r\n## Proposed solutions\r\n\r\n### 1. Extend viper's public API with `Get*E` methods\r\n\r\nMirroring what `cast` does, add sister methods that returns an `error` alongside the value. There are currently 14 `Get*` methods we would need to duplicate.\r\n\r\nPros:\r\n- Consistency with `cast`\r\n- Simple to implement\r\n\r\nCons:\r\n- Clutters the public API\r\n- Needs us to grep all viper calls and use the new ones for error handling\r\n\r\n### 2. Keep a state of past errors and offer a method to dump it\r\n\r\nAdd a `map[string]error` field to the `Viper` object to keep the latest error per config key. Add one public method to retrieve this map as part of our diagnostics logic.\r\n\r\nPros:\r\n- Small change\r\n\r\nCons:\r\n- Loosing context of the error (what code triggered the lookup)\r\n- Need to protect the map by a `Mutex` to avoid race conditions\r\n\r\n### 3. Allow to provide an error handling hook\r\n\r\nAs we can extend `Viper` by passing `DecodeHookFunc` callbacks, allow to provide an error handling callback. We would set the following logic in the function:\r\n  - log a warning line\r\n  - populate the last errors map (solution 2) for our diagnostic dumps\r\n\r\nPros:\r\n- Small change\r\n- No need for a mutex\r\n\r\nCons:\r\n- Not very idiomatic in go\r\n- Added complexity\r\n- Errors will be less explicit than if created by the user class via option 1"}}
{"actor":{"display_login":"xvello"}}