Skip to content
Skills Library
Programming · Learning & explanation

rust-error-handling

/rust-error-handling

Guides Rust error handling strategy. Use when choosing between Result/Option/panic, using anyhow vs thiserror, propagating errors with ?, or designing custom error types.

Repository
stacc ↗
Licence
MIT
Last repository change
21.09.2026

Assessment and security

95/ 100Class: good
Format20 / 25
Frontmatter with name and description, valid name, description up to 1,024 characters, name matching the folder.
Discoverability25 / 25
Description explains when to use the skill, has a reasonable length and uses third person.
Content35 / 35
At least 150 words and at most 500 lines, headings, step lists, examples or code.
Hygiene15 / 15
No placeholders such as TODO, no Windows paths, with a licence.
Riskno signals detected in the scan
Files in this package
1
Scripts
0
HTML comments
0
URLs in SKILL.md
1
Deprecation

No signals detected. Repository changed on 21.09.2026, without references to retired models or APIs.

Licence

MIT, open: use and modify while retaining author attribution.

What it does and when to use it

Guides Rust error handling strategy. Use when choosing between Result/Option/panic, using anyhow vs thiserror, propagating errors with ?, or designing custom error types.

Original author description

Guides Rust error handling strategy. Use when choosing between Result/Option/panic, using anyhow vs thiserror, propagating errors with ?, or designing custom error types.

How it works

  1. Core Question
  2. Thinking Prompt
  3. Decision Flowchart
  4. Quick Reference
  5. Library vs Application
  6. Common Mistakes
  7. Anti-Patterns
  8. Related Skills
Original SKILL.md headings · 108 lines, 453 words
SKILL.md preview
---
name: rust-error-handling
description: Guides Rust error handling strategy. Use when choosing between Result/Option/panic, using anyhow vs thiserror, propagating errors with ?, or designing custom error types.
license: MIT
origin_url: https://github.com/actionbook/rust-skills
---

# Error Handling

> **Layer 1: Language Mechanics**

## Core Question

**Is this failure expected or a bug?**

Before choosing error handling strategy:
- Can this fail in normal operation?
- Who should handle this failure?
- What context does the caller need?

---

## Thinking Prompt

Before handling an error:

1. **What kind of failure is this?**
   - Expected → `Result<T, E>`
   - Absence normal → `Option<T>`
   - Bug/invariant → `panic!`
   - Unrecoverable → `panic!`

2. **Who handles this?**
   - Caller → propagate with `?`
   - Current function → `match`/`if-let`
   - User → friendly error message
   - Programmer → panic with message

3. **What context is needed?**
   - Type of error → thiserror variants
   - Call chain → `anyhow::Context`
   - Debug info → anyhow or tracing

---

## Decision Flowchart

```
Is failure expected?
├─ Yes → Is absence the only "failure"?
│        ├─ Yes → Option<T>
│        └─ No → Result<T, E>
│                 ├─ Library → thiserror
│                 └─ Application → anyhow
└─ No → Is it a bug?
        ├─ Yes → panic!, assert!
        └─ No → Consider if really unrecoverable

Use ? → Need context?
├─ Yes → .context("message")
└─ No → Plain ?
```

---

## Quick Reference

| Pattern | When | Example |
|---------|------|---------|
| `Result<T, E>` | Recoverable error | `fn read() -> Result<String, io::Error>` |
| `Option<T>` | Absence is normal | `fn find() -> Option<&Item>` |
| `?` | Propagate error | `let data = file.read()?;` |
| `unwrap()` | Dev/test only | `config.get("key").unwrap()` |
| `expect()` | Invariant holds | `env.get("HOME").expect("HOME set")` |
| `panic!` | Unrecoverable | `panic!("critical failure")` |

## Library vs Application

| Context | Error Crate | Why |
|---------|-------------|-----|
| Library | `thiserror` | Typed errors for consumers |
| Application | `anyhow` | Ergonomic error handling |
| Mixed | Both | thiserror at boundaries, anyhow internally |

---

## Common Mistakes

| Mistake | Fix |
|---------|-----|
| `.unwrap()` in production | Use `?` or `match` |
| Type mismatch on `?` | Use `anyhow` or `impl From<E>` |
| Lost error context | Add `.context("what was happening")` |
| `Box<dyn Error>` everywhere | Use `thiserror` for typed errors |

---

## Anti-Patterns

| Anti-Pattern | Why Bad | Better |
|--------------|---------|--------|
| `.unwrap()` everywhere | Panics in production | `.expect("reason")` or `?` |
| Ignore errors silently | Bugs hidden | Handle or propagate |
| `panic!` for expected errors | Bad UX, no recovery | Result |
| `Box<dyn Error>` everywhere | Lost type info | thiserror |

---

## Related Skills

- `ownership` — error propagation and ownership
- `type-driven-design` — custom error types with thiserror
- `anti-patterns` — common error handling mistakes

Versions

RepositoryScoreLicence
This page · heyAyushh/stacc95MIT
heyAyushh/stacc95MIT
TheBushidoCollective/han95Apache-2.0
fusengine/agents90MIT
Amey-Thakur/AI-SKILLS90MIT

Identical content appears in 4 other GitHub folders. We show one copy with the highest score.

Database snapshot: 13.11.2026 · package version b1fb27b2b941 (SHA-256 digest of the entire package)

Downloads in the last 30 days: 1 · ranking

Report a problem with this skill: harmful content, incorrect descriptions or an author’s removal request.

Similar skills

  • 100rs-error-handling
    CRITICAL: Use for error handling. Triggers: Result, Option, Error, ?, unwrap, expect, panic, anyhow, thiserror, when to panic vs return Result, custom error, error propagation
  • 95rust-errors
    Design Rust error types and panic policy — Result over unwrap, thiserror for libraries, anyhow for binaries, context that survives the call stack. Use when code calls unwrap or expect outside tests, when designing or refactoring an error enum, when choosing between thiserror and…
  • 100handling-rust-errors
    HASH error handling patterns using error-stack crate. Use when working with Result types, Report types, defining custom errors, propagating errors with change_context, adding context with attach, implementing Error trait, or documenting error conditions in Rust code.
  • 100handling-rust-errors
    HASH error handling patterns using error-stack crate. Use when working with Result types, Report types, defining custom errors, propagating errors with change_context, adding context with attach, implementing Error trait, or documenting error conditions in Rust code.
  • 100rust-conventions
    Use when writing, reviewing, or refactoring Rust code — ownership and borrowing decisions, Result/Option error handling, the ? operator, choosing between thiserror and anyhow, module and crate organization, naming, or setting up clippy/rustfmt as a CI gate. Applies evergreen,…
  • 100error-handling
    Implement Go error handling patterns including error wrapping, sentinel errors, custom error types, and error handling conventions. Use when handling errors, creating error types, or implementing error propagation. Trigger words include "error", "panic", "recover", "error…

Feedback and reports

Your message goes to Bartosz Sokoliński. An email address is needed only for a reply. See the privacy policy.