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 b5b3ef1 commit c955daf
Show file tree
Hide file tree
Showing 2 changed files with 41 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.0.0"
supports-unicode = "2.0.0"
owo-colors = "3.5.0"
thiserror = "1.0.48"

[build-dependencies]
rustc_version = "0.4"
Expand Down
40 changes: 40 additions & 0 deletions libs/error-stack/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,46 @@
//!
//! # 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 carry source/inner error manually.
//! #[derive(Error, Debug)]
//! enum AppError {
//! #[error("serious error: {consequences}")]
//! Serious {
//! consequences: &'static str,
//! },
//! #[error("trivial 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<()> {
//! 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 c955daf

Please sign in to comment.