{"record":{"id":"577da8b76fa8c58e","repo":"embassy-rs/embassy","slug":"unwrap-of-failed-577da8","errorCode":null,"errorMessage":"unwrap of `{}` failed: {}: {:?}","messagePattern":"unwrap of `(.+?)` failed: (.+?): (.+?)","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"embassy-nxp/src/fmt.rs","lineNumber":223,"sourceCode":"    };\n}\n\n#[cfg(not(feature = \"defmt\"))]\n#[collapse_debuginfo(yes)]\nmacro_rules! unwrap {\n    ($arg:expr) => {\n        match $crate::fmt::Try::into_result($arg) {\n            ::core::result::Result::Ok(t) => t,\n            ::core::result::Result::Err(e) => {\n                ::core::panic!(\"unwrap of `{}` failed: {:?}\", ::core::stringify!($arg), e);\n            }\n        }\n    };\n    ($arg:expr, $($msg:expr),+ $(,)? ) => {\n        match $crate::fmt::Try::into_result($arg) {\n            ::core::result::Result::Ok(t) => t,\n            ::core::result::Result::Err(e) => {\n                ::core::panic!(\"unwrap of `{}` failed: {}: {:?}\", ::core::stringify!($arg), ::core::format_args!($($msg,)*), e);\n            }\n        }\n    }\n}\n\n#[derive(Debug, Copy, Clone, Eq, PartialEq)]\npub struct NoneError;\n\npub trait Try {\n    type Ok;\n    type Error;\n    fn into_result(self) -> Result<Self::Ok, Self::Error>;\n}\n\nimpl<T> Try for Option<T> {\n    type Ok = T;\n    type Error = NoneError;\n","sourceCodeStart":205,"sourceCodeEnd":241,"githubUrl":"https://github.com/embassy-rs/embassy/blob/463a07b963419a1bfe61d5d597c44acb810afb8b/embassy-nxp/src/fmt.rs#L205-L241","documentation":"This panic comes from the embassy-nxp `unwrap!` macro, which wraps a `Result`- or `Option`-returning expression and panics when it holds an error variant. The library throws it instead of `.unwrap()` so the panic message includes the caller-supplied context message plus the debug-formatted error value. Any call site that uses `unwrap!(expr, \"context\")` on a failing expression triggers it.","triggerScenarios":"Calling `unwrap!(some_result, \"message\")` in embassy-nxp code (or user code using the exported macro) where the expression resolves to `Err(e)` via `fmt::Try::into_result`, or `None` for Options.","commonSituations":"Peripheral drivers encountering unexpected hardware states (failed init, NACKed I2C transactions, timeout results) that were force-unwrapped; drivers written for a Result-returning API being ported to a version where the call now returns an error type.","solutions":["Read the `: {:?}` debug output at the end of the panic to identify the underlying error variant","Find the call site via the stringified expression in the panic message and handle the Result with `?` or a match instead of unwrapping","Fix the root cause the inner error reports (wrong peripheral config, busy hardware, missing init) before retrying","If the expression is an Option that should never be None, add an explicit assert with a domain-specific message"],"exampleFix":"// before\nlet status = unwrap!(i2c.blocking_write_read(addr, &reg, &mut buf), \"read failed\");\n// after\nlet status = match i2c.blocking_write_read(addr, &reg, &mut buf) {\n    Ok(s) => s,\n    Err(e) => { defmt::error!(\"read failed: {:?}\", e); return Err(e); }\n};","handlingStrategy":"validation","validationCode":"fn unwrap_ok<T, E: core::fmt::Debug>(r: Result<T, E>) -> T { match r { Ok(t) => t, Err(e) => { defmt::error!(\"unwrap failed: {:?}\", e); unreachable!() } } } // prefer handling Err explicitly before calling unwrap!\n","typeGuard":"fn is_ok<T, E>(r: &Result<T, E>) -> bool { r.is_ok() }\n","tryCatchPattern":"// Rust panics are not catchable per-call; use panic = \"abort\" awareness or catch_unwind on std targets\nlet v = std::panic::catch_unwind(|| unwrap!(fallible(), \"ctx\"));\n","preventionTips":["Never unwrap fallible HAL calls in production firmware; propagate with ?","Use the macro's context message parameter to aid debugging","Log the inner error's Debug output before any unwrap","Prefer explicit match/ok_or over the unwrap macro"],"tags":["rust","embedded","unwrap-panic","macro"],"backgroundTag":"internal-invariant-violation","analyzedSha":"463a07b963419a1bfe61d5d597c44acb810afb8b","analyzedAt":"2026-09-10T13:38:26.660Z","contentChangedAt":"2026-09-10T13:38:26.660Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}