aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSteve Moyer <smoyer1@selesy.com>2022-12-23 07:16:36 -0500
committerGitHub <noreply@github.com>2022-12-23 07:16:36 -0500
commitf94616ec76e01145a7b2b90c11b27a25d4a4be96 (patch)
tree885844b5412f824b4ba9aad3725080dded0d27fe
parent0a5a0ec1ef4ad98bc2116a953e201f96474941ab (diff)
parent0cd2f3b4bda294b040e31337ddb380fb72192a63 (diff)
downloadgit-bug-f94616ec76e01145a7b2b90c11b27a25d4a4be96.tar.gz
Merge pull request #936 from MichaelMure/feat/935/disambiguate-config-errors
feat(935): disambiguate config errors
-rw-r--r--commands/webui.go3
-rw-r--r--entities/identity/identity_user.go6
-rw-r--r--repository/config.go15
-rw-r--r--repository/config_mem.go2
-rw-r--r--repository/config_testing.go4
-rw-r--r--repository/gogit.go3
-rw-r--r--repository/gogit_config.go12
7 files changed, 28 insertions, 17 deletions
diff --git a/commands/webui.go b/commands/webui.go
index 6cbaff78..ea9fde0c 100644
--- a/commands/webui.go
+++ b/commands/webui.go
@@ -2,6 +2,7 @@ package commands
import (
"context"
+ "errors"
"fmt"
"io"
"log"
@@ -178,7 +179,7 @@ func runWebUI(env *execenv.Env, opts webUIOptions) error {
env.Out.Println("Press Ctrl+c to quit")
configOpen, err := env.Repo.AnyConfig().ReadBool(webUIOpenConfigKey)
- if err == repository.ErrNoConfigEntry {
+ if errors.Is(err, repository.ErrNoConfigEntry) {
// default to true
configOpen = true
} else if err != nil {
diff --git a/entities/identity/identity_user.go b/entities/identity/identity_user.go
index e671e662..7eb374d4 100644
--- a/entities/identity/identity_user.go
+++ b/entities/identity/identity_user.go
@@ -36,10 +36,10 @@ func GetUserIdentity(repo repository.Repo) (*Identity, error) {
func GetUserIdentityId(repo repository.Repo) (entity.Id, error) {
val, err := repo.LocalConfig().ReadString(identityConfigKey)
- if err == repository.ErrNoConfigEntry {
+ if errors.Is(err, repository.ErrNoConfigEntry) {
return entity.UnsetId, ErrNoIdentitySet
}
- if err == repository.ErrMultipleConfigEntry {
+ if errors.Is(err, repository.ErrMultipleConfigEntry) {
return entity.UnsetId, ErrMultipleIdentitiesSet
}
if err != nil {
@@ -58,7 +58,7 @@ func GetUserIdentityId(repo repository.Repo) (entity.Id, error) {
// IsUserIdentitySet say if the user has set his identity
func IsUserIdentitySet(repo repository.Repo) (bool, error) {
_, err := repo.LocalConfig().ReadString(identityConfigKey)
- if err == repository.ErrNoConfigEntry {
+ if errors.Is(err, repository.ErrNoConfigEntry) {
return false, nil
}
if err != nil {
diff --git a/repository/config.go b/repository/config.go
index c6880b7d..7e1ee6e8 100644
--- a/repository/config.go
+++ b/repository/config.go
@@ -2,6 +2,7 @@ package repository
import (
"errors"
+ "fmt"
"strconv"
"time"
)
@@ -11,6 +12,14 @@ var (
ErrMultipleConfigEntry = errors.New("multiple config entry for the given key")
)
+func newErrNoConfigEntry(key string) error {
+ return fmt.Errorf("%w: missing key %s", ErrNoConfigEntry, key)
+}
+
+func newErrMultipleConfigEntry(key string) error {
+ return fmt.Errorf("%w: duplicated key %s", ErrMultipleConfigEntry, key)
+}
+
// Config represent the common function interacting with the repository config storage
type Config interface {
ConfigRead
@@ -96,7 +105,7 @@ func (m *mergedConfig) ReadBool(key string) (bool, error) {
if err == nil {
return v, nil
}
- if err != ErrNoConfigEntry && err != ErrMultipleConfigEntry {
+ if !errors.Is(err, ErrNoConfigEntry) && !errors.Is(err, ErrMultipleConfigEntry) {
return false, err
}
return m.global.ReadBool(key)
@@ -107,7 +116,7 @@ func (m *mergedConfig) ReadString(key string) (string, error) {
if err == nil {
return val, nil
}
- if err != ErrNoConfigEntry && err != ErrMultipleConfigEntry {
+ if !errors.Is(err, ErrNoConfigEntry) && !errors.Is(err, ErrMultipleConfigEntry) {
return "", err
}
return m.global.ReadString(key)
@@ -118,7 +127,7 @@ func (m *mergedConfig) ReadTimestamp(key string) (time.Time, error) {
if err == nil {
return val, nil
}
- if err != ErrNoConfigEntry && err != ErrMultipleConfigEntry {
+ if !errors.Is(err, ErrNoConfigEntry) && !errors.Is(err, ErrMultipleConfigEntry) {
return time.Time{}, err
}
return m.global.ReadTimestamp(key)
diff --git a/repository/config_mem.go b/repository/config_mem.go
index 019bc111..55b12fd7 100644
--- a/repository/config_mem.go
+++ b/repository/config_mem.go
@@ -49,7 +49,7 @@ func (mc *MemConfig) ReadString(key string) (string, error) {
key = normalizeKey(key)
val, ok := mc.config[key]
if !ok {
- return "", ErrNoConfigEntry
+ return "", newErrNoConfigEntry(key)
}
return val, nil
diff --git a/repository/config_testing.go b/repository/config_testing.go
index f8a2762b..8c22934a 100644
--- a/repository/config_testing.go
+++ b/repository/config_testing.go
@@ -53,7 +53,7 @@ func testConfig(t *testing.T, config Config) {
}, configs)
_, err = config.ReadBool("section.true")
- require.Equal(t, ErrNoConfigEntry, err)
+ require.ErrorIs(t, err, ErrNoConfigEntry)
err = config.RemoveAll("section.nonexistingkey")
require.Error(t, err)
@@ -62,7 +62,7 @@ func testConfig(t *testing.T, config Config) {
require.NoError(t, err)
_, err = config.ReadString("section.key")
- require.Equal(t, ErrNoConfigEntry, err)
+ require.ErrorIs(t, err, ErrNoConfigEntry)
err = config.RemoveAll("nonexistingsection")
require.Error(t, err)
diff --git a/repository/gogit.go b/repository/gogit.go
index b14efbe5..01c47d41 100644
--- a/repository/gogit.go
+++ b/repository/gogit.go
@@ -2,6 +2,7 @@ package repository
import (
"bytes"
+ "errors"
"fmt"
"io/ioutil"
"os"
@@ -270,7 +271,7 @@ func (repo *GoGitRepo) GetCoreEditor() (string, error) {
if err == nil && val != "" {
return val, nil
}
- if err != nil && err != ErrNoConfigEntry {
+ if err != nil && !errors.Is(err, ErrNoConfigEntry) {
return "", err
}
diff --git a/repository/gogit_config.go b/repository/gogit_config.go
index 891e3ffb..afa652b1 100644
--- a/repository/gogit_config.go
+++ b/repository/gogit_config.go
@@ -119,7 +119,7 @@ func (cr *goGitConfigReader) ReadString(key string) (string, error) {
sectionName := split[0]
if !cfg.Raw.HasSection(sectionName) {
- return "", ErrNoConfigEntry
+ return "", newErrNoConfigEntry(key)
}
section := cfg.Raw.Section(sectionName)
@@ -127,24 +127,24 @@ func (cr *goGitConfigReader) ReadString(key string) (string, error) {
case len(split) == 2:
optionName := split[1]
if !section.HasOption(optionName) {
- return "", ErrNoConfigEntry
+ return "", newErrNoConfigEntry(key)
}
if len(section.OptionAll(optionName)) > 1 {
- return "", ErrMultipleConfigEntry
+ return "", newErrMultipleConfigEntry(key)
}
return section.Option(optionName), nil
default:
subsectionName := strings.Join(split[1:len(split)-1], ".")
optionName := split[len(split)-1]
if !section.HasSubsection(subsectionName) {
- return "", ErrNoConfigEntry
+ return "", newErrNoConfigEntry(key)
}
subsection := section.Subsection(subsectionName)
if !subsection.HasOption(optionName) {
- return "", ErrNoConfigEntry
+ return "", newErrNoConfigEntry(key)
}
if len(subsection.OptionAll(optionName)) > 1 {
- return "", ErrMultipleConfigEntry
+ return "", newErrMultipleConfigEntry(key)
}
return subsection.Option(optionName), nil
}