{"id":"e8783ed9958e92aa","repo":"gohugoio/hugo","slug":"cannot-encode-an-empty-string","errorCode":null,"errorMessage":"cannot encode an empty string","messagePattern":"cannot encode an empty string","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"tpl/images/images.go","lineNumber":143,"sourceCode":"\t\tLevel     string // error correction level; one of low, medium, quartile, or high\n\t\tScale     int    // number of image pixels per QR code module\n\t\tTargetDir string // target directory relative to publishDir\n\t}{\n\t\tLevel: qrDefaultErrorCorrectionLevel,\n\t\tScale: qrDefaultScale,\n\t}\n\n\tif len(args) == 0 || len(args) > 2 {\n\t\treturn nil, errors.New(\"requires 1 or 2 arguments\")\n\t}\n\n\ttext, err := cast.ToStringE(args[0])\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\n\tif text == \"\" {\n\t\treturn nil, errors.New(\"cannot encode an empty string\")\n\t}\n\n\tif len(args) == 2 {\n\t\terr := mapstructure.WeakDecode(args[1], &opts)\n\t\tif err != nil {\n\t\t\treturn nil, err\n\t\t}\n\t}\n\n\tlevel, ok := qrErrorCorrectionLevels[opts.Level]\n\tif !ok {\n\t\treturn nil, errors.New(\"error correction level must be one of low, medium, quartile, or high\")\n\t}\n\n\tif opts.Scale < 2 {\n\t\treturn nil, errors.New(\"scale must be an integer greater than or equal to 2\")\n\t}\n","sourceCodeStart":125,"sourceCodeEnd":161,"githubUrl":"https://github.com/gohugoio/hugo/blob/8a468df065a75c1c7cf9f6850f32148746590ea5/tpl/images/images.go#L125-L161","documentation":"`images.QR` encodes text into a QR code PNG resource. After casting the first argument to a string, Hugo requires it to be non-empty because an empty payload produces no meaningful QR code; it fails fast instead of generating a useless image.","triggerScenarios":"Calling `{{ images.QR \"\" }}` or `{{ images.QR $text }}` where `$text` evaluates to an empty string.","commonSituations":"Generating QR codes from front matter (e.g. `.Params.url`) or `.Permalink` on pages where the value is missing; templating a URL from site params that aren't set in the environment's config.","solutions":["Wrap the call in a guard: `{{ with .Params.url }}{{ $qr := images.QR . }}...{{ end }}`.","Ensure the source value (front matter field, site param, permalink) is populated for every page rendering the template.","Trace where the empty string comes from (e.g. `printf` the value with `warnf`) before calling QR."],"exampleFix":"<!-- before -->\n{{ $qr := images.QR .Params.shortlink }}\n<!-- after -->\n{{ with .Params.shortlink }}\n  {{ $qr := images.QR . }}\n  <img src=\"{{ $qr.RelPermalink }}\">\n{{ end }}","handlingStrategy":"validation","validationCode":"{{ $text := .Params.qrText | default \"\" }}\n{{ if $text }}\n  {{ $qr := images.QR $text }}\n{{ else }}\n  {{ errorf \"qrText param missing on %s\" .Path }}\n{{ end }}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never pass an empty string to images.QR — guard with `if` or `with` first","Validate front-matter params that feed QR generation, and fail loudly with errorf when required data is missing","Trim whitespace before checking: whitespace-only input encodes but is usually a data bug"],"tags":["hugo","templates","images","qr"],"analyzedSha":"8a468df065a75c1c7cf9f6850f32148746590ea5","analyzedAt":"2026-07-31T21:23:07.045Z","schemaVersion":2}