Skip to content

Commit

Permalink
fixup error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
olix0r committed Mar 29, 2019
1 parent 0e2fdb5 commit ad1c6fc
Showing 1 changed file with 6 additions and 10 deletions.
16 changes: 6 additions & 10 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package config

import (
"bytes"
"fmt"
"io/ioutil"
"strings"

Expand All @@ -11,8 +11,6 @@ import (
log "github.com/sirupsen/logrus"
)

var unmarshaler = jsonpb.Unmarshaler{}

// Global returns the Global protobuf config from the linkerd-config ConfigMap
func Global(filepath string) (*pb.Global, error) {
config := &pb.Global{}
Expand All @@ -30,22 +28,20 @@ func Proxy(filepath string) (*pb.Proxy, error) {
func unmarshalConfig(filepath string, msg proto.Message) error {
configJSON, err := ioutil.ReadFile(filepath)
if err != nil {
log.Errorf("error reading %s: %s", filepath, err)
return err
return fmt.Errorf("failed to read config file: %s", err)
}

log.Debugf("%s config JSON: %s", filepath, configJSON)

err = unmarshaler.Unmarshal(bytes.NewReader(configJSON), msg)
if err != nil {
log.Errorf("error unmarshaling %s: %s", filepath, err)
return err
if err = unmarshal(string(configJSON), msg); err != nil {
return fmt.Errorf("failed to unmarshal JSON from: %s: %s", filepath, err)
}

return nil
}

func unmarshal(json string, msg proto.Message) error {
// If we're using older code to read a newer config, blowing up during decoding
// is not helpful. We should detect that through other means.
u := jsonpb.Unmarshaler{AllowUnknownFields: true}
return u.Unmarshal(strings.NewReader(json), msg)
}
Expand Down

0 comments on commit ad1c6fc

Please sign in to comment.