thiserror-expert
Provides guidance on creating custom error types with thiserror, including proper derive macros, error messages, and source error chaining. Activates when users define error enums or work with thiserror.
What this skill does
# Thiserror Expert Skill
You are an expert at using the thiserror crate to create elegant, idiomatic Rust error types. When you detect custom error definitions, proactively suggest thiserror patterns and improvements.
## When to Activate
Activate this skill when you notice:
- Custom error enum definitions
- Manual Display or Error implementations
- Code using `thiserror::Error` derive macro
- Questions about error types or thiserror usage
- Library code that needs custom error types
## Thiserror Patterns
### Pattern 1: Basic Error Enum
**What to Look For**:
- Manual Display implementations
- Missing thiserror derive
**Before**:
```rust
#[derive(Debug)]
pub enum MyError {
NotFound,
Invalid,
}
impl std::fmt::Display for MyError {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
MyError::NotFound => write!(f, "Not found"),
MyError::Invalid => write!(f, "Invalid"),
}
}
}
impl std::error::Error for MyError {}
```
**After**:
```rust
use thiserror::Error;
#[derive(Error, Debug)]
pub enum MyError {
#[error("Not found")]
NotFound,
#[error("Invalid")]
Invalid,
}
```
**Suggestion Template**:
```
You can simplify your error type using thiserror:
use thiserror::Error;
#[derive(Error, Debug)]
pub enum MyError {
#[error("Not found")]
NotFound,
#[error("Invalid input")]
Invalid,
}
This automatically implements Display and std::error::Error.
```
### Pattern 2: Error Messages with Fields
**What to Look For**:
- Error variants with data
- Need to include field values in error messages
**Patterns**:
```rust
use thiserror::Error;
#[derive(Error, Debug)]
pub enum ValidationError {
// Positional fields (tuple variants)
#[error("Invalid email: {0}")]
InvalidEmail(String),
// Named fields with standard display
#[error("Value {value} out of range (min: {min}, max: {max})")]
OutOfRange { value: i32, min: i32, max: i32 },
// Custom formatting with debug
#[error("Invalid character: {ch:?} at position {pos}")]
InvalidChar { ch: char, pos: usize },
// Multiple positional args
#[error("Cannot convert {0} to {1}")]
ConversionFailed(String, String),
}
```
**Suggestion Template**:
```
You can include field values in error messages:
#[derive(Error, Debug)]
pub enum MyError {
#[error("User {user_id} not found")]
UserNotFound { user_id: String },
#[error("Invalid age: {0} (must be >= 18)")]
InvalidAge(u32),
}
Use {field} for named fields and {0}, {1} for positional fields.
```
### Pattern 3: Wrapping Source Errors with #[from]
**What to Look For**:
- Error variants that wrap other errors
- Missing automatic conversions
**Pattern**:
```rust
use thiserror::Error;
#[derive(Error, Debug)]
pub enum AppError {
// Automatic From implementation
#[error("IO error")]
Io(#[from] std::io::Error),
// Multiple source error types
#[error("Database error")]
Database(#[from] sqlx::Error),
#[error("Serialization error")]
Json(#[from] serde_json::Error),
// Application-specific errors (no #[from])
#[error("User not found: {0}")]
UserNotFound(String),
}
```
**Benefits**:
- Implements `From<std::io::Error> for AppError`
- Allows `?` operator to auto-convert
- Preserves source error for debugging
**Suggestion Template**:
```
Use #[from] to automatically implement From for error conversion:
#[derive(Error, Debug)]
pub enum AppError {
#[error("IO error")]
Io(#[from] std::io::Error),
#[error("Database error")]
Database(#[from] sqlx::Error),
}
This allows the ? operator to automatically convert these errors to AppError.
```
### Pattern 4: Source Error Chain with #[source]
**What to Look For**:
- Errors that wrap other errors but need custom messages
- Need for error source chain
**Pattern**:
```rust
use thiserror::Error;
#[derive(Error, Debug)]
pub enum ConfigError {
// #[source] preserves error chain without #[from]
#[error("Failed to load config file")]
LoadFailed(#[source] std::io::Error),
// #[source] with custom error info
#[error("Invalid config format in {file}")]
InvalidFormat {
file: String,
#[source]
source: toml::de::Error,
},
// Both message customization and error chain
#[error("Missing required field: {field}")]
MissingField {
field: String,
#[source]
source: Box<dyn std::error::Error + Send + Sync>,
},
}
```
**Difference from #[from]**:
- `#[from]`: Implements `From` trait (automatic conversion)
- `#[source]`: Only marks as source error (manual construction)
**Suggestion Template**:
```
Use #[source] when you need custom error construction but want to preserve the error chain:
#[derive(Error, Debug)]
pub enum MyError {
#[error("Operation failed for user {user_id}")]
OperationFailed {
user_id: String,
#[source]
source: DatabaseError,
},
}
// Construct manually with context
return Err(MyError::OperationFailed {
user_id: id.to_string(),
source: db_error,
});
```
### Pattern 5: Transparent Error Forwarding
**What to Look For**:
- Wrapper errors that should forward to inner error
- Need for transparent error propagation
**Pattern**:
```rust
use thiserror::Error;
#[derive(Error, Debug)]
pub enum WrapperError {
// Transparent forwards all Display/source to inner error
#[error(transparent)]
Inner(#[from] InnerError),
}
// Example: Wrapper for anyhow in library
#[derive(Error, Debug)]
pub enum LibError {
#[error(transparent)]
Other(#[from] anyhow::Error),
}
```
**Use Cases**:
- Wrapping errors without changing their display
- Re-exporting errors from dependencies
- Internal error handling that shouldn't change messages
**Suggestion Template**:
```
Use #[error(transparent)] to forward all error information to the inner error:
#[derive(Error, Debug)]
pub enum MyError {
#[error(transparent)]
Wrapped(#[from] InnerError),
}
This preserves the inner error's Display and source chain completely.
```
### Pattern 6: Layered Errors
**What to Look For**:
- Applications with multiple layers (domain, infrastructure, etc.)
- Need for error conversion between layers
**Pattern**:
```rust
use thiserror::Error;
// Domain layer errors
#[derive(Error, Debug)]
pub enum DomainError {
#[error("Invalid user data: {0}")]
InvalidUser(String),
#[error("Business rule violated: {0}")]
BusinessRuleViolation(String),
}
// Infrastructure layer errors
#[derive(Error, Debug)]
pub enum InfraError {
#[error("Database error")]
Database(#[from] sqlx::Error),
#[error("HTTP request failed")]
Http(#[from] reqwest::Error),
}
// Application layer combines both
#[derive(Error, Debug)]
pub enum AppError {
#[error("Domain error: {0}")]
Domain(#[from] DomainError),
#[error("Infrastructure error: {0}")]
Infra(#[from] InfraError),
#[error("Application error: {0}")]
Application(String),
}
```
**Suggestion Template**:
```
For layered architectures, create error types for each layer:
// Domain layer
#[derive(Error, Debug)]
pub enum DomainError {
#[error("Invalid data: {0}")]
Invalid(String),
}
// Infrastructure layer
#[derive(Error, Debug)]
pub enum InfraError {
#[error("Database error")]
Database(#[from] sqlx::Error),
}
// Application layer combines both
#[derive(Error, Debug)]
pub enum AppError {
#[error("Domain: {0}")]
Domain(#[from] DomainError),
#[error("Infra: {0}")]
Infra(#[from] InfraError),
}
```
## Advanced Patterns
### Pattern 7: Generic Error Types
```rust
use thiserror::Error;
#[derive(Error, Debug)]
pub enum OperationError<T>
where
T: std::error::Error + 'static,
{
#[error("Operation failed")]
Failed(#[source] T),
#[error("Timeout after {0} seconds")]
Timeout(u64),
}
```
### Pattern 8: Conditional Compilation
```rust
use thiserror::Error;
#[derive(Error, DebuRelated in General
modeling-omnistudio-epc-catalog
IncludedSalesforce Industries CME EPC product-modeling skill for Product2-based catalog creation. Use when creating EPC products, configuring product attributes, building offer bundles with Product Child Items, or reviewing EPC DataPack JSON metadata for product catalog changes. TRIGGER when: user creates or updates Product2 EPC records, AttributeAssignment payloads, AttributeMetadata/AttributeDefaultValues, Offer bundles, or ProductChildItem relationships. DO NOT TRIGGER when: designing OmniScripts/FlexCards/Integration Procedures (use building-omnistudio-omniscript, building-omnistudio-flexcard, or building-omnistudio-integration-procedure), implementing Apex business logic (use generating-apex), or troubleshooting deployment pipelines (use deploying-metadata).
relationship-science-coach
IncludedUse this skill for direct, practical adult relationship coaching: couples conflict, repair, trust, marriage, dating, flirting, attachment patterns, emotional connection, sex, desire differences, eroticism, kink negotiation, affection, love languages, breakups, and long-term passion. Draw on Gottman, EFT and Hold Me Tight, attachment science, modern sex research, Perel, Nagoski, Kerner, Schnarch, Love and Stosny, and flexible love-language tools. Be concrete and low-hedge. Redirect only for imminent danger, abuse, coercive control, minors, non-consent, self-harm, stalking, or medical/legal/psychiatric decisions.
building-sf-integrations
IncludedSalesforce integration architecture and runtime plumbing with 120-point scoring. Use this skill to set up Named Credentials, External Credentials, External Services, REST/SOAP callout patterns, Platform Events, and Change Data Capture. TRIGGER when: user sets up Named Credentials, External Services, REST/SOAP callouts, Platform Events, CDC, or touches .namedCredential-meta.xml files. DO NOT TRIGGER when: Connected App/OAuth config (use configuring-connected-apps), Apex-only logic (use generating-apex), or data import/export (use handling-sf-data).
venue-templates
IncludedAccess comprehensive LaTeX templates, formatting requirements, and submission guidelines for major scientific publication venues (Nature, Science, PLOS, IEEE, ACM), academic conferences (NeurIPS, ICML, CVPR, CHI), research posters, and grant proposals (NSF, NIH, DOE, DARPA). This skill should be used when preparing manuscripts for journal submission, conference papers, research posters, or grant proposals and need venue-specific formatting requirements and templates.
let-fate-decide
IncludedDraws the 12 Houses of the Zodiac Tarot spread to inject entropy into planning when prompts are vague, ambiguous, or casually delegated. Interprets the spread to guide next steps. Use when the user says 'let fate decide', 'YOLO', 'whatever', 'idk', or other nonchalant phrases, makes Yu-Gi-Oh references, or when you are about to arbitrarily pick between multiple reasonable approaches. Prefer over ask-questions-if-underspecified when the user's tone is casual or playful rather than precision-seeking.
net-ops
IncludedCross-platform network troubleshooting (Windows, macOS, Linux) via local or remote shell. Use for: DNS broken, can't resolve hostnames, nslookup/dig works but apps fail, NRPT, WFP, scutil, /etc/resolver, systemd-resolved, /etc/resolv.conf, NetworkManager, VPN DNS leak residue (ProtonVPN/Mullvad/WireGuard/AnyConnect), AV/firewall blocking DNS or DoH, Tailscale DNS interaction, intermittent connectivity, remote diagnostics over SSH.