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

Error Handling

Robust error handling is essential in any concurrent system. rsActor provides comprehensive mechanisms to deal with errors at every level.

Error Categories

Errors in rsActor fall into these categories:

  1. Actor Lifecycle Errors: Errors in on_start, on_run, or on_stop (your custom Actor::Error type)
  2. Communication Errors: Errors during message sending (rsactor::Error)
  3. Message Handling Errors: Errors within handler methods (your custom reply types)
  4. Panics: If an actor task panics (caught by JoinHandle)

rsactor::Error

The framework’s built-in error type for communication failures:

VariantDescriptionRetryable?
Error::SendActor’s mailbox is closed (actor stopped)No
Error::ReceiveReply channel was dropped before responseNo
Error::TimeoutOperation exceeded the specified timeoutYes
Error::DowncastReply type downcast failed (programming error)No
Error::RuntimeActor lifecycle runtime failureNo
Error::MailboxCapacityMailbox capacity configuration errorNo
Error::JoinSpawned task panicked or was cancelledNo

Error Utilities

#![allow(unused)]
fn main() {
match actor_ref.tell(msg).await {
    Ok(()) => { /* delivered */ }
    Err(e) => {
        // Check if retrying might help
        if e.is_retryable() {
            // Only Timeout errors are retryable
        }

        // Get actionable debugging suggestions
        for tip in e.debugging_tips() {
            println!("  - {}", tip);
        }
    }
}
}

Custom Actor Errors

Define your own error type for lifecycle methods. Any type implementing Send + Debug + 'static works:

#![allow(unused)]
fn main() {
#[derive(Debug, thiserror::Error)]
enum MyActorError {
    #[error("Database error: {0}")]
    DbError(#[from] sqlx::Error),

    #[error("Network timeout")]
    NetworkTimeout,
}

impl Actor for MyActor {
    type Error = MyActorError;
    // ...
}
}

on_tell_result Hook

For fire-and-forget (tell) messages, errors in the handler are silently dropped by default. The on_tell_result hook provides observability:

#![allow(unused)]
fn main() {
#[message_handlers]
impl MyActor {
    #[handler]
    async fn handle_command(&mut self, msg: Command, _: &ActorRef<Self>) {
        self.execute(msg)?;
    }
}

// In the Message<Command> trait implementation (auto-generated by #[handler]):
// on_tell_result is called with the handler's return value when sent via tell()
}

When implementing Message<T> manually, override on_tell_result:

#![allow(unused)]
fn main() {
impl Message<Command> for MyActor {
    type Reply = Result<(), CommandError>;

    async fn handle(&mut self, msg: Command, _: &ActorRef<Self>) -> Self::Reply {
        self.execute(msg)
    }

    fn on_tell_result(result: &Self::Reply, _actor_ref: &ActorRef<Self>) {
        if let Err(e) = result {
            tracing::error!("Command failed (fire-and-forget): {e:?}");
        }
    }
}
}

Use #[handler(no_log)] to suppress the default warning log for tell errors.

Dead Letters

When a message cannot be delivered, rsActor automatically records a dead letter with structured tracing. See Dead Letter Tracking for details.