Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Errors

All fallible operations return nxt_rs::Result<T>, an alias for Result<T, nxt_rs::Error>.

VariantMeaning
Error::Transport(e)The USB/Bluetooth/serial link failed: unplugged, permission denied, …
Error::TimeoutThe brick didn’t answer in time (Brick::with_timeout)
Error::Brick { kind, opcode, status }The brick rejected a command, with a typed Status
Error::Decode(e) / Error::Encode(e)A telegram was malformed (rare; usually a bug or a noisy link)
Error::Value(e)A value was out of range, e.g. Power::new(150)
Error::PortTaken(port)The port is already claimed by another device

Handling brick errors

error.status() gives the brick’s status code, if the brick sent one:

nxt_rs::doc_example(|brick| async move {
use nxt_rs::{Status, protocol::FileName};

match brick.files().read(&FileName::new("missing.txt")?).await {
    Ok(data) => println!("{} bytes", data.len()),
    Err(e) if e.status() == Some(Status::FileNotFound) => println!("no such file"),
    Err(e) => return Err(e.into()),
}
Ok(()) });

Many “errors” are normal answers, and the high-level API turns those into ordinary values:

Low levelHigh level
stop_program() fails with NoActiveProgrambrick.stop_program() returns Ok(false)
get_current_program_name() fails with NoActiveProgrambrick.current_program() returns Ok(None)
message_read() fails with MailboxEmptymessages().recv() returns Ok(None)
find_first() fails with FileNotFoundfiles().list() returns an empty Vec

Status::is_retryable() tells you whether trying again later may help, for example while an I²C transfer is still running.

With anyhow and friends

nxt_rs::Error implements std::error::Error + Send + Sync + 'static, so it works with anyhow, eyre and Box<dyn Error>.