<span><svgxmlns="http://www.w3.org/2000/svg"aria-hidden="true"focusable="false"x="0px"y="0px"viewBox="0 0 100 100"width="15"height="15"class="icon outbound"><pathfill="currentColor"d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path><polygonfill="currentColor"points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg><spanclass="sr-only">(opens new window)</span></span></a></div><!----></nav><ulclass="sidebar-links"><li><sectionclass="sidebar-group depth-0"><pclass="sidebar-heading"><span>Getting Started</span><!----></p><ulclass="sidebar-links sidebar-group-items"><li><ahref="/anchor/getting-started/introduction.html"class="sidebar-link">Introduction</a></li><li><ahref="/anchor/getting-started/installation.html"class="sidebar-link">Installing Dependencies</a></li></ul></section></li><li><sectionclass="sidebar-group depth-0"><pclass="sidebar-heading"><span>Teams</span><!----></p><ulclass="sidebar-links sidebar-group-items"><li><ahref="/anchor/getting-started/projects.html"class="sidebar-link">Projects</a></li></ul></section></li><li><sectionclass="sidebar-group depth-0"><pclass="sidebar-heading open"><span>Tutorials</span><!----></p><ulclass="sidebar-links sidebar-group-items"><li><ahref="/anchor/tutorials/tutorial-0.html"class="sidebar-link">A Minimal Example</a></li><li><ahref="/anchor/tutorials/tutorial-1.html"class="sidebar-link">Arguments and Accounts</a></li><li><ahref="/anchor/tutorials/tutorial-2.html"class="sidebar-link">Account Constraints and Access Control</a></li><li><ahref="/anchor/tutorials/tutorial-3.html"class="sidebar-link">Cross Program Invocations (CPI)</a></li><li><ahref="/anchor/tutorials/tutorial-4.html"aria-current="page"class="active sidebar-link">Errors</a><ulclass="sidebar-sub-headers"><liclass="sidebar-sub-header"><ahref="/anchor/tutorials/tutorial-4.html#defining-a-program"class="sidebar-link">Defining a Program</a></li><liclass="sidebar-sub-header"><ahref="/anchor/tutorials/tutorial-4.html#using-the-client"class="sidebar-link">Using the Client</a></li></ul></li></ul></section></li><li><sectionclass="sidebar-group depth-0"><pclass="sidebar-heading"><span>CLI</span><!----></p><ulclass="sidebar-links sidebar-group-items"><li><ahref="/anchor/cli/commands.html"class="sidebar-link">Commands</a></li></ul></section></li><li><sectionclass="sidebar-group depth-0"><pclass="sidebar-heading"><span>Source Verification</span><!----></p><ulclass="sidebar-links sidebar-group-items"><li><ahref="/anchor/getting-started/verification.html"class="sidebar-link">Verifiable Builds</a></li><li><ahref="/anchor/getting-started/publishing.html"class="sidebar-link">Publishing Source</a></li></ul></section></li></ul></aside><mainclass="page"><divclass="theme-default-content content__default"><h1id="errors"><ahref="#errors"class="header-anchor">#</a> Errors</h1><p>If you've ever programmed on a blockchain, you've probably been frustrated by
either non existant or opaque error codes. Anchor attempts to address this by
providing the <code>#[error]</code> attribute, which can be used to create typed Errors with
descriptive messages that automatically propagate to the client.</p><h2id="defining-a-program"><ahref="#defining-a-program"class="header-anchor">#</a> Defining a Program</h2><p>For example,</p><divclass="language-rust extra-class"><preclass="language-rust"><code><spanclass="token keyword">use</span><spanclass="token namespace">anchor_lang<spanclass="token punctuation">::</span>prelude<spanclass="token punctuation">::</span></span><spanclass="token operator">*</span><spanclass="token punctuation">;</span>
<spanclass="token attribute attr-name">#[msg(<spanclass="token string">"This is an error message clients will automatically display"</span>)]</span>
</code></pre></div><p>Observe the <ahref="https://docs.rs/anchor-lang/latest/anchor_lang/attr.error.html"target="_blank"rel="noopener noreferrer">#[error]<span><svgxmlns="http://www.w3.org/2000/svg"aria-hidden="true"focusable="false"x="0px"y="0px"viewBox="0 0 100 100"width="15"height="15"class="icon outbound"><pathfill="currentColor"d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path><polygonfill="currentColor"points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg><spanclass="sr-only">(opens new window)</span></span></a> attribute on the <code>ErrorCode</code> enum. This macro generates two types: an <code>Error</code> and a <code>Result</code>, both of which can be used when returning from your program.</p><p>To use the <code>Error</code>, you can simply use the user defined <code>ErrorCode</code> with Rust's <ahref="https://doc.rust-lang.org/std/convert/trait.From.html"target="_blank"rel="noopener noreferrer">From<span><svgxmlns="http://www.w3.org/2000/svg"aria-hidden="true"focusable="false"x="0px"y="0px"viewBox="0 0 100 100"width="15"height="15"class="icon outbound"><pathfill="currentColor"d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path><polygonfill="currentColor"points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg><spanclass="sr-only">(opens new window)</span></span></a> trait. If you're unfamiliar with <code>From</code>, no worries. Just know that you need to either call
<code>.into()</code> when using your <code>ErrorCode</code>. Or use Rust's <code>?</code> operator, when returning an error.
Both of these will automatically convert <em>into</em> the correct <code>Error</code>.</p><detailsclass="custom-block details"><summary>DETAILS</summary><p>What's the deal with this From stuff? Well, because the Solana runtime expects a <ahref="https://docs.rs/solana-program/1.5.5/solana_program/program_error/enum.ProgramError.html"target="_blank"rel="noopener noreferrer">ProgramError<span><svgxmlns="http://www.w3.org/2000/svg"aria-hidden="true"focusable="false"x="0px"y="0px"viewBox="0 0 100 100"width="15"height="15"class="icon outbound"><pathfill="currentColor"d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path><polygonfill="currentColor"points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg><spanclass="sr-only">(opens new window)</span></span></a> in the return value. The framework needs to wrap the user defined error code into a
<code>ProgramError::Code</code> variant, before returning. The alternative would be to use the
<code>ProgramError</code> directly.</p></details><h2id="using-the-client"><ahref="#using-the-client"class="header-anchor">#</a> Using the Client</h2><p>When using the client, we get the error message.</p><divclass="language-javascript extra-class"><preclass="language-javascript"><code><spanclass="token keyword">try</span><spanclass="token punctuation">{</span>
<spanclass="token keyword">const</span> errMsg <spanclass="token operator">=</span><spanclass="token string">"This is an error message clients will automatically display"</span><spanclass="token punctuation">;</span>
</code></pre></div><p>It's that easy. 😃</p><p>To run the full example, go <ahref="https://github.com/project-serum/anchor/tree/master/examples/tutorial/basic-4"target="_blank"rel="noopener noreferrer">here<span><svgxmlns="http://www.w3.org/2000/svg"aria-hidden="true"focusable="false"x="0px"y="0px"viewBox="0 0 100 100"width="15"height="15"class="icon outbound"><pathfill="currentColor"d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path><polygonfill="currentColor"points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg><spanclass="sr-only">(opens new window)</span></span></a>.</p></div><footerclass="page-edit"><!----><!----></footer><divclass="page-nav"><pclass="inner"><spanclass="prev">