6 releases

Uses old Rust 2015

0.0.5 Feb 26, 2021
0.0.4 Oct 4, 2017
0.0.3 Jan 29, 2017

#787 in Procedural macros

Download history 2192/week @ 2024-07-22 1800/week @ 2024-07-29 2251/week @ 2024-08-05 2414/week @ 2024-08-12 2241/week @ 2024-08-19 1685/week @ 2024-08-26 1867/week @ 2024-09-02 1555/week @ 2024-09-09 1544/week @ 2024-09-16 1794/week @ 2024-09-23 1812/week @ 2024-09-30 1998/week @ 2024-10-07 2220/week @ 2024-10-14 2182/week @ 2024-10-21 1954/week @ 2024-10-28 1931/week @ 2024-11-04

8,396 downloads per month
Used in 289 crates (29 directly)

MIT/Apache

17KB
311 lines

Derive Rust Errors

Build Status Latest Version Docs

This crate uses macros 1.1 to derive custom errors.

Getting Started

Add this crate to your dependencies section:-

[dependencies]
derive-error = "0.0.4"

Import it in your main.rs or lib.rs:-

#[macro_use]
extern crate derive_error;

Deriving errors is simple. Simply create an enum for your errors as suggested in the Rust book, add short descriptions for the enum variants using doc comments, throw in a #[derive(Debug, Error)] and you are done. Here is the example in the book implemented using this library:-

#[derive(Debug, Error)]
enum CliError {
  /// IO Error
  Io(io::Error),
  /// Failed to parse the CSV file
  Csv(csv::Error),
  /// No matching cities with a population were found
  NotFound,
}

This will derive implementations for Display, Error and From. See the reql crate for a real world example of how to use this crate.

Configuration

Not all errors are exactly the same and need, for example, From implementations. derive-error supports attributes on Enum variants that allow you to enable/disable certain aspects of how Error deriving functions. There are 3 attributes that can be attached to variants of enums deriving Error: non_std, msg_embedded, no_from.

msg_embedded displays the error through an item in the variant, generally a String.

no_from skips the From impl for the variant of the enum, you want this if the error isn't coming from somewhere else, and your code is essentially the source of the error.

non_std displays the error from the variant via it's doc comment /// or it's embedded message (if the variant has that attribute), otherwise it will try and use the cause of the underlying error inside the variant. This is usually combined with no_from.

#[derive(Debug, Error)]
enum Error {
	#[error(msg_embedded, no_from, non_std)]
	RuntimeError(String),

	Io(Io::Error),

	#[error(non_std, no_from)]
	Json(serde_json::Value)
}

This example showcases how to attach the attributes. RuntimeError has a String internally which in the case of an error will be used for displaying. Io is getting a From implementation for Io::Error and this is generally the most common usecase. It is also possible to embed values directly in errors, in the Json variant it's embedding a Value inside, to be used later on for other information.

These were adapted from the the reql crate's usage of derive-error

Dependencies

~2MB
~43K SLoC