mirror of
https://github.com/akvorado/akvorado.git
synced 2025-12-11 22:14:02 +01:00
helpers/mapstructure: turn panic while decoding to an error message
While there is more helpful information in a panic, this is confusing to the user. With the amount of code using reflection, it seems better to have clearer messages to help the user find the faulty section if any.
This commit is contained in:
@@ -4,6 +4,8 @@
|
||||
package helpers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strings"
|
||||
|
||||
"github.com/mitchellh/mapstructure"
|
||||
@@ -26,16 +28,31 @@ func GetMapStructureDecoderConfig(config interface{}, hooks ...mapstructure.Deco
|
||||
ErrorUnused: true,
|
||||
WeaklyTypedInput: true,
|
||||
MatchName: MapStructureMatchName,
|
||||
DecodeHook: mapstructure.ComposeDecodeHookFunc(
|
||||
mapstructure.ComposeDecodeHookFunc(hooks...),
|
||||
mapstructure.ComposeDecodeHookFunc(mapstructureUnmarshallerHookFuncs...),
|
||||
mapstructure.TextUnmarshallerHookFunc(),
|
||||
mapstructure.StringToTimeDurationHookFunc(),
|
||||
mapstructure.StringToSliceHookFunc(","),
|
||||
DecodeHook: ProtectedDecodeHookFunc(
|
||||
mapstructure.ComposeDecodeHookFunc(
|
||||
mapstructure.ComposeDecodeHookFunc(hooks...),
|
||||
mapstructure.ComposeDecodeHookFunc(mapstructureUnmarshallerHookFuncs...),
|
||||
mapstructure.TextUnmarshallerHookFunc(),
|
||||
mapstructure.StringToTimeDurationHookFunc(),
|
||||
mapstructure.StringToSliceHookFunc(","),
|
||||
),
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
// ProtectedDecodeHookFunc wraps a DecodeHookFunc to recover and returns an error on panic.
|
||||
func ProtectedDecodeHookFunc(hook mapstructure.DecodeHookFunc) mapstructure.DecodeHookFunc {
|
||||
return func(from, to reflect.Value) (v interface{}, err error) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
v = nil
|
||||
err = fmt.Errorf("internal error while parsing: %s", r)
|
||||
}
|
||||
}()
|
||||
return mapstructure.DecodeHookExec(hook, from, to)
|
||||
}
|
||||
}
|
||||
|
||||
// MapStructureMatchName tells if map key and field names are equal.
|
||||
func MapStructureMatchName(mapKey, fieldName string) bool {
|
||||
key := strings.ToLower(strings.ReplaceAll(mapKey, "-", ""))
|
||||
|
||||
Reference in New Issue
Block a user