Skip to content

Commit

Permalink
Add a simple example for new error-stack users starting new project
Browse files Browse the repository at this point in the history
Fix #3209
  • Loading branch information
dpc committed Sep 22, 2023
1 parent 8434622 commit 5b07a07
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
1 change: 1 addition & 0 deletions libs/error-stack/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ once_cell = "1.18.0"
supports-color = "2.1.0"
supports-unicode = "2.0.0"
owo-colors = "3.5.0"
thiserror = "1.0.48"

[build-dependencies]
rustc_version = "0.4"
Expand Down
42 changes: 42 additions & 0 deletions libs/error-stack/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,48 @@
//!
//! # Quick-Start Guide
//!
//! ## In a new project
//!
//! ```rust
//! # #![allow(dead_code)]
//! use error_stack::ResultExt;
//! // using `thiserror` is not neccessary, but convenient
//! use thiserror::Error;
//!
//! // Errors can enumerate variants users care about
//! // but notably don't need to chain source/inner error manually.
//! #[derive(Error, Debug)]
//! enum AppError {
//! #[error("serious app error: {consequences}")]
//! Serious { consequences: &'static str },
//! #[error("trivial app error")]
//! Trivial,
//! }
//!
//! type AppResult<T> = error_stack::Result<T, AppError>;
//!
//! // Errors can also be a plain `struct`, somewhat like in `anyhow`.
//! #[derive(Error, Debug)]
//! #[error("logic error")]
//! struct LogicError;
//!
//! type LogicResult<T> = error_stack::Result<T, LogicError>;
//!
//! fn do_logic() -> LogicResult<()> {
//! Ok(())
//! }
//!
//! fn main() -> AppResult<()> {
//! // `error-stack` requires developer to properly handle
//! // changing error contexts
//! do_logic().change_context(AppError::Serious {
//! consequences: "math no longer works",
//! })?;
//!
//! Ok(())
//! }
//! ```
//!
//! ## Where to use a Report
//!
//! [`Report`] has been designed to be used as the [`Err`] variant of a `Result`. This crate
Expand Down

0 comments on commit 5b07a07

Please sign in to comment.