{"record":{"id":"aedf8a9fb0352487","repo":"OpenNHP/opennhp","slug":"failed-to-generate-uuid","errorCode":null,"errorMessage":"failed to generate UUID: ","messagePattern":"failed to generate UUID: ","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"nhp/utils/uuid.go","lineNumber":21,"sourceCode":"import (\n\t\"errors\"\n\t\"fmt\"\n\t\"math/rand\"\n\t\"time\"\n\n\t\"github.com/google/uuid\"\n)\n\n// NewUUID generates a UUID using math/rand. For cryptographically secure UUIDs,\n// use GenerateUUIDv4() instead which uses crypto/rand.\n//\n//nolint:gosec // G404: math/rand is acceptable for non-security UUID generation\nfunc NewUUID() (string, error) {\n\trng := rand.New(rand.NewSource(time.Now().UnixNano()))\n\tuuid := make([]byte, 16)\n\t_, err := rng.Read(uuid)\n\tif err != nil {\n\t\treturn \"\", errors.New(\"failed to generate UUID: \" + err.Error())\n\t}\n\n\t// Set version bits (version 4)\n\tuuid[6] = (uuid[6] & 0x0F) | 0x40\n\t// Set variant bits (variant 1)\n\tuuid[8] = (uuid[8] & 0x3F) | 0x80\n\n\treturn fmt.Sprintf(\"%x-%x-%x-%x-%x\", uuid[0:4], uuid[4:6], uuid[6:8], uuid[8:10], uuid[10:]), nil\n}\n\n// RandNumber returns a random 4-digit number (1000-10999) for non-security purposes.\n//\n//nolint:gosec // G404: math/rand is acceptable for non-security random numbers\nfunc RandNumber() int {\n\trng := rand.New(rand.NewSource(time.Now().UnixNano()))\n\trandomNumber := rng.Intn(10000)\n\tif randomNumber < 1000 {\n\t\trandomNumber += 1000","sourceCodeStart":3,"sourceCodeEnd":39,"githubUrl":"https://github.com/OpenNHP/opennhp/blob/6e04ca5ff03222a699c24205cd4bf8fee9af7ffe/nhp/utils/uuid.go#L3-L39","documentation":"NewUUID builds a v4 UUID from a math/rand source seeded with the current time; if reading 16 random bytes from that source fails (essentially never for math/rand, but possible in principle), the raw error is wrapped into this message and returned.","triggerScenarios":"rng.Read on the freshly seeded *rand.Rand returns a non-nil error — practically only when the error is induced in tests or the rand source is replaced by a failing implementation.","commonSituations":"Seen in TestUUID when injecting a failing reader; not expected in production with the default time-seeded math/rand source.","solutions":["Retry the call; a transient rand failure is practically impossible and safe to retry","If this occurs in tests, fix the injected failing rand source","Consider crypto/rand-based UUID generation if the UUID must be unguessable (which would eliminate this path's realism concerns differently)","Inspect the wrapped err text after the colon for the actual root cause"],"exampleFix":"// before\nid, err := NewUUID()\nif err != nil {\n    return err\n}\n// after\nid, err := NewUUID()\nif err != nil {\n    log.Warnf(\"uuid generation failed, retrying: %v\", err)\n    id, err = NewUUID()\n    if err != nil { return err }\n}","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"id, err := NewUUID()\nif err != nil {\n    // wrapped message includes root cause after 'failed to generate UUID: '\n    return fmt.Errorf(\"NewUUID: %w\", err)\n}","preventionTips":["Handle the error at call sites instead of ignoring it, even though it is near-impossible","Switch to crypto/rand (or google/uuid) for security-sensitive identifiers","Keep the error wrap intact so root causes are diagnosable"],"tags":["go","uuid","random"],"backgroundTag":"internal-invariant-violation","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"}