-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update config loading to automatically call PostLoad hooks (#17)
* Update config loading to implicitly call PostLoad on supported configs, and update to a more modern error handling pattern. * Update testify version to support ErrorAs tests
- Loading branch information
Showing
8 changed files
with
236 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package config | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
var ( | ||
ErrInvalidMaskTag = errors.New("invalid mask tag") | ||
) | ||
|
||
// LoadError is an error returned when a config fails to load. It contains Errors, a slice of | ||
// any errors that occurred during config load. | ||
type LoadError struct { | ||
// Errors that were found while loading a config. | ||
Errors []error | ||
} | ||
|
||
func newLoadError(errs []error) *LoadError { | ||
return &LoadError{Errors: errs} | ||
} | ||
|
||
func (le *LoadError) Error() string { | ||
var errs []string | ||
for _, errStr := range le.Errors { | ||
errs = append(errs, errStr.Error()) | ||
} | ||
|
||
return "failed to load config: " + strings.Join(errs, ", ") | ||
} | ||
|
||
// SerializeError is an error returned when a config loader fails to serialize a field | ||
// in a config struct. | ||
type SerializeError struct { | ||
FieldName string | ||
Err error | ||
} | ||
|
||
func newSerializeError(fieldName string, serializeErr error) *SerializeError { | ||
return &SerializeError{ | ||
FieldName: fieldName, | ||
Err: serializeErr, | ||
} | ||
} | ||
|
||
func (se *SerializeError) Error() string { | ||
return fmt.Sprintf("field '%s': %s", se.FieldName, se.Err) | ||
} | ||
|
||
func (se *SerializeError) Unwrap() error { | ||
return se.Err | ||
} | ||
|
||
// PostLoadError is an error returned when a config's PostLoad method fails. | ||
type PostLoadError struct { | ||
Err error | ||
} | ||
|
||
func newPostLoadError(err error) *PostLoadError { | ||
return &PostLoadError{Err: err} | ||
} | ||
|
||
func (ple *PostLoadError) Error() string { | ||
return "post load callback failed: " + ple.Err.Error() | ||
} | ||
|
||
func (ple *PostLoadError) Unwrap() error { | ||
return ple.Err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package config | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestLoadError(t *testing.T) { | ||
t.Run("error string includes error slice", func(t *testing.T) { | ||
err := newLoadError([]error{ | ||
fmt.Errorf("err1"), | ||
fmt.Errorf("err2"), | ||
}) | ||
|
||
assert.EqualError(t, | ||
err, | ||
"failed to load config: err1, err2", | ||
) | ||
}) | ||
} | ||
|
||
func TestSerializeError(t *testing.T) { | ||
t.Run("unwrap error", func(t *testing.T) { | ||
innerErr := fmt.Errorf("inner") | ||
err := newSerializeError("X", innerErr) | ||
assert.Equal(t, "X", err.FieldName) | ||
assert.ErrorIs(t, err, innerErr) | ||
}) | ||
t.Run("error string", func(t *testing.T) { | ||
innerErr := fmt.Errorf("inner") | ||
err := newSerializeError("X", innerErr) | ||
assert.EqualError(t, | ||
err, | ||
"field 'X': inner", | ||
) | ||
}) | ||
} | ||
|
||
func TestPostLoadError(t *testing.T) { | ||
t.Run("error string", func(t *testing.T) { | ||
err := newPostLoadError(fmt.Errorf("inner err")) | ||
assert.EqualError(t, | ||
err, | ||
"post load callback failed: inner err", | ||
) | ||
}) | ||
t.Run("unwrap error", func(t *testing.T) { | ||
err := newPostLoadError(fmt.Errorf("inner err")) | ||
|
||
innerErr := err.Unwrap() | ||
require.NotNil(t, innerErr) | ||
assert.EqualError(t, innerErr, "inner err") | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.