{"record":{"id":"6fe0318cd4724a7c","repo":"antonmedv/fx","slug":"panics-with-the-raw-toml-input-bytes-in-when-to","errorCode":null,"errorMessage":"<panics with the raw TOML input bytes (in) when toml.Unmarshal fails>","messagePattern":"<panics with the raw TOML input bytes \\(in\\) when toml\\.Unmarshal fails>","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/toml/toml.go","lineNumber":33,"sourceCode":"type jnode interface{}\n\ntype jobject struct {\n\tfields []jfield\n}\n\ntype jfield struct {\n\tkey string\n\tval jnode\n}\n\ntype jarray struct {\n\telems []jnode\n}\n\nfunc ToJSON(in []byte) ([]byte, error) {\n\tvar typed any\n\tif err := toml.Unmarshal(in, &typed); err != nil {\n\t\tpanic(in)\n\t}\n\n\troot := &jobject{}\n\tp := unstable.Parser{}\n\tp.Reset(in)\n\n\taotActive := map[string]int{} // path -> current index for that AOT\n\taotCount := map[string]int{}  // path -> how many elems seen\n\tcurrentTablePath := []string{}\n\n\tfor p.NextExpression() {\n\t\te := p.Expression()\n\t\tswitch e.Kind {\n\t\tcase unstable.Table:\n\t\t\tcurrentTablePath = keyParts(e.Key())\n\t\t\t_ = ensureContainer(root, currentTablePath, aotActive)\n\n\t\tcase unstable.ArrayTable:","sourceCodeStart":15,"sourceCodeEnd":51,"githubUrl":"https://github.com/antonmedv/fx/blob/4f31cd3a0c5d66f1b4290a2719bab14a5cee8ebe/internal/toml/toml.go#L15-L51","documentation":"ToJSON converts raw TOML bytes to JSON. When toml.Unmarshal fails to parse the input, the function panics with the raw input bytes rather than returning the parser error. The error path is treated as an internal invariant because the caller is expected to feed it TOML that the unstable.Parser can also consume.","triggerScenarios":"Calling toml.ToJSON with bytes that are not valid TOML — syntax errors, mismatched brackets, invalid key names — causing toml.Unmarshal to return an error.","commonSituations":"Users piping a malformed .toml file (or accidentally a JSON/YAML file) into fx with --toml; truncated config files; TOML features unsupported by the go-toml version in use.","solutions":["Validate the TOML input with a standalone parser (e.g. toml.Validate or a test Unmarshal into a scratch value) before calling ToJSON","Fix the syntax error in the TOML source file reported by the parse failure","Ensure the correct file is being passed (not JSON/YAML) and the --toml flag or .toml extension is intended"],"exampleFix":"// before\nvar typed any\nif err := toml.Unmarshal(in, &typed); err != nil {\n\tpanic(in)\n}\n// after\nvar typed any\nif err := toml.Unmarshal(in, &typed); err != nil {\n\treturn nil, fmt.Errorf(\"invalid TOML: %w\", err)\n}","handlingStrategy":"validation","validationCode":"// validate TOML before calling ToJSON\nvar probe any\nif err := toml.Unmarshal(input, &probe); err != nil {\n\treturn fmt.Errorf(\"invalid TOML input: %w\", err)\n}\nout, err := toml.ToJSON(input)","typeGuard":null,"tryCatchPattern":"// guard against the panic with recover\nfunc safeToJSON(in []byte) (out []byte, err error) {\n\tdefer func() {\n\t\tif r := recover(); r != nil {\n\t\t\terr = fmt.Errorf(\"invalid TOML: %v\", r)\n\t\t}\n\t}()\n\treturn toml.ToJSON(in)\n}","preventionTips":["Lint TOML files with a TOML linter (e.g. taplo) before processing","Never pass JSON/YAML files with --toml; rely on the .toml extension auto-detection","Validate user-supplied config files at pipeline boundaries, not inside the converter"],"tags":["go","panic","toml","parse-error"],"backgroundTag":"toml-parse-error","analyzedSha":"4f31cd3a0c5d66f1b4290a2719bab14a5cee8ebe","analyzedAt":"2026-09-02T02:17:47.344Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T06:17:21.866Z"}