#error #derive

macro derive-error

Derive macro for Error using macros 1.1

6 releases

Uses old Rust 2015

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

#767 in Procedural macros

Download history 2042/week @ 2023-11-01 2092/week @ 2023-11-08 2257/week @ 2023-11-15 1342/week @ 2023-11-22 1702/week @ 2023-11-29 1703/week @ 2023-12-06 1803/week @ 2023-12-13 1120/week @ 2023-12-20 1091/week @ 2023-12-27 1842/week @ 2024-01-03 1593/week @ 2024-01-10 1601/week @ 2024-01-17 1680/week @ 2024-01-24 1543/week @ 2024-01-31 1968/week @ 2024-02-07 1441/week @ 2024-02-14

6,958 downloads per month
Used in 285 crates (28 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

~1.5MB
~40K SLoC