{"record":{"id":"bd43812287f0b151","repo":"dgraph-io/badger","slug":"errkeynotfound","errorCode":"ErrKeyNotFound","errorMessage":"Key not found","messagePattern":"Key not found","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"info","filePath":"errors.go","lineNumber":24,"sourceCode":"package badger\n\nimport (\n\tstderrors \"errors\"\n\t\"math\"\n)\n\nconst (\n\t// ValueThresholdLimit is the maximum permissible value of opt.ValueThreshold.\n\tValueThresholdLimit = math.MaxUint16 - 16 + 1\n)\n\nvar (\n\t// ErrValueLogSize is returned when opt.ValueLogFileSize option is not within the valid\n\t// range.\n\tErrValueLogSize = stderrors.New(\"Invalid ValueLogFileSize, must be in range [1MB, 2GB)\")\n\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,","sourceCodeStart":6,"sourceCodeEnd":42,"githubUrl":"https://github.com/dgraph-io/badger/blob/2a001d466f6b71a917319a1db41f99860e16e269/errors.go#L6-L42","documentation":"ErrKeyNotFound is the sentinel returned by Txn.Get when no live version of the requested key is visible to the transaction's read timestamp. It also flows out of helpers like findFirstInvalidTxn. In Badger, absence of a key is an error return, not a nil item.","triggerScenarios":"tx.Get(k) on a key never written, deleted, written in an uncommitted transaction, or whose newest version is older than the reader's read timestamp (managed mode with a too-high ts); backup/restore code assuming a key exists (backup_test.go:165).","commonSituations":"Existence checks, read-after-write races before the writer commits, opening an empty/new DB directory, managed-mode timestamp mismatches.","solutions":["Treat err == ErrKeyNotFound as the 'absent' case, not a fatal failure","If the key should exist, verify the writing transaction's Commit succeeded before reading","In managed mode, ensure the read timestamp is >= the write's commit timestamp","Confirm you opened the same directory (not a fresh/empty path)"],"exampleFix":"// before\nitem, err := tx.Get(key)\nval, _ := item.ValueCopy(nil) // panics when err != nil\n// after\nitem, err := tx.Get(key)\nif err != nil {\n    if errors.Is(err, badger.ErrKeyNotFound) {\n        return nil // key absent\n    }\n    return err\n}","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"item, err := txn.Get(key)\nif errors.Is(err, badger.ErrKeyNotFound) {\n    return handleAbsent(key)\n}\nif err != nil {\n    return err\n}","preventionTips":["Never dereference item without checking err first","Ensure the writer's Commit succeeded before reading the key","In managed mode, use a read timestamp <= the write's commit ts","Log/annotate ErrKeyNotFound with the key to ease debugging"],"tags":["badger","key-not-found","read-path"],"backgroundTag":"key-not-found","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"}