Skip to content

Commit

Permalink
Merge pull request #2 from steviebps/unmarshall-error-messaging
Browse files Browse the repository at this point in the history
Unmarshal error handling messages for toggle
  • Loading branch information
steviebps authored Oct 29, 2020
2 parents 3e06dba + bcdc3e1 commit a68aeb3
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ var rootCmd = &cobra.Command{
}

if err := json.Unmarshal(byteValue, &globalChamber); err != nil {
fmt.Printf("Error reading JSON: %s\n", err)
fmt.Printf("Error unmarshaling %s: %s\n", chamberFile, err)
os.Exit(1)
}
},
Expand Down
5 changes: 4 additions & 1 deletion pkg/toggle.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package rein
import (
"encoding/json"
"errors"
"fmt"
"reflect"
)

Expand All @@ -12,6 +13,7 @@ type Toggle struct {
Value interface{} `json:"value"`
}

// UnmarshalJSON Custom UnmarshalJSON method for validating toggle Value to the ToggleType
func (t *Toggle) UnmarshalJSON(b []byte) error {
var alias toggleAlias
err := json.Unmarshal(b, &alias)
Expand All @@ -20,7 +22,8 @@ func (t *Toggle) UnmarshalJSON(b []byte) error {
}

if !isValidType(alias.Value, alias.ToggleType) {
return errors.New("Toggle is not the right type")
errMsg := fmt.Sprintf("%v (%T) not of the type %s from the toggle: %s", alias.Value, alias.Value, alias.ToggleType, alias.Name)
return errors.New(errMsg)
}

*t = alias.toToggle()
Expand Down

0 comments on commit a68aeb3

Please sign in to comment.