Skip to content

Commit

Permalink
Merge pull request #7745 from ethereum-optimism/10-18-feat_Throw_on_u…
Browse files Browse the repository at this point in the history
…nknown_key_error

feat(indexer): Throw when unknown key in toml
  • Loading branch information
roninjin10 authored Oct 25, 2023
2 parents ed341cd + 821dd5e commit 84167fb
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
9 changes: 8 additions & 1 deletion indexer/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,18 @@ func LoadConfig(log log.Logger, path string) (Config, error) {
data = []byte(os.ExpandEnv(string(data)))
log.Debug("parsed config file", "data", string(data))

if _, err := toml.Decode(string(data), &cfg); err != nil {
md, err := toml.Decode(string(data), &cfg)
if err != nil {
log.Error("failed to decode config file", "err", err)
return cfg, err
}

if len(md.Undecoded()) > 0 {
log.Error("unknown fields in config file", "fields", md.Undecoded())
err = fmt.Errorf("unknown fields in config file: %v", md.Undecoded())
return cfg, err
}

if cfg.Chain.Preset == DevnetPresetId {
preset, err := DevnetPreset()
if err != nil {
Expand Down
46 changes: 46 additions & 0 deletions indexer/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,3 +257,49 @@ func TestLocalDevnet(t *testing.T) {

require.Equal(t, devnetPreset.ChainConfig.L1Contracts, conf.Chain.L1Contracts)
}

func TestThrowsOnUnknownKeys(t *testing.T) {
logger := testlog.Logger(t, log.LvlInfo)
tmpfile, err := os.CreateTemp("", "test.toml")
require.NoError(t, err)
defer os.Remove(tmpfile.Name())
defer tmpfile.Close()

testData := `
[chain]
unknown_key = 420
preset = 420
[rpcs]
l1-rpc = "https://l1.example.com"
l2-rpc = "https://l2.example.com"
[db]
another_unknownKey = 420
host = "127.0.0.1"
port = 5432
user = "postgres"
password = "postgres"
name = "indexer"
[http]
host = "127.0.0.1"
port = 8080
[metrics]
host = "127.0.0.1"
port = 7300
`

data := []byte(testData)
err = os.WriteFile(tmpfile.Name(), data, 0644)
require.NoError(t, err)
defer os.Remove(tmpfile.Name())

err = tmpfile.Close()
require.NoError(t, err)

_, err = LoadConfig(logger, tmpfile.Name())
require.Error(t, err)
require.Contains(t, err.Error(), "unknown fields in config file")
}

0 comments on commit 84167fb

Please sign in to comment.