skills/emillindfors/thiserror-expert/SKILL.md
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.
npx skillsauth add aiskillstore/marketplace thiserror-expertInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
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.
Activate this skill when you notice:
thiserror::Error derive macroWhat to Look For:
Before:
#[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:
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.
What to Look For:
Patterns:
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.
What to Look For:
Pattern:
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:
From<std::io::Error> for AppError? operator to auto-convertSuggestion 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.
What to Look For:
Pattern:
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,
});
What to Look For:
Pattern:
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:
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.
What to Look For:
Pattern:
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),
}
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),
}
use thiserror::Error;
#[derive(Error, Debug)]
pub enum MyError {
#[error("IO error")]
Io(#[from] std::io::Error),
#[cfg(feature = "postgres")]
#[error("Database error")]
Database(#[from] sqlx::Error),
#[cfg(feature = "redis")]
#[error("Cache error")]
Cache(#[from] redis::RedisError),
}
use thiserror::Error;
#[derive(Error, Debug)]
pub enum ValidationError {
// Unit variant
#[error("Value is required")]
Required,
// Tuple variant
#[error("Invalid format: {0}")]
InvalidFormat(String),
// Struct variant
#[error("Out of range (expected {expected}, got {actual})")]
OutOfRange { expected: String, actual: String },
// Nested error
#[error("Validation failed")]
Nested(#[from] SubValidationError),
}
#[derive(Error, Debug)]
pub enum ConfigError {
// ✅ Clear and actionable
#[error("Config file not found at '{path}'. Create one using: config init")]
NotFound { path: String },
// ✅ Explains what's wrong and expected format
#[error("Invalid port number '{port}'. Expected a number between 1 and 65535")]
InvalidPort { port: String },
}
#[derive(Error, Debug)]
pub enum BadError {
// ❌ Too vague
#[error("Error")]
Error,
// ❌ Not helpful
#[error("Something went wrong")]
Failed,
}
#[derive(Error, Debug)]
pub enum AppError {
// ✅ Includes what, where, and source
#[error("Failed to read file '{path}'")]
ReadFailed {
path: String,
#[source]
source: std::io::Error,
},
}
pub type Result<T> = std::result::Result<T, MyError>;
// Now you can use:
pub fn operation() -> Result<Value> {
Ok(value)
}
// ❌ BAD: Source error not marked
#[derive(Error, Debug)]
pub enum MyError {
#[error("Failed")]
Failed(std::io::Error), // Missing #[source]
}
// ✅ GOOD: Properly marked
#[derive(Error, Debug)]
pub enum MyError {
#[error("Failed")]
Failed(#[source] std::io::Error),
}
// ❌ Can't add context with #[from]
#[derive(Error, Debug)]
pub enum MyError {
#[error("Failed")]
Failed(#[from] std::io::Error),
}
// ✅ Use #[source] for custom construction
#[derive(Error, Debug)]
pub enum MyError {
#[error("Failed to read config file '{path}'")]
ConfigReadFailed {
path: String,
#[source]
source: std::io::Error,
},
}
When you see custom error types, immediately suggest thiserror patterns that will make them more ergonomic and idiomatic.
development
Apple Human Interface Guidelines for content display components. Use this skill when the user asks about charts component, collection view, image view, web view, color well, image well, activity view, lockup, data visualization, content display, displaying images, rendering web content, color pickers, or presenting collections of items in Apple apps. Also use when the user says how should I display charts, what's the best way to show images, should I use a web view, how do I build a grid of items, what component shows media, or how do I present a share sheet. Cross-references: hig-foundations for color/typography/accessibility, hig-patterns for data visualization patterns, hig-components-layout for structural containers, hig-platforms for platform-specific component behavior.
tools
Automate HelpDesk tasks via Rube MCP (Composio): list tickets, manage views, use canned responses, and configure custom fields. Always search tools first for current schemas.
testing
Expert Haskell engineer specializing in advanced type systems, pure functional design, and high-reliability software. Use PROACTIVELY for type-level programming, concurrency, and architecture guidance.
tools
GraphQL gives clients exactly the data they need - no more, no less. One endpoint, typed schema, introspection. But the flexibility that makes it powerful also makes it dangerous. Without proper controls, clients can craft queries that bring down your server. This skill covers schema design, resolvers, DataLoader for N+1 prevention, federation for microservices, and client integration with Apollo/urql. Key insight: GraphQL is a contract. The schema is the API documentation. Design it carefully.