{"record":{"id":"ea3ae92c359b6897","repo":"JanDeDobbeleer/oh-my-posh","slug":"panic-err","errorCode":null,"errorMessage":"panic(err)","messagePattern":"panic\\(err\\)","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/template/strings.go","lineNumber":12,"sourceCode":"package template\n\nimport (\n\t\"unicode/utf8\"\n\n\t\"github.com/jandedobbeleer/oh-my-posh/src/generics\"\n)\n\nfunc trunc(length any, s string) string {\n\tc, err := generics.TryParseInt[int](length)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\trunes := []rune(s)\n\tif len(runes) <= c {\n\t\treturn s\n\t}\n\n\tif c < 0 {\n\t\treturn string(runes[len(runes)+c:])\n\t}\n\n\treturn string(runes[0:c])\n}\n\nfunc TruncE(length any, s string) string {\n\tc, err := generics.TryParseInt[int](length)\n\tif err != nil {\n\t\tpanic(err)","sourceCodeStart":1,"sourceCodeEnd":30,"githubUrl":"https://github.com/JanDeDobbeleer/oh-my-posh/blob/0976794618c5ed95de0985dded50de1b4dc914cb/src/template/strings.go#L1-L30","documentation":"The internal template function trunc (src/template/strings.go:12) panics when its first argument cannot be parsed into an int via generics.TryParseInt[int]. trunc is invoked from inside prompt templates, so a non-numeric length argument aborts template execution with a panic rather than a graceful error. The library treats a bad trunc length as a template-authoring bug that should surface loudly.","triggerScenarios":"A template calling trunc with a non-integer first argument, e.g. {{ trunc \"abc\" .Path }}, {{ trunc .SomeString .Path }}, or {{ trunc nil .Path }}. Any value that TryParseInt cannot coerce (letters, floats with fractions, arbitrary objects) triggers the panic.","commonSituations":"Theme authors passing the segment's width property (which may be a string or empty) directly into trunc; typos like trunc \"10\" with quotes working but trunc \"1O\" failing; using a template variable expected to hold a number that is actually unset and renders as an empty string.","solutions":["Pass a literal integer or a variable known to hold an integer: {{ trunc 30 .Path }}.","If the value may be unset, guard it: use a default first, e.g. {{ trunc (.Width | default 30) .Path }}.","Switch to TruncE only if you actually want the ellipsis variant - it panics identically, so fix the argument instead.","Test the template with oh-my-posh config render/debug locally to catch the panic before shipping the theme."],"exampleFix":"// before\n{{ trunc .Width .Path }}\n// after\n{{ trunc (.Width | default 40) .Path }}","handlingStrategy":"validation","validationCode":"// in a template, coerce the length before calling trunc:\n// {{ trunc (int .Width) .Path }} or {{ trunc (.Width | default 40) .Path }}\nfunction isTemplateInt(v) { return Number.isInteger(Number(v)) && String(v).trim() !== ''; }","typeGuard":"function isIntLike(v) {\n  if (typeof v === 'number') return Number.isInteger(v);\n  if (typeof v === 'string') return /^-?\\d+$/.test(v.trim());\n  return false;\n}","tryCatchPattern":"trunc panics inside template execution, so catch at the render boundary:\ntry {\n  const out = omp.render(cfg, 'json', '', options);\n} catch (e) {\n  if (String(e).includes('TryParseInt') || String(e).includes('invalid')) {\n    console.error('trunc received a non-integer length in a template:', e);\n  }\n}","preventionTips":["Always pass integer literals or guaranteed-numeric variables as trunc's first argument.","Use `| default N` on any possibly-empty template variable before feeding it to trunc.","Quote-check config values: numbers in TOML/YAML must be unquoted to stay numeric.","Run `oh-my-posh debug` on themes during development to surface template panics early."],"tags":["template","panic","truncation","config"],"backgroundTag":"invalid-function-argument-panic","analyzedSha":"0976794618c5ed95de0985dded50de1b4dc914cb","analyzedAt":"2026-08-31T23:41:19.708Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T05:18:18.240Z"}