Back to all reviewers

Use error chain iterators

PostHog/posthog
Based on 2 comments
Rust

When traversing error chains in Rust, prefer using the `chain()` iterator method over manual source traversal with while loops. The `chain()` method provides a more idiomatic and readable approach to walking through error chains, eliminating the need for manual loop management and making the code more concise.

Error Handling Rust

Reviewer Prompt

When traversing error chains in Rust, prefer using the chain() iterator method over manual source traversal with while loops. The chain() method provides a more idiomatic and readable approach to walking through error chains, eliminating the need for manual loop management and making the code more concise.

Instead of manually iterating through error sources:

let mut source = error.source();
while let Some(err) = source {
    if let Some(rl) = err.downcast_ref::<RateLimitedError>() {
        return rl.retry_after;
    }
    source = err.source();
}

Use the iterator-based approach:

error
    .chain()
    .find_map(|e| e.downcast_ref::<RateLimitedError>())
    .and_then(|rl| rl.retry_after)

This pattern works well with other iterator methods like any(), find(), and filter_map() to create more expressive and maintainable error handling code.

2
Comments Analyzed
Rust
Primary Language
Error Handling
Category

Source Discussions