{"record":{"id":"8c78ce47255ed6ba","repo":"gohugoio/hugo","slug":"wraperror-with-nil","errorCode":null,"errorMessage":"wrapError with nil","messagePattern":"wrapError with nil","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"hugolib/page.go","lineNumber":779,"sourceCode":"\n\t\to = append(o, of)\n\t}\n\treturn o\n}\n\ntype renderStringOpts struct {\n\tDisplay string\n\tMarkup  string\n}\n\nvar defaultRenderStringOpts = renderStringOpts{\n\tDisplay: \"inline\",\n\tMarkup:  \"\", // Will inherit the page's value when not set.\n}\n\nfunc (m *pageMetaSource) wrapError(err error, sourceFs afero.Fs) error {\n\tif err == nil {\n\t\tpanic(\"wrapError with nil\")\n\t}\n\n\tif m.f == nil {\n\t\t// No more details to add.\n\t\treturn fmt.Errorf(\"%q: %w\", m.Path(), err)\n\t}\n\n\treturn hugofs.AddFileInfoToError(err, m.f.FileInfo(), sourceFs)\n}\n\n// wrapError adds some more context to the given error if possible/needed\nfunc (ps *pageState) wrapError(err error) error {\n\treturn ps.m.wrapError(err, ps.s.h.SourceFs)\n}\n\nfunc (ps *pageState) getPageInfoForError() string {\n\ts := fmt.Sprintf(\"kind: %q, path: %q\", ps.Kind(), ps.Path())\n\tif ps.File() != nil {","sourceCodeStart":761,"sourceCodeEnd":797,"githubUrl":"https://github.com/gohugoio/hugo/blob/52c9bd7908b4d02d4d0ff8f82a888834d6ee10d2/hugolib/page.go#L761-L797","documentation":"Panic in pageMetaSource.wrapError when called with a nil error. wrapError exists solely to add file/source context to an existing error; a nil argument is a programming mistake in the caller. It is a defensive assertion to prevent silent no-op error wrapping.","triggerScenarios":"Hugo internal code calls m.wrapError(err) without first checking err != nil. Typically a logic bug where the error variable is nil at the call site (e.g. a function that returned nil error but the wrap is reached unconditionally).","commonSituations":"A bug introduced during refactoring of error handling paths. A new code path that wraps unconditionally. Reached only via malformed input that bypasses an earlier error check.","solutions":["If you see this in Hugo core, file an issue; it is an internal bug — wrapError must only be called with a non-nil error.","Update Hugo to the latest release where the call-site bug is likely fixed.","If patching locally, guard the call site: if err != nil { err = ps.wrapError(err) }."],"exampleFix":"// before (Hugo internal call site)\nreturn m.wrapError(err) // err may be nil\n\n// after\nif err != nil {\n    return m.wrapError(err)\n}\nreturn nil","handlingStrategy":"validation","validationCode":"// Hugo internal callers: never call wrapError with a nil error.\n//   if err != nil { err = ps.wrapError(err) }\n// API users cannot trigger this directly; it indicates an internal bug.","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Guard all wrapError call sites with err != nil.","Upgrade Hugo when this surfaces.","Report the call site from the stack trace upstream."],"tags":["errors","pages","panic","internal-invariant","programming-error"],"backgroundTag":null,"analyzedSha":"52c9bd7908b4d02d4d0ff8f82a888834d6ee10d2","analyzedAt":"2026-08-09T21:49:36.660Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}