{"record":{"id":"7bfdb46ab9f9bd9a","repo":"dgraph-io/badger","slug":"erremptykey","errorCode":"ErrEmptyKey","errorMessage":"Key cannot be empty","messagePattern":"Key cannot be empty","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"errors.go","lineNumber":40,"sourceCode":"\n\t// ErrKeyNotFound is returned when key isn't found on a txn.Get.\n\tErrKeyNotFound = stderrors.New(\"Key not found\")\n\n\t// ErrTxnTooBig is returned if too many writes are fit into a single transaction.\n\tErrTxnTooBig = stderrors.New(\"Txn is too big to fit into one request\")\n\n\t// ErrConflict is returned when a transaction conflicts with another transaction. This can\n\t// happen if the read rows had been updated concurrently by another transaction.\n\tErrConflict = stderrors.New(\"Transaction Conflict. Please retry\")\n\n\t// ErrReadOnlyTxn is returned if an update function is called on a read-only transaction.\n\tErrReadOnlyTxn = stderrors.New(\"No sets or deletes are allowed in a read-only transaction\")\n\n\t// ErrDiscardedTxn is returned if a previously discarded transaction is reused.\n\tErrDiscardedTxn = stderrors.New(\"This transaction has been discarded. Create a new one\")\n\n\t// ErrEmptyKey is returned if an empty key is passed on an update function.\n\tErrEmptyKey = stderrors.New(\"Key cannot be empty\")\n\n\t// ErrInvalidKey is returned if the key has a special !badger! prefix,\n\t// reserved for internal usage.\n\tErrInvalidKey = stderrors.New(\"Key is using a reserved !badger! prefix\")\n\n\t// ErrBannedKey is returned if the read/write key belongs to any banned namespace.\n\tErrBannedKey = stderrors.New(\"Key is using the banned prefix\")\n\n\t// ErrThresholdZero is returned if threshold is set to zero, and value log GC is called.\n\t// In such a case, GC can't be run.\n\tErrThresholdZero = stderrors.New(\n\t\t\"Value log GC can't run because threshold is set to zero\")\n\n\t// ErrNoRewrite is returned if a call for value log GC doesn't result in a log file rewrite.\n\tErrNoRewrite = stderrors.New(\n\t\t\"Value log GC attempt didn't result in any cleanup\")\n\n\t// ErrRejected is returned if a value log GC is called either while another GC is running, or","sourceCodeStart":22,"sourceCodeEnd":58,"githubUrl":"https://github.com/dgraph-io/badger/blob/2a001d466f6b71a917319a1db41f99860e16e269/errors.go#L22-L58","documentation":"ErrEmptyKey is returned when an update function is given a key of length zero (errors.go:40). Both Txn.modify (txn.go:360) and the IndexCache/Get path (db.go:1437) reject empty keys since Badger cannot store or look them up meaningfully.","triggerScenarios":"txn.Set(nil, val) or txn.Set([]byte{}, val); txn.Delete with an empty key; GetIndexFor/lookup APIs (db.go:1437) receiving an empty key; a variable holding a key that was never populated.","commonSituations":"Dynamic key construction where a prefix/format produced \"\", unmarshalling records with missing fields into keys, nil byte slices from earlier failed operations.","solutions":["Validate len(key) > 0 before calling Set/Delete/Get and skip or error on your side","Fix key construction so every key has a non-empty prefix/field","Guard against nil slices (a nil []byte has len 0)"],"exampleFix":"// before\ntxn.Set(key, value) // key may be empty -> ErrEmptyKey\n// after\nif len(key) == 0 {\n    return fmt.Errorf(\"skipping record with empty key\")\n}\ntxn.Set(key, value)","handlingStrategy":"validation","validationCode":"if len(key) == 0 {\n    return errors.New(\"refusing DB write with empty key\")\n}","typeGuard":"func validKey(key []byte) bool { return len(key) > 0 && !bytes.HasPrefix(key, []byte(\"!badger!\")) }","tryCatchPattern":"if err := txn.Set(key, val); err != nil {\n    if errors.Is(err, badger.ErrEmptyKey) {\n        return fmt.Errorf(\"empty key for record %v: %w\", rec, err)\n    }\n    return err\n}","preventionTips":["Validate keys at the boundary where records are built","Check for nil slices — nil []byte has length 0","Unit-test key construction with edge-case records"],"tags":["badger","validation","key","api-misuse"],"backgroundTag":"empty-key-rejected","analyzedSha":"2a001d466f6b71a917319a1db41f99860e16e269","analyzedAt":"2026-09-05T13:00:02.264Z","contentChangedAt":"2026-09-05T13:00:02.264Z","schemaVersion":2},"datasetVersion":"2026-09-12T17:17:11.597Z"}