Docs›qos-sdk›Error Handling
qos-sdk Reference

Error Handling

In a #[no_std] WebAssembly environment, error handling must be explicit and deterministic. Q-OS modules must declare a custom panic handler.

#The Panic Handler

If a module panics, the Wasmtime host will trap the execution and immediately terminate the sandbox. To ensure you see these panics in the Admin HUD logs, you must wire up the panic handler to qos_sdk::log_msg.

src/lib.rs
use qos_sdk::{log_msg, LogLevel};

#[cfg(not(test))]
#[panic_handler]
fn panic(_info: &core::panic::PanicInfo) -> ! {
    log_msg(LogLevel::Error, "WASM module panicked!");
    
    // Trap the WebAssembly execution
    #[cfg(target_arch = "wasm32")]
    core::arch::wasm32::unreachable();
    
    // Fallback for native test builds
    #[cfg(not(target_arch = "wasm32"))]
    loop {}
}

#Logging to the Host

The log_msg function allows you to send formatted strings to the host daemon's stdout (and potentially to the WebSocket stream, depending on log level configurations).

pub fn log_msg(level: LogLevel, msg: &str)

ℹ️ NOTE

Always favor returning an error receipt via qos_sdk::output::write rather than panicking. Panicking is unrecoverable for the guest's UI, while an error receipt allows the Micro-UI to display a graceful error message (e.g., "Invalid QR code").