Errors
All fallible operations return nxt_rs::Result<T>, an alias for
Result<T, nxt_rs::Error>.
| Variant | Meaning |
|---|---|
Error::Transport(e) | The USB/Bluetooth/serial link failed: unplugged, permission denied, … |
Error::Timeout | The 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 level | High level |
|---|---|
stop_program() fails with NoActiveProgram | brick.stop_program() returns Ok(false) |
get_current_program_name() fails with NoActiveProgram | brick.current_program() returns Ok(None) |
message_read() fails with MailboxEmpty | messages().recv() returns Ok(None) |
find_first() fails with FileNotFound | files().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>.