{"record":{"id":"c0f880e9e8c474b9","repo":"embassy-rs/embassy","slug":"unwrap-of-failed-c0f880","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":215,"sourceCode":"    };\n}\n\n#[cfg(feature = \"defmt\")]\n#[collapse_debuginfo(yes)]\nmacro_rules! unwrap {\n    ($($x:tt)*) => {\n        ::defmt::unwrap!($($x)*)\n    };\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;","sourceCodeStart":197,"sourceCodeEnd":233,"githubUrl":"https://github.com/embassy-rs/embassy/blob/463a07b963419a1bfe61d5d597c44acb810afb8b/embassy-nxp/src/fmt.rs#L197-L233","documentation":"embassy-nxp's internal `unwrap!` macro wraps an expression whose type implements `fmt::Try` (Result/Option) and, on Err/None, panics with the stringified expression and the Debug value of the error. It exists so embassy code and user code can unwrap with the embassy logger formatting (using `core::panic!` so it works without std). The message interpolates both the source expression text and the error payload.","triggerScenarios":"Any call site (in embassy-nxp or user code using `embassy_nxp::fmt::unwrap!`) where the wrapped expression evaluates to `Err(e)` (or `None`); the panic message shows the expression string and `{:?}` of the error.","commonSituations":"Unwrapping peripheral init or operation Results that can legitimately fail (I2C NACK, timeout, invalid configuration); relying on unwrap during bring-up and then hitting a real runtime error instead of handling it.","solutions":["Read the `{:?}` error payload in the message to identify the underlying error kind, then fix its cause.","Replace `unwrap!` with explicit `match`/`?` handling in application code once past bring-up.","Use the two-argument form `unwrap!(expr, \"context\")` to add context to the panic while debugging.","For embedded error types with no Debug (defmt/std mismatch), ensure the error type implements the right Debug/Format trait for your logging feature set."],"exampleFix":"// before\nlet byte = unwrap!(i2c.blocking_read(addr, &mut buf));\n// after\nmatch i2c.blocking_read(addr, &mut buf) {\n    Ok(()) => {}\n    Err(e) => { defmt::error!(\"i2c read failed: {:?}\", e); return Err(e); }\n}","handlingStrategy":"try-catch","validationCode":"// Replace blind unwrap with explicit handling at the call site:\nif let Err(e) = i2c.blocking_write_read(addr, &mut cmd, &mut buf) {\n    defmt::warn!(\"i2c write_read failed: {:?}\", e);\n    return Err(e.into());\n}","typeGuard":"fn is_ok<T, E>(r: &Result<T, E>) -> bool { r.is_ok() }","tryCatchPattern":"// Embassy `unwrap!` panics and cannot be caught in embedded; handle Results before they reach unwrap!:\nmatch op {\n    Ok(v) => use(v),\n    Err(e) => { defmt::error!(\"op failed: {:?}\", e); recover_or_reset(); }\n}","preventionTips":["Use unwrap! only during bring-up; migrate to `?`/match in production code paths.","Always read the `{:?}` payload in the panic message to find the real error kind.","Add the contextual form unwrap!(expr, \"stage\") so panics identify the failing init step.","Wrap fallible peripheral operations in your own error type with recovery paths (retry, re-init)."],"tags":["embedded","unwrap","macro","nxp","panic"],"backgroundTag":"unwrap-failed","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"}