Skip to content

Commit c24c439

Browse files
authored
fix: proper error when parsing telemetry configuration (cosmos#12981)
When parsing `telemetry.global-labels` config the code assumes that the type will be an array. I saw an issue where someone edited the configuration in the wrong way and got the following error: ![photo_2022-08-21_08-02-21](https://user-images.githubusercontent.com/22855163/185793842-c5759a54-1860-4dd1-bdb4-b94f4dab3c16.jpg) Instead, I suggest here to print a proper error log to indicate what the issue is.
1 parent 695d7f5 commit c24c439

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

server/config/config.go

+12-5
Original file line numberDiff line numberDiff line change
@@ -287,11 +287,18 @@ func DefaultConfig() *Config {
287287
}
288288

289289
// GetConfig returns a fully parsed Config object.
290-
func GetConfig(v *viper.Viper) Config {
291-
globalLabelsRaw := v.Get("telemetry.global-labels").([]interface{})
290+
func GetConfig(v *viper.Viper) (Config, error) {
291+
globalLabelsRaw, ok := v.Get("telemetry.global-labels").([]interface{})
292+
if !ok {
293+
return Config{}, fmt.Errorf("failed to parse global-labels config")
294+
}
295+
292296
globalLabels := make([][]string, 0, len(globalLabelsRaw))
293-
for _, glr := range globalLabelsRaw {
294-
labelsRaw := glr.([]interface{})
297+
for idx, glr := range globalLabelsRaw {
298+
labelsRaw, ok := glr.([]interface{})
299+
if !ok {
300+
return Config{}, fmt.Errorf("failed to parse global label number %d from config", idx)
301+
}
295302
if len(labelsRaw) == 2 {
296303
globalLabels = append(globalLabels, []string{labelsRaw[0].(string), labelsRaw[1].(string)})
297304
}
@@ -356,7 +363,7 @@ func GetConfig(v *viper.Viper) Config {
356363
SnapshotInterval: v.GetUint64("state-sync.snapshot-interval"),
357364
SnapshotKeepRecent: v.GetUint32("state-sync.snapshot-keep-recent"),
358365
},
359-
}
366+
}, nil
360367
}
361368

362369
// ValidateBasic returns an error if min-gas-prices field is empty in BaseConfig. Otherwise, it returns nil.

server/start.go

+12-2
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,13 @@ func startStandAlone(ctx *Context, appCreator types.AppCreator) error {
208208
}
209209

210210
app := appCreator(ctx.Logger, db, traceWriter, ctx.Viper)
211-
_, err = startTelemetry(serverconfig.GetConfig(ctx.Viper))
211+
212+
config, err := serverconfig.GetConfig(ctx.Viper)
213+
if err != nil {
214+
return err
215+
}
216+
217+
_, err = startTelemetry(config)
212218
if err != nil {
213219
return err
214220
}
@@ -271,7 +277,11 @@ func startInProcess(ctx *Context, clientCtx client.Context, appCreator types.App
271277
return err
272278
}
273279

274-
config := serverconfig.GetConfig(ctx.Viper)
280+
config, err := serverconfig.GetConfig(ctx.Viper)
281+
if err != nil {
282+
return err
283+
}
284+
275285
if err := config.ValidateBasic(); err != nil {
276286
return err
277287
}

0 commit comments

Comments
 (0)