Skip to content

Commit

Permalink
fix: clearer err msg when overriding an unknown provider type (#665)
Browse files Browse the repository at this point in the history
  • Loading branch information
wesl-ee authored Aug 12, 2024
1 parent cf19513 commit fb3f4de
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
9 changes: 9 additions & 0 deletions cmd/slinky/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,14 +133,23 @@ func ReadOracleConfigWithOverrides(path string, marketMapProvider string) (confi
// oracleConfigFromViper unmarshals an oracle config from viper, validates it, and returns it.
func oracleConfigFromViper() (config.OracleConfig, error) {
var cfg config.OracleConfig
unmarshalMetadata := mapstructure.Metadata{}
if err := viper.Unmarshal(&cfg, func(c *mapstructure.DecoderConfig) {
c.ErrorUnused = true
c.Metadata = &unmarshalMetadata
}); err != nil {
return config.OracleConfig{}, err
}

// for each api-provider, we'll have to manually fill the endpoints
for _, provider := range cfg.Providers {
// if a provider was not unmarshaled correctly, surface that error
if provider.Name == "" {
if len(unmarshalMetadata.Unset) > 0 {
return config.OracleConfig{}, fmt.Errorf("overridden key %s does not correspond to a known provider", unmarshalMetadata.Unset[0])
}
}

// Update API endpoints
for i, endpoint := range provider.API.Endpoints {
provider.API.Endpoints[i], _ = updateEndpointFromEnvironment(endpoint, provider.Name, i, "api")
Expand Down
46 changes: 46 additions & 0 deletions cmd/slinky/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,52 @@ func TestReadOracleConfigWithOverrides(t *testing.T) {
require.Equal(t, expectedConfig.UpdateInterval, cfg.UpdateInterval)
require.Equal(t, expectedConfig.Metrics.PrometheusServerAddress, cfg.Metrics.PrometheusServerAddress)
})

t.Run("overriding a nonexistent provider via config fails", func(t *testing.T) {
// create a temp file in the current directory
tmpfile, err := os.CreateTemp("", "slinky-config-*.json")
require.NoError(t, err)

defer os.Remove(tmpfile.Name())

overrides := fmt.Sprintf(`
{
"updateInterval": "%s",
"metrics": {
"prometheusServerAddress": "%s"
},
"providers": {
"doesNotExist": {
"api": {
"endpoints": [
{
"url": "%s"
},
{
"url": "%s",
"authentication": {
"apiKey": "%s",
"apiKeyHeader": "%s"
}
}
]
}
}
}
}
`,
updateIntervalOverride,
prometheusServerOverride,
raydium.DefaultAPIConfig.Endpoints[0].URL,
endpointOverride.URL,
endpointOverride.Authentication.APIKey,
endpointOverride.Authentication.APIKeyHeader,
)
tmpfile.Write([]byte(overrides))

_, err = cmdconfig.ReadOracleConfigWithOverrides(tmpfile.Name(), marketmap.Name)
require.ErrorContains(t, err, "overridden key")
})
}

func TestOracleConfigWithExtraKeys(t *testing.T) {
Expand Down

0 comments on commit fb3f4de

Please sign in to comment.