{"id":"943a3c6ae15c4006","repo":"gohugoio/hugo","slug":"failed-to-decode-media-types-w","errorCode":null,"errorMessage":"failed to decode media types: %w","messagePattern":"failed to decode media types: %w","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"media/config.go","lineNumber":216,"sourceCode":"\t\t\t}\n\t\t}\n\n\t\tfor k := range s {\n\t\t\tmediaType, found := types.GetByType(k)\n\t\t\tif !found {\n\t\t\t\treturn c, nil, fmt.Errorf(\"unknown media type %q\", k)\n\t\t\t}\n\t\t\tc.types = append(c.types, mediaType)\n\t\t}\n\n\t\tc.init(types)\n\n\t\treturn c, s, nil\n\t}\n\n\tns, err := config.DecodeNamespace[map[string]ContentTypeConfig](in, buildConfig)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"failed to decode media types: %w\", err)\n\t}\n\treturn ns, nil\n}\n\n// DecodeTypes decodes the given map of media types.\nfunc DecodeTypes(in map[string]any) (*config.ConfigNamespace[map[string]MediaTypeConfig, Types], error) {\n\tbuildConfig := func(v any) (Types, any, error) {\n\t\tm, err := hmaps.ToStringMapE(v)\n\t\tif err != nil {\n\t\t\treturn nil, nil, err\n\t\t}\n\t\tif m == nil {\n\t\t\tm = map[string]any{}\n\t\t}\n\t\tm = hmaps.CleanConfigStringMap(m)\n\t\t// Merge with defaults.\n\t\thmaps.MergeShallow(m, defaultMediaTypesConfig)\n","sourceCodeStart":198,"sourceCodeEnd":234,"githubUrl":"https://github.com/gohugoio/hugo/blob/8a468df065a75c1c7cf9f6850f32148746590ea5/media/config.go#L198-L234","documentation":"This is the wrapping error returned when Hugo fails to decode the contentTypes config namespace. The underlying cause (%w) is either a structural decode failure (the section isn't a map, or a value can't be weak-decoded into ContentTypeConfig) or the 'unknown media type' validation error from the same buildConfig function. It surfaces at config load, halting the build.","triggerScenarios":"config.DecodeNamespace failing while building ContentTypes: passing a non-map value for contentTypes, a value that mapstructure.WeakDecode cannot coerce into ContentTypeConfig, or an unregistered media-type key (which produces error 391 as the wrapped cause).","commonSituations":"Malformed contentTypes section in hugo.toml/yaml/json (e.g. a string or array where a table/map is expected); wrong nesting of keys; unknown media type keys; merging config from themes/modules where types conflict.","solutions":["Read the wrapped error after the colon — it names the exact cause (often 'unknown media type ...') and fix that.","Ensure contentTypes is a map of MIME-string keys to tables, matching the documented shape.","Validate the merged config with 'hugo config' to spot values injected by themes or modules."],"exampleFix":"# before (hugo.yaml)\ncontentTypes: text/markdown\n\n# after\ncontentTypes:\n  text/markdown: {}","handlingStrategy":"try-catch","validationCode":"// Ensure mediaTypes config values are tables/maps, not scalars\nfor k, v := range cfg.GetStringMap(\"mediaTypes\") {\n    if _, ok := v.(map[string]any); !ok {\n        return fmt.Errorf(\"mediaTypes.%s must be a table, got %T\", k, v)\n    }\n}","typeGuard":null,"tryCatchPattern":"conf, err := allconfig.LoadConfig(...)\nif err != nil {\n    // wrapped error chain: use errors.Unwrap / %+v to find the offending key\n    log.Fatalf(\"config decode failed: %+v\", err)\n}","preventionTips":["Keep mediaTypes entries as tables with known keys (suffixes, delimiter) only","Validate TOML/YAML syntax with a linter before running hugo","Unwrap the %w chain to find the exact malformed field instead of guessing"],"tags":["hugo","config","decode","content-types"],"analyzedSha":"8a468df065a75c1c7cf9f6850f32148746590ea5","analyzedAt":"2026-07-31T21:23:07.045Z","schemaVersion":2}