{"record":{"id":"190f84b0a90d6274","repo":"OpenNHP/opennhp","slug":"file-path-cannot-be-empty","errorCode":null,"errorMessage":"file path cannot be empty","messagePattern":"file path cannot be empty","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"nhp/utils/utils.go","lineNumber":117,"sourceCode":"\tif err != nil {\n\t\treturn \"\", err\n\t}\n\n\ttempPath := file.Name()\n\n\tif err := file.Close(); err != nil {\n\t\treturn \"\", err\n\t}\n\n\treturn tempPath, nil\n}\n\nfunc SaveStructAsJsonFile(filePath string, data any) error {\n\tif data == nil {\n\t\treturn fmt.Errorf(\"data cannot be nil\")\n\t}\n\tif filePath == \"\" {\n\t\treturn fmt.Errorf(\"file path cannot be empty\")\n\t}\n\n\tjsonData, err := json.MarshalIndent(data, \"\", \"  \")\n\tif err != nil {\n\t\treturn fmt.Errorf(\"failed to marshal data to JSON: %w\", err)\n\t}\n\n\terr = os.WriteFile(filePath, jsonData, 0644) //nolint:gosec // G306: Generic utility - callers determine sensitivity\n\tif err != nil {\n\t\treturn fmt.Errorf(\"failed to write JSON to file: %w\", err)\n\t}\n\n\treturn nil\n}\n\nfunc LoadJsonFileAsStruct(filePath string) (any, error) {\n\tif filePath == \"\" {\n\t\treturn nil, fmt.Errorf(\"file path cannot be empty\")","sourceCodeStart":99,"sourceCodeEnd":135,"githubUrl":"https://github.com/OpenNHP/opennhp/blob/6e04ca5ff03222a699c24205cd4bf8fee9af7ffe/nhp/utils/utils.go#L99-L135","documentation":"SaveStructAsJsonFile requires a non-empty filePath; an empty string returns 'file path cannot be empty' before marshaling. This prevents os.WriteFile from failing with a confusing path error at the write stage.","triggerScenarios":"Calling SaveStructAsJsonFile(\"\", data) — typically when a path variable was never set, a template/config field for the output path is missing, or a Join of empty components yielded \"\".","commonSituations":"Missing config field specifying output directory; filepath.Join with all-empty parts; refactor left a hardcoded path removed.","solutions":["Ensure the path is computed/loaded before the call and fail earlier with context","Validate config at startup: required output path field present and non-empty","Use filepath.Join with a guaranteed base directory constant"],"exampleFix":"// before\nSaveStructAsJsonFile(cfg.TAPath, payload) // cfg.TAPath == \"\"\n// after\nif cfg.TAPath == \"\" { return fmt.Errorf(\"ta_path missing in config\") }\nSaveStructAsJsonFile(cfg.TAPath, payload)","handlingStrategy":"validation","validationCode":"if filePath == \"\" { return fmt.Errorf(\"output path required\") }\nif dir := filepath.Dir(filePath); dir != \".\" { if err := os.MkdirAll(dir, 0o755); err != nil { return err } }","typeGuard":null,"tryCatchPattern":"if err := SaveStructAsJsonFile(path, data); err != nil { return fmt.Errorf(\"write %q: %w\", path, err) }","preventionTips":["Validate required path fields at config startup","Construct paths with a fixed base directory constant","Check filepath.Join inputs for empty components"],"tags":["validation","filesystem","config"],"backgroundTag":"empty-required-field","analyzedSha":"6e04ca5ff03222a699c24205cd4bf8fee9af7ffe","analyzedAt":"2026-09-07T15:44:59.941Z","contentChangedAt":"2026-09-07T15:44:59.941Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}