{"record":{"id":"f05b6dffec75593e","repo":"OpenNHP/opennhp","slug":"data-cannot-be-nil","errorCode":null,"errorMessage":"data cannot be nil","messagePattern":"data cannot be nil","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"nhp/utils/utils.go","lineNumber":114,"sourceCode":"\nfunc GenerateTempFilePath(pattern string) (string, error) {\n\tfile, err := os.CreateTemp(\"\", pattern)\n\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","sourceCodeStart":96,"sourceCodeEnd":132,"githubUrl":"https://github.com/OpenNHP/opennhp/blob/6e04ca5ff03222a699c24205cd4bf8fee9af7ffe/nhp/utils/utils.go#L96-L132","documentation":"SaveStructAsJsonFile refuses to serialize a nil value: if the data parameter is nil it returns 'data cannot be nil' before any file I/O. This guards against writing a literal 'null' JSON file or a confusing marshal outcome. Callers (e.g. registerTAService) must pass a concrete struct/map value.","triggerScenarios":"Passing an untyped nil, a nil pointer of concrete type passed as any and checked as nil only for untyped nil is NOT caught — but direct nil, or a variable that is literally nil (nil map assigned to any interface value is non-nil interface; untyped nil is caught), typically from a failed lookup upstream returning nil data.","commonSituations":"Upstream API/registry call returned nil and the result was forwarded unchecked to SaveStructAsJsonFile; forgotten struct initialization; error path that skips population of the payload.","solutions":["Check the value is non-nil before calling, or return an error upstream instead","Initialize the struct with defaults when source data is missing","Handle the error from SaveStructAsJsonFile rather than ignoring it"],"exampleFix":"// before\nvar taService *TAService // nil after failed lookup\nSaveStructAsJsonFile(path, taService)\n// after\nif taService == nil { return fmt.Errorf(\"ta service not found\") }\nSaveStructAsJsonFile(path, taService)","handlingStrategy":"validation","validationCode":"if data == nil { return fmt.Errorf(\"refusing to save nil payload to %s\", filePath) }","typeGuard":"func nonNil(v any) bool { return v != nil }","tryCatchPattern":"if err := SaveStructAsJsonFile(path, payload); err != nil { return fmt.Errorf(\"save ta service: %w\", err) }","preventionTips":["Never forward possibly-nil lookup results straight into save helpers","Return errors from upstream lookups instead of nil values","Check pointer receivers for nil before building payload structs"],"tags":["json","validation","null"],"backgroundTag":"null-argument","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"}