162 lines
16 KiB
HTML
162 lines
16 KiB
HTML
<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="generator" content="rustdoc"><meta name="description" content="github crates-io docs-rs"><meta name="keywords" content="rust, rustlang, rust-lang, thiserror"><title>thiserror - Rust</title><link rel="preload" as="font" type="font/woff2" crossorigin href="../SourceSerif4-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../FiraSans-Regular.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../FiraSans-Medium.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../SourceCodePro-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../SourceSerif4-Bold.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../SourceCodePro-Semibold.ttf.woff2"><link rel="stylesheet" type="text/css" href="../normalize.css"><link rel="stylesheet" type="text/css" href="../rustdoc.css" id="mainThemeStyle"><link rel="stylesheet" type="text/css" href="../ayu.css" disabled><link rel="stylesheet" type="text/css" href="../dark.css" disabled><link rel="stylesheet" type="text/css" href="../light.css" id="themeStyle"><script id="default-settings" ></script><script src="../storage.js"></script><script defer src="../crates.js"></script><script defer src="../main.js"></script><noscript><link rel="stylesheet" href="../noscript.css"></noscript><link rel="alternate icon" type="image/png" href="../favicon-16x16.png"><link rel="alternate icon" type="image/png" href="../favicon-32x32.png"><link rel="icon" type="image/svg+xml" href="../favicon.svg"></head><body class="rustdoc mod crate"><!--[if lte IE 11]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="mobile-topbar"><button class="sidebar-menu-toggle">☰</button><a class="sidebar-logo" href="../thiserror/index.html"><div class="logo-container"><img class="rust-logo" src="../rust-logo.svg" alt="logo"></div></a><h2 class="location"></h2></nav><nav class="sidebar"><a class="sidebar-logo" href="../thiserror/index.html"><div class="logo-container"><img class="rust-logo" src="../rust-logo.svg" alt="logo"></div></a><h2 class="location"><a href="#">Crate thiserror</a></h2><div class="sidebar-elems"><div class="block"><ul><li class="version">Version 1.0.38</li><li><a id="all-types" href="all.html">All Items</a></li></ul></div><section><div class="block"><ul><li><a href="#derives">Derive Macros</a></li></ul></div></section></div></nav><main><div class="width-limiter"><div class="sub-container"><a class="sub-logo-container" href="../thiserror/index.html"><img class="rust-logo" src="../rust-logo.svg" alt="logo"></a><nav class="sub"><form class="search-form"><div class="search-container"><span></span><input class="search-input" name="search" autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"><div id="help-button" title="help" tabindex="-1"><button type="button">?</button></div><div id="settings-menu" tabindex="-1"><a href="../settings.html" title="settings"><img width="22" height="22" alt="Change settings" src="../wheel.svg"></a></div></div></form></nav></div><section id="main-content" class="content"><div class="main-heading"><h1 class="fqn"><span class="in-band">Crate <a class="mod" href="#">thiserror</a><button id="copy-path" onclick="copy_path(this)" title="Copy item path to clipboard"><img src="../clipboard.svg" width="19" height="18" alt="Copy item path"></button></span></h1><span class="out-of-band"><a class="srclink" href="../src/thiserror/lib.rs.html#1-254">source</a> · <a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs">[<span class="inner">−</span>]</a></span></div><details class="rustdoc-toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p><a href="https://github.com/dtolnay/thiserror"><img src="https://img.shields.io/badge/github-8da0cb?style=for-the-badge&labelColor=555555&logo=github" alt="github" /></a> <a href="https://crates.io/crates/thiserror"><img src="https://img.shields.io/badge/crates.io-fc8d62?style=for-the-badge&labelColor=555555&logo=rust" alt="crates-io" /></a> <a href="https://docs.rs/thiserror"><img src="https://img.shields.io/badge/docs.rs-66c2a5?style=for-the-badge&labelColor=555555&logo=docs.rs" alt="docs-rs" /></a></p>
|
||
<br>
|
||
<p>This library provides a convenient derive macro for the standard library’s
|
||
<a href="https://doc.rust-lang.org/std/error/trait.Error.html"><code>std::error::Error</code></a> trait.</p>
|
||
<br>
|
||
<h2 id="example"><a href="#example">Example</a></h2>
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use</span> <span class="ident">thiserror::Error</span>;
|
||
|
||
<span class="attribute">#[<span class="ident">derive</span>(<span class="ident">Error</span>, <span class="ident">Debug</span>)]</span>
|
||
<span class="kw">pub</span> <span class="kw">enum</span> <span class="ident">DataStoreError</span> {
|
||
<span class="attribute">#[<span class="ident">error</span>(<span class="string">"data store disconnected"</span>)]</span>
|
||
<span class="ident">Disconnect</span>(<span class="attribute">#[<span class="ident">from</span>]</span> <span class="ident">io::Error</span>),
|
||
<span class="attribute">#[<span class="ident">error</span>(<span class="string">"the data for key `{0}` is not available"</span>)]</span>
|
||
<span class="ident">Redaction</span>(<span class="ident">String</span>),
|
||
<span class="attribute">#[<span class="ident">error</span>(<span class="string">"invalid header (expected {expected:?}, found {found:?})"</span>)]</span>
|
||
<span class="ident">InvalidHeader</span> {
|
||
<span class="ident">expected</span>: <span class="ident">String</span>,
|
||
<span class="ident">found</span>: <span class="ident">String</span>,
|
||
},
|
||
<span class="attribute">#[<span class="ident">error</span>(<span class="string">"unknown data store error"</span>)]</span>
|
||
<span class="ident">Unknown</span>,
|
||
}</code></pre></div>
|
||
<br>
|
||
<h2 id="details"><a href="#details">Details</a></h2>
|
||
<ul>
|
||
<li>
|
||
<p>Thiserror deliberately does not appear in your public API. You get the
|
||
same thing as if you had written an implementation of <code>std::error::Error</code>
|
||
by hand, and switching from handwritten impls to thiserror or vice versa
|
||
is not a breaking change.</p>
|
||
</li>
|
||
<li>
|
||
<p>Errors may be enums, structs with named fields, tuple structs, or unit
|
||
structs.</p>
|
||
</li>
|
||
<li>
|
||
<p>A <code>Display</code> impl is generated for your error if you provide
|
||
<code>#[error("...")]</code> messages on the struct or each variant of your enum, as
|
||
shown above in the example.</p>
|
||
<p>The messages support a shorthand for interpolating fields from the error.</p>
|
||
<ul>
|
||
<li><code>#[error("{var}")]</code> ⟶ <code>write!("{}", self.var)</code></li>
|
||
<li><code>#[error("{0}")]</code> ⟶ <code>write!("{}", self.0)</code></li>
|
||
<li><code>#[error("{var:?}")]</code> ⟶ <code>write!("{:?}", self.var)</code></li>
|
||
<li><code>#[error("{0:?}")]</code> ⟶ <code>write!("{:?}", self.0)</code></li>
|
||
</ul>
|
||
<p>These shorthands can be used together with any additional format args,
|
||
which may be arbitrary expressions. For example:</p>
|
||
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="attribute">#[<span class="ident">derive</span>(<span class="ident">Error</span>, <span class="ident">Debug</span>)]</span>
|
||
<span class="kw">pub</span> <span class="kw">enum</span> <span class="ident">Error</span> {
|
||
<span class="attribute">#[<span class="ident">error</span>(<span class="string">"invalid rdo_lookahead_frames {0} (expected < {})"</span>, <span class="ident">i32::MAX</span>)]</span>
|
||
<span class="ident">InvalidLookahead</span>(<span class="ident">u32</span>),
|
||
}</code></pre></div>
|
||
<p>If one of the additional expression arguments needs to refer to a field of
|
||
the struct or enum, then refer to named fields as <code>.var</code> and tuple fields
|
||
as <code>.0</code>.</p>
|
||
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="attribute">#[<span class="ident">derive</span>(<span class="ident">Error</span>, <span class="ident">Debug</span>)]</span>
|
||
<span class="kw">pub</span> <span class="kw">enum</span> <span class="ident">Error</span> {
|
||
<span class="attribute">#[<span class="ident">error</span>(<span class="string">"first letter must be lowercase but was {:?}"</span>, <span class="ident">first_char</span>(.<span class="number">0</span>))]</span>
|
||
<span class="ident">WrongCase</span>(<span class="ident">String</span>),
|
||
<span class="attribute">#[<span class="ident">error</span>(<span class="string">"invalid index {idx}, expected at least {} and at most {}"</span>, .<span class="ident">limits</span>.<span class="ident">lo</span>, .<span class="ident">limits</span>.<span class="ident">hi</span>)]</span>
|
||
<span class="ident">OutOfBounds</span> { <span class="ident">idx</span>: <span class="ident">usize</span>, <span class="ident">limits</span>: <span class="ident">Limits</span> },
|
||
}</code></pre></div>
|
||
</li>
|
||
<li>
|
||
<p>A <code>From</code> impl is generated for each variant containing a <code>#[from]</code>
|
||
attribute.</p>
|
||
<p>Note that the variant must not contain any other fields beyond the source
|
||
error and possibly a backtrace. A backtrace is captured from within the
|
||
<code>From</code> impl if there is a field for it.</p>
|
||
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="attribute">#[<span class="ident">derive</span>(<span class="ident">Error</span>, <span class="ident">Debug</span>)]</span>
|
||
<span class="kw">pub</span> <span class="kw">enum</span> <span class="ident">MyError</span> {
|
||
<span class="ident">Io</span> {
|
||
<span class="attribute">#[<span class="ident">from</span>]</span>
|
||
<span class="ident">source</span>: <span class="ident">io::Error</span>,
|
||
<span class="ident">backtrace</span>: <span class="ident">Backtrace</span>,
|
||
},
|
||
}</code></pre></div>
|
||
</li>
|
||
<li>
|
||
<p>The Error trait’s <code>source()</code> method is implemented to return whichever
|
||
field has a <code>#[source]</code> attribute or is named <code>source</code>, if any. This is
|
||
for identifying the underlying lower level error that caused your error.</p>
|
||
<p>The <code>#[from]</code> attribute always implies that the same field is <code>#[source]</code>,
|
||
so you don’t ever need to specify both attributes.</p>
|
||
<p>Any error type that implements <code>std::error::Error</code> or dereferences to <code>dyn std::error::Error</code> will work as a source.</p>
|
||
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="attribute">#[<span class="ident">derive</span>(<span class="ident">Error</span>, <span class="ident">Debug</span>)]</span>
|
||
<span class="kw">pub</span> <span class="kw">struct</span> <span class="ident">MyError</span> {
|
||
<span class="ident">msg</span>: <span class="ident">String</span>,
|
||
<span class="attribute">#[<span class="ident">source</span>]</span> <span class="comment">// optional if field name is `source`</span>
|
||
<span class="ident">source</span>: <span class="ident">anyhow::Error</span>,
|
||
}</code></pre></div>
|
||
</li>
|
||
<li>
|
||
<p>The Error trait’s <code>provide()</code> method is implemented to provide whichever
|
||
field has a type named <code>Backtrace</code>, if any, as a
|
||
<code>std::backtrace::Backtrace</code>.</p>
|
||
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use</span> <span class="ident">std::backtrace::Backtrace</span>;
|
||
|
||
<span class="attribute">#[<span class="ident">derive</span>(<span class="ident">Error</span>, <span class="ident">Debug</span>)]</span>
|
||
<span class="kw">pub</span> <span class="kw">struct</span> <span class="ident">MyError</span> {
|
||
<span class="ident">msg</span>: <span class="ident">String</span>,
|
||
<span class="ident">backtrace</span>: <span class="ident">Backtrace</span>, <span class="comment">// automatically detected</span>
|
||
}</code></pre></div>
|
||
</li>
|
||
<li>
|
||
<p>If a field is both a source (named <code>source</code>, or has <code>#[source]</code> or
|
||
<code>#[from]</code> attribute) <em>and</em> is marked <code>#[backtrace]</code>, then the Error
|
||
trait’s <code>provide()</code> method is forwarded to the source’s <code>provide</code> so that
|
||
both layers of the error share the same backtrace.</p>
|
||
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="attribute">#[<span class="ident">derive</span>(<span class="ident">Error</span>, <span class="ident">Debug</span>)]</span>
|
||
<span class="kw">pub</span> <span class="kw">enum</span> <span class="ident">MyError</span> {
|
||
<span class="ident">Io</span> {
|
||
<span class="attribute">#[<span class="ident">backtrace</span>]</span>
|
||
<span class="ident">source</span>: <span class="ident">io::Error</span>,
|
||
},
|
||
}</code></pre></div>
|
||
</li>
|
||
<li>
|
||
<p>Errors may use <code>error(transparent)</code> to forward the source and Display
|
||
methods straight through to an underlying error without adding an
|
||
additional message. This would be appropriate for enums that need an
|
||
“anything else” variant.</p>
|
||
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="attribute">#[<span class="ident">derive</span>(<span class="ident">Error</span>, <span class="ident">Debug</span>)]</span>
|
||
<span class="kw">pub</span> <span class="kw">enum</span> <span class="ident">MyError</span> {
|
||
...
|
||
|
||
<span class="attribute">#[<span class="ident">error</span>(<span class="ident">transparent</span>)]</span>
|
||
<span class="ident">Other</span>(<span class="attribute">#[<span class="ident">from</span>]</span> <span class="ident">anyhow::Error</span>), <span class="comment">// source and Display delegate to anyhow::Error</span>
|
||
}</code></pre></div>
|
||
<p>Another use case is hiding implementation details of an error
|
||
representation behind an opaque error type, so that the representation is
|
||
able to evolve without breaking the crate’s public API.</p>
|
||
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="comment">// PublicError is public, but opaque and easy to keep compatible.</span>
|
||
<span class="attribute">#[<span class="ident">derive</span>(<span class="ident">Error</span>, <span class="ident">Debug</span>)]</span>
|
||
<span class="attribute">#[<span class="ident">error</span>(<span class="ident">transparent</span>)]</span>
|
||
<span class="kw">pub</span> <span class="kw">struct</span> <span class="ident">PublicError</span>(<span class="attribute">#[<span class="ident">from</span>]</span> <span class="ident">ErrorRepr</span>);
|
||
|
||
<span class="kw">impl</span> <span class="ident">PublicError</span> {
|
||
<span class="comment">// Accessors for anything we do want to expose publicly.</span>
|
||
}
|
||
|
||
<span class="comment">// Private and free to change across minor version of the crate.</span>
|
||
<span class="attribute">#[<span class="ident">derive</span>(<span class="ident">Error</span>, <span class="ident">Debug</span>)]</span>
|
||
<span class="kw">enum</span> <span class="ident">ErrorRepr</span> {
|
||
...
|
||
}</code></pre></div>
|
||
</li>
|
||
<li>
|
||
<p>See also the <a href="https://github.com/dtolnay/anyhow"><code>anyhow</code></a> library for a convenient single error type to use
|
||
in application code.</p>
|
||
</li>
|
||
</ul>
|
||
</div></details><h2 id="derives" class="small-section-header"><a href="#derives">Derive Macros</a></h2><div class="item-table"><div class="item-row"><div class="item-left module-item"><a class="derive" href="derive.Error.html" title="thiserror::Error derive">Error</a></div><div class="item-right docblock-short"></div></div></div></section></div></main><div id="rustdoc-vars" data-root-path="../" data-current-crate="thiserror" data-themes="ayu,dark,light" data-resource-suffix="" data-rustdoc-version="1.64.0 (a55dd71d5 2022-09-19)" ></div></body></html> |