Skip to content

🌱 (chore): wrap config parsing and validation errors with contextual messages #4743

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/config/store/yaml/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func New(fs machinery.Filesystem) store.Store {
func (s *yamlStore) New(version config.Version) error {
cfg, err := config.New(version)
if err != nil {
return err
return fmt.Errorf("could not create config: %w", err)
}

s.cfg = cfg
Expand Down
10 changes: 7 additions & 3 deletions pkg/config/v3/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ func (ss *stringSlice) UnmarshalJSON(b []byte) error {
if b[0] == '[' {
var sl []string
if err := yaml.Unmarshal(b, &sl); err != nil {
return err
return fmt.Errorf("error unmarshalling string slice %q: %w", sl, err)
}
*ss = sl
return nil
}

var st string
if err := yaml.Unmarshal(b, &st); err != nil {
return err
return fmt.Errorf("error unmarshalling string %q: %w", st, err)
}
*ss = stringSlice{st}
return nil
Expand Down Expand Up @@ -235,7 +235,11 @@ func (c *Cfg) UpdateResource(res resource.Resource) error {

for i, r := range c.Resources {
if res.GVK.IsEqualTo(r.GVK) {
return c.Resources[i].Update(res)
if err := c.Resources[i].Update(res); err != nil {
return fmt.Errorf("failed to update resource %q: %w", res.GVK, err)
}

return nil
}
}

Expand Down
21 changes: 15 additions & 6 deletions pkg/config/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,14 @@ func (v *Version) Parse(version string) error {
if n, errParse := strconv.Atoi(version); errParse == nil && n < 0 {
return errNonPositive
}
return err
return fmt.Errorf("failed to convert version number %q: %w", substrings[0], err)
} else if v.Number == 0 {
return errNonPositive
}

if len(substrings) > 1 {
if err = v.Stage.Parse(substrings[1]); err != nil {
return err
return fmt.Errorf("failed to parse stage: %w", err)
}
}

Expand All @@ -83,7 +83,11 @@ func (v Version) Validate() error {
return errNonPositive
}

return v.Stage.Validate()
if err := v.Stage.Validate(); err != nil {
return fmt.Errorf("failed to validate stage: %w", err)
}

return nil
}

// Compare returns -1 if v < other, 0 if v == other, and 1 if v > other.
Expand All @@ -105,17 +109,22 @@ func (v Version) IsStable() bool {
// MarshalJSON implements json.Marshaller
func (v Version) MarshalJSON() ([]byte, error) {
if err := v.Validate(); err != nil {
return []byte{}, err
return []byte{}, fmt.Errorf("failed to validate version: %w", err)
}

marshaled, err := json.Marshal(v.String())
if err != nil {
return []byte{}, fmt.Errorf("failed to marshal version: %w", err)
}

return json.Marshal(v.String())
return marshaled, nil
}

// UnmarshalJSON implements json.Unmarshaller
func (v *Version) UnmarshalJSON(b []byte) error {
var str string
if err := json.Unmarshal(b, &str); err != nil {
return err
return fmt.Errorf("failed to unmarshal version: %w", err)
}

return v.Parse(str)
Expand Down