{"record":{"id":"e33ac3b3e11af2a4","repo":"vectordotdev/vector","slug":"key-is-not-found","errorCode":null,"errorMessage":"Key is not found: {:?}","messagePattern":"Key is not found: (.+?)","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"lib/vector-core/src/event/log_event.rs","lineNumber":728,"sourceCode":"    type Error = crate::Error;\n\n    fn try_into(self) -> Result<serde_json::Value, Self::Error> {\n        Ok(serde_json::to_value(&self.inner.fields)?)\n    }\n}\n\n#[cfg(any(test, feature = \"test\"))]\nimpl<T> std::ops::Index<T> for LogEvent\nwhere\n    T: AsRef<str>,\n{\n    type Output = Value;\n\n    fn index(&self, key: T) -> &Value {\n        self.parse_path_and_get_value(key.as_ref())\n            .ok()\n            .flatten()\n            .unwrap_or_else(|| panic!(\"Key is not found: {:?}\", key.as_ref()))\n    }\n}\n\nimpl<K, V> Extend<(K, V)> for LogEvent\nwhere\n    K: AsRef<str>,\n    V: Into<Value>,\n{\n    fn extend<I: IntoIterator<Item = (K, V)>>(&mut self, iter: I) {\n        for (k, v) in iter {\n            if let Ok(path) = parse_target_path(k.as_ref()) {\n                self.insert(&path, v.into());\n            }\n        }\n    }\n}\n\n// Allow converting any kind of appropriate key/value iterator directly into a LogEvent.","sourceCodeStart":710,"sourceCodeEnd":746,"githubUrl":"https://github.com/vectordotdev/vector/blob/3708c39b12a93212ed8b8d7510b4cc7769cb5864/lib/vector-core/src/event/log_event.rs#L710-L746","documentation":"`LogEvent` implements `std::ops::Index` only under `#[cfg(any(test, feature = \"test\"))]` (lib/vector-core/src/event/log_event.rs:721-736) to give tests ergonomic `event[\"field\"]` access. The index lookup goes through `parse_path_and_get_value`; if the path fails to parse or the key is absent, there is no `None` to return from `Index`, so it panics with the missing key. Production code is expected to use the fallible `get` API instead.","triggerScenarios":"In a test (or code built with the `test` feature), indexing an event at a key or path that was never inserted: `event[\"message\"]` on an event built without `message`, or a malformed path like `\"a..b\"` that fails `parse_path_and_get_value`. The same code compiled without the `test` feature simply does not compile, so this panic is test-only.","commonSituations":"Unit tests where the fixture builder forgets to insert an asserted field; renaming a field in the code under test but not in the test's index expressions; tests using VRL-style path strings (`\"a.b[0]\"`) that do not parse as expected; enabling the `test` feature in CI integration code that then indexes optional fields.","solutions":["Use the fallible accessor in assertions: `event.get(\"message\").expect(\"message set\")` instead of `event[\"message\"]`.","Fix the fixture: insert the key (or fix the path string) so the lookup succeeds before indexing.","For path lookups, prefer `event.get(\"a.b\")` and assert on the returned `Option<&Value>` to get a clear failure message."],"exampleFix":"// before\nassert_eq!(event[\"message\"], \"hello\"); // panics 'Key is not found' if unset\n\n// after\nassert_eq!(\n    event.get(\"message\").expect(\"message must be set\").\n        as_string_cloned().expect(\"message is a string\"),\n    \"hello\"\n);","handlingStrategy":"type-guard","validationCode":"if event.get(\"message\").is_some() {\n    assert_eq!(event[\"message\"], \"hello\");\n}","typeGuard":"fn has_key(event: &LogEvent, key: &str) -> bool { event.get(key).is_some() }","tryCatchPattern":null,"preventionTips":["Prefer event.get(key) with expect() in tests — the panic message then names your expectation, not the raw key.","Build fixtures with explicit inserts for every key later indexed.","Remember the Index impl exists only under cfg(test)/feature \"test\" — production code must use get/insert."],"tags":["rust","vector","log-event","unit-testing","panic"],"backgroundTag":"missing-map-key","analyzedSha":"3708c39b12a93212ed8b8d7510b4cc7769cb5864","analyzedAt":"2026-08-20T07:02:18.786Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T20:17:18.057Z"}