Skip to content
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

feat: add errors.Is support to all errors #1735

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
17 changes: 17 additions & 0 deletions internal/encoding/error_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package encoding

import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
)

func Test_encodingError(t *testing.T) {
err1 := fmt.Errorf("test error")
err2 := encodingError("encoding error")
assert.NotErrorIs(t, err1, err2)
assert.NotErrorIs(t, err2, err1)
assert.ErrorIs(t, err2, encodingError("encoding error"))
assert.NotErrorIs(t, err2, encodingError("other encodingerror"))
}
6 changes: 6 additions & 0 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ func (pe ConfigParseError) Unwrap() error {
return pe.err
}

// Is adds a check to see if the specified target is an error of this type, see [errors.Is]
func (pe ConfigParseError) Is(err error) bool {
_, ok := err.(*ConfigParseError)
return ok
}

// toCaseInsensitiveValue checks if the value is a map;
// if so, create a copy and lower-case the keys recursively.
func toCaseInsensitiveValue(value any) any {
Expand Down
11 changes: 11 additions & 0 deletions util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
package viper

import (
"fmt"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -84,3 +85,13 @@ func TestAbsPathify(t *testing.T) {
assert.Equal(t, test.output, got)
}
}

func TestConfigParseError(t *testing.T) {
// test a generic error
err1 := fmt.Errorf("test error")
assert.NotErrorIs(t, err1, &ConfigParseError{})
// test the wrapped generic error
err2 := ConfigParseError{err: err1}
assert.ErrorIs(t, err2, &ConfigParseError{})
assert.ErrorIs(t, err2.Unwrap(), err1)
}
17 changes: 17 additions & 0 deletions viper.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,17 @@ func (e ConfigMarshalError) Error() string {
return fmt.Sprintf("While marshaling config: %s", e.err.Error())
}

// Unwrap returns the wrapped error.
func (e ConfigMarshalError) Unwrap() error {
return e.err
}

// Is adds a check to see if the specified target is an error of this type, see [errors.Is]
func (e ConfigMarshalError) Is(err error) bool {
_, ok := err.(*ConfigMarshalError)
return ok
}

var v *Viper

type RemoteResponse struct {
Expand Down Expand Up @@ -118,6 +129,12 @@ func (fnfe ConfigFileNotFoundError) Error() string {
return fmt.Sprintf("Config File %q Not Found in %q", fnfe.name, fnfe.locations)
}

// Is adds a check to see if the specified target is an error of this type, see [errors.Is]
func (e ConfigFileNotFoundError) Is(err error) bool {
_, ok := err.(*ConfigFileNotFoundError)
return ok
}

// ConfigFileAlreadyExistsError denotes failure to write new configuration file.
type ConfigFileAlreadyExistsError string

Expand Down
58 changes: 58 additions & 0 deletions viper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package viper
import (
"bytes"
"encoding/json"
"fmt"
"io"
"os"
"os/exec"
Expand Down Expand Up @@ -2696,3 +2697,60 @@ func skipWindows(t *testing.T) {
t.Skip("Skip test on Windows")
}
}

// Test the ConfigMarshalError
func TestConfigMarshalError(t *testing.T) {
// test a generic error
err1 := fmt.Errorf("test error")
assert.NotErrorIs(t, err1, &ConfigMarshalError{})
// test the wrapped generic error
err2 := ConfigMarshalError{err: err1}
assert.ErrorIs(t, err2, &ConfigMarshalError{})
assert.ErrorIs(t, err2.Unwrap(), err1)
}

func TestUnsupportedConfigError(t *testing.T) {
err1 := fmt.Errorf("test error")
err2 := UnsupportedConfigError("some string")
assert.NotErrorIs(t, err2, err1)
assert.NotErrorIs(t, err1, err2)
assert.ErrorIs(t, err2, UnsupportedConfigError("some string"))
assert.NotErrorIs(t, err2, UnsupportedConfigError("other string"))
}

func TestUnsupportedRemoteProviderError(t *testing.T) {
err1 := fmt.Errorf("test error")
err2 := UnsupportedRemoteProviderError("some string")
assert.NotErrorIs(t, err1, err2)
assert.NotErrorIs(t, err2, err1)
assert.ErrorIs(t, err2, UnsupportedRemoteProviderError("some string"))
assert.NotErrorIs(t, err2, UnsupportedRemoteProviderError("other string"))
}

func TestRemoteConfigError(t *testing.T) {
err1 := fmt.Errorf("test error")
err2 := RemoteConfigError("some string")
assert.NotErrorIs(t, err1, err2)
assert.NotErrorIs(t, err2, err1)
assert.ErrorIs(t, err2, RemoteConfigError("some string"))
assert.NotErrorIs(t, err2, RemoteConfigError("other string"))
}

func TestConfigFileNotFoundError(t *testing.T) {
err1 := fmt.Errorf("test error")
err2 := ConfigFileNotFoundError{name: "name", locations: "locations"}
assert.NotErrorIs(t, err1, err2)
assert.NotErrorIs(t, err2, err1)
assert.ErrorIs(t, err2, &ConfigFileNotFoundError{})
assert.ErrorIs(t, err2, &ConfigFileNotFoundError{name: "name", locations: "locations"})
assert.ErrorIs(t, err2, &ConfigFileNotFoundError{name: "other name", locations: "other locations"})
}

func TestConfigFileAlreadyExistsError(t *testing.T) {
err1 := fmt.Errorf("test error")
err2 := ConfigFileAlreadyExistsError("some string")
assert.NotErrorIs(t, err1, err2)
assert.NotErrorIs(t, err2, err1)
assert.ErrorIs(t, err2, ConfigFileAlreadyExistsError("some string"))
assert.NotErrorIs(t, err2, ConfigFileAlreadyExistsError("other string"))
}