{"record":{"id":"2f0e39dacb49629a","repo":"t8y2/dbx","slug":"checked-above","errorCode":null,"errorMessage":"checked above","messagePattern":"checked above","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"info","filePath":"crates/dbx-core/src/db/dynamodb_driver.rs","lineNumber":1198,"sourceCode":"        .await\n        .map_err(|error| dynamodb_sdk_error!(\"Failed to delete DynamoDB item\", error))?;\n    Ok(1)\n}\n\nfn json_document_to_item(doc_json: &str) -> Result<HashMap<String, AttributeValue>, String> {\n    let mut value: Value =\n        serde_json::from_str(doc_json).map_err(|error| format!(\"Invalid DynamoDB item JSON: {error}\"))?;\n    let object = value.as_object_mut().ok_or_else(|| \"DynamoDB item must be a JSON object\".to_string())?;\n    object.remove(\"_id\");\n    object.iter().map(|(key, value)| Ok((key.clone(), json_to_attribute_value(value)?))).collect()\n}\n\nfn validate_item_keys(table: &DynamoDbTableDescription, item: &HashMap<String, AttributeValue>) -> Result<(), String> {\n    for key in [Some(&table.partition_key), table.sort_key.as_ref()].into_iter().flatten() {\n        if !item.contains_key(&key.name) {\n            return Err(format!(\"DynamoDB item requires key attribute: {}\", key.name));\n        }\n        let value = item.get(&key.name).expect(\"checked above\");\n        let actual_type = match value {\n            AttributeValue::S(_) => \"S\",\n            AttributeValue::N(_) => \"N\",\n            AttributeValue::B(_) => \"B\",\n            _ => \"non-scalar\",\n        };\n        if actual_type != key.attribute_type {\n            return Err(format!(\n                \"DynamoDB key attribute {} must use type {} (received {actual_type})\",\n                key.name, key.attribute_type\n            ));\n        }\n    }\n    Ok(())\n}\n\nfn item_key(\n    table: &DynamoDbTableDescription,","sourceCodeStart":1180,"sourceCodeEnd":1216,"githubUrl":"https://github.com/t8y2/dbx/blob/c0390bff16418b651f4728520d99adf8ce48829a/crates/dbx-core/src/db/dynamodb_driver.rs#L1180-L1216","documentation":"Panic from `Option::expect` in validate_item_keys after `item.contains_key(&key.name)` returned true; the subsequent item.get(&key.name) is therefore guaranteed Some. The expect documents that get must succeed right after contains_key on the same map. It can only panic if the map is mutated between the two calls or the code is restructured without preserving the guard, or if the borrowed key/name disagree (same &str source, so not realistic).","triggerScenarios":"Effectively unreachable as written; would fire only under concurrent mutation of the item map between contains_key and get, or if the contains_key guard is removed in a refactor.","commonSituations":"Code review refactors splitting the check and the get; concurrent writers to the item HashMap; not triggerable by normal API input.","solutions":["Keep contains_key immediately before get on the same unshared map","Collapse to entry()/get + match to eliminate the expect: match item.get(&key.name) { Some(v) => ..., None => return Err(...) }","Use item.get(&key.name).ok_or_else(|| ...)? to merge the check and retrieval","Document the adjacency invariant with a comment or test"],"exampleFix":"// before\nif !item.contains_key(&key.name) { return Err(...); }\nlet value = item.get(&key.name).expect(\"checked above\");\n// after\nlet Some(value) = item.get(&key.name) else {\n    return Err(format!(\"DynamoDB item requires key attribute: {}\", key.name));\n};","handlingStrategy":"validation","validationCode":"for key_name in required_key_names(table) {\n    if !item.contains_key(key_name) {\n        return Err(format!(\"item missing key attribute: {key_name}\"));\n    }\n}","typeGuard":"fn has_all_key_attributes(desc: &DynamoDbTableDescription, item: &HashMap<String, AttributeValue>) -> bool {\n    [Some(&desc.partition_key), desc.sort_key.as_ref()].into_iter().flatten()\n        .all(|k| item.contains_key(&k.name))\n}","tryCatchPattern":"match put_item_with_identity(client, table, &item).await {\n    Ok(_) => (),\n    Err(e) => eprintln!(\"item rejected: {e}\"),\n}","preventionTips":["Fetch the table description first and include every key attribute in items","Match key attribute types (S/N/B) to the table's declared key types","Validate items against the table schema before writes"],"tags":["rust","dynamodb","panic","invariant","validation"],"backgroundTag":"internal-invariant-panic","analyzedSha":"c0390bff16418b651f4728520d99adf8ce48829a","analyzedAt":"2026-09-05T23:05:10.900Z","contentChangedAt":"2026-09-05T23:05:10.900Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}