{"id":"954661f2785aa203","repo":"spf13/viper","slug":"decoder-not-found-for-this-format","errorCode":null,"errorMessage":"decoder not found for this format","messagePattern":"decoder not found for this format","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"encoding.go","lineNumber":147,"sourceCode":"// Encoder implements the [EncoderRegistry] interface.\n//\n// Format is case-insensitive.\nfunc (r *DefaultCodecRegistry) Encoder(format string) (Encoder, error) {\n\tencoder, ok := r.codec(format)\n\tif !ok {\n\t\treturn nil, errors.New(\"encoder not found for this format\")\n\t}\n\n\treturn encoder, nil\n}\n\n// Decoder implements the [DecoderRegistry] interface.\n//\n// Format is case-insensitive.\nfunc (r *DefaultCodecRegistry) Decoder(format string) (Decoder, error) {\n\tdecoder, ok := r.codec(format)\n\tif !ok {\n\t\treturn nil, errors.New(\"decoder not found for this format\")\n\t}\n\n\treturn decoder, nil\n}\n\nfunc (r *DefaultCodecRegistry) codec(format string) (Codec, bool) {\n\tr.mu.Lock()\n\tdefer r.mu.Unlock()\n\n\tformat = strings.ToLower(format)\n\n\tif r.codecs != nil {\n\t\tcodec, ok := r.codecs[format]\n\t\tif ok {\n\t\t\treturn codec, true\n\t\t}\n\t}\n","sourceCodeStart":129,"sourceCodeEnd":165,"githubUrl":"https://github.com/spf13/viper/blob/528f7416c4b56a4948673984b190bf8713f0c3c4/encoding.go#L129-L165","documentation":"Returned by DefaultCodecRegistry.Decoder (encoding.go:144-150) when r.codec(format) finds no match. Same codec table as the encoder: only yaml/yml, json, toml, dotenv/env are built in. It surfaces through unmarshalReader (viper.go:1800) wrapped as ConfigParseError whenever Viper reads/parses a config stream.","triggerScenarios":"Calling ReadConfig, ReadConfigAs, WatchConfig (on change), or unmarshaling a remote/env source whose getConfigType() resolves to a format with no built-in decoder — 'ini', 'hcl', 'tfvars', 'properties', 'props', 'prop' — or a custom format with no codec registered via WithDecoderRegistry.","commonSituations":"Reading a legacy .ini/.hcl/.properties config file after a Viper upgrade that dropped the bundled parser; remote KV store (consul/etcd) with SetConfigType(\"hcl\"); custom decoder registry missing RegisterCodec for the format you read.","solutions":["Convert the config to a built-in format (json/yaml/toml/env) or read from one.","Register a decoder for the format: r := NewCodecRegistry(); r.RegisterCodec(\"ini\", iniCodec{}); v := NewWithOptions(WithDecoderRegistry(r)).","Double-check that SetConfigType matches an actually-registered codec, not merely an entry in SupportedExts (which lists more formats than the default registry supports)."],"exampleFix":"// before\nv.SetConfigType(\"ini\")\n_ = v.ReadConfig(bytes.NewReader(data)) // -> ConfigParseError: decoder not found for this format\n\n// after\nr := NewCodecRegistry()\n_ = r.RegisterCodec(\"ini\", myIniCodec{})\nv := NewWithOptions(WithDecoderRegistry(r))\nv.SetConfigType(\"ini\")\n_ = v.ReadConfig(bytes.NewReader(data))","handlingStrategy":"validation","validationCode":"var hasBuiltinDecoder = map[string]bool{\"json\": true, \"yaml\": true, \"yml\": true, \"toml\": true, \"env\": true, \"dotenv\": true}\nfunc canDecode(format string) bool { return hasBuiltinDecoder[strings.ToLower(format)] }\n\n// usage before ReadConfig\ncfgType := strings.ToLower(v.GetConfigType())\nif cfgType == \"\" { cfgType = strings.TrimPrefix(filepath.Ext(configPath), \".\") }\nif !canDecode(cfgType) {\n    // register a decoder or convert the source to a built-in format\n}","typeGuard":null,"tryCatchPattern":"if err := v.ReadConfig(r); err != nil {\n    var parseErr viper.ConfigParseError\n    if errors.As(err, &parseErr) && strings.Contains(parseErr.Error(), \"decoder not found\") {\n        // format lacks a registered decoder; register one or change SetConfigType\n    }\n}","preventionTips":["If you read .ini/.hcl/.properties, register a codec for it explicitly at startup.","Standardize config files on json/yaml/toml to stay within built-in codec coverage.","Add a smoke test that loads each bundled example config file."],"tags":["encoding","codec","config-read","registry"],"analyzedSha":"528f7416c4b56a4948673984b190bf8713f0c3c4","analyzedAt":"2026-08-04T21:46:12.352Z","schemaVersion":2}