Skip to content

Commit

Permalink
config: return error when toml.DecodeFile failed. (#51828) (#52577)
Browse files Browse the repository at this point in the history
close #51399
  • Loading branch information
ti-chi-bot authored Apr 15, 2024
1 parent e5f699a commit 1fec74b
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
2 changes: 1 addition & 1 deletion config/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ go_test(
data = glob(["**"]),
embed = [":config"],
flaky = True,
shard_count = 23,
shard_count = 24,
deps = [
"//testkit/testsetup",
"//util/logutil",
Expand Down
5 changes: 4 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -1237,13 +1237,16 @@ func (c *Config) RemovedVariableCheck(confFile string) error {
// Load loads config options from a toml file.
func (c *Config) Load(confFile string) error {
metaData, err := toml.DecodeFile(confFile, c)
if err != nil {
return err
}
if c.TokenLimit == 0 {
c.TokenLimit = 1000
}
// If any items in confFile file are not mapped into the Config struct, issue
// an error and stop the server from starting.
undecoded := metaData.Undecoded()
if len(undecoded) > 0 && err == nil {
if len(undecoded) > 0 {
var undecodedItems []string
for _, item := range undecoded {
undecodedItems = append(undecodedItems, item.String())
Expand Down
22 changes: 22 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1329,3 +1329,25 @@ func TestAutoScalerConfig(t *testing.T) {
conf.UseAutoScaler = false
})
}

func TestInvalidConfigWithDeprecatedConfig(t *testing.T) {
tmpDir := t.TempDir()
configFile := filepath.Join(tmpDir, "config.toml")

f, err := os.Create(configFile)
require.NoError(t, err)

_, err = f.WriteString(`
[log]
slow-threshold = 1000
[performance]
enforce-mpp = 1
`)
require.NoError(t, err)
require.NoError(t, f.Sync())

var conf Config
err = conf.Load(configFile)
require.Error(t, err)
require.Equal(t, err.Error(), "toml: line 5 (last key \"performance.enforce-mpp\"): incompatible types: TOML value has type int64; destination has type boolean")
}

0 comments on commit 1fec74b

Please sign in to comment.