handling-rust-errors

Apply error-stack patterns to standardize Rust error handling with contextual propagation.

1.6k|123|Updated Jul 15, 2019
One-click install
npx skills add https://github.com/hashintel/hash --skill handling-rust-errors
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: handling-rust-errors
Source: https://github.com/hashintel/hash/tree/main/.claude/skills/handling-rust-errors
Command: npx skills add https://github.com/hashintel/hash --skill handling-rust-errors

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

This Skill provides HASH-specific patterns for robust and consistent error handling in Rust using the error-stack crate. It eliminates common pitfalls, ensures clear, debuggable error reports, and reduces the time spent on identifying the root cause of issues, allowing you to build more reliable software.

Core Features & Use Cases

  • Structured Error Types: Guides you to define custom error enums with derive_more for clear, user-friendly messages and structured data, improving error clarity.
  • Contextual Propagation: Learn to propagate errors with .change_context() for seamless type conversion and .attach() for adding rich debugging information, creating a comprehensive error trail.
  • Lazy Context Attachment: Utilize _with variants (attach_with, change_context_with) to defer expensive context computations until an error actually occurs, optimizing performance.
  • Error Documentation: Provides guidance on documenting error conditions in function signatures using # Errors sections and intra-doc links, enhancing API clarity.
  • Use Case: When a function might fail (e.g., a database operation), this skill guides you to wrap the external error with Report::new, convert it to a specific MyError variant using change_context, and attach relevant debugging details like an ID using attach, ensuring a clear error trail.

Quick Start

Use the handling-rust-errors skill to define a new error enum 'NetworkError' with variants for 'ConnectionFailed' and 'Timeout'.

Frequently Asked Questions about handling-rust-errors

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I handle errors in Rust without using Box<dyn Error> or anyhow?

Error handling in Rust can use the error-stack crate with custom error enums and Report<MyError> for structured, debuggable error propagation. Define error types with derive_more for Display, then propagate with .change_context() and .attach() to build comprehensive error trails without boxing or anyhow.

What's the best way to add context and debugging information to Rust errors?

Attach rich debugging context to Rust errors using .attach() for immediate details and .attach_with() to defer expensive computations until errors occur. This creates a clear error trail while optimizing performance in the success path.

How do I define custom error types in Rust with clear error messages?

Define custom error enums using derive_more for Display implementations, then implement core::error::Error. This approach provides structured data and user-friendly messages while maintaining type safety across error propagation.

Can I document error conditions in Rust function signatures?

Yes, document error conditions in function signatures using # Errors sections with intra-doc links to your custom error types. This enhances API clarity and helps users understand what errors a function might return.

Why should I use error-stack patterns instead of generic error handling?

Error-stack patterns eliminate inconsistency in error handling, ensure clear error reports with full context trails, and reduce debugging time by capturing rich information at error points. This approach builds more reliable software with faster root-cause identification.

How do I convert external errors to my custom error types in Rust?

Wrap external errors with Report::new, then use .change_context() to convert them to your custom error variant. This seamless type conversion lets you compose error hierarchies while maintaining a consistent error interface.