<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Simone Romano</title>
    <description>The latest articles on DEV Community by Simone Romano (@simonerom).</description>
    <link>https://dev.to/simonerom</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F451654%2F7b619ab9-d7b2-4f9e-9301-88969987b12f.jpg</url>
      <title>DEV Community: Simone Romano</title>
      <link>https://dev.to/simonerom</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/simonerom"/>
    <language>en</language>
    <item>
      <title>Custom Errors in Solidity: A More Efficient and More Expressive Approach</title>
      <dc:creator>Simone Romano</dc:creator>
      <pubDate>Sun, 06 Aug 2023 15:03:34 +0000</pubDate>
      <link>https://dev.to/simonerom/custom-errors-in-solidity-a-more-efficient-and-more-expressive-approach-12jc</link>
      <guid>https://dev.to/simonerom/custom-errors-in-solidity-a-more-efficient-and-more-expressive-approach-12jc</guid>
      <description>&lt;p&gt;One of the coolest features introduced in Solidity version 0.8.4 is "Custom Errors", and I love it. Custom Errors replace the traditional "revert with message" approach, giving us a more efficient and more expressive way to handle errors in our smart contracts.&lt;/p&gt;

&lt;h2&gt;
  
  
  A Game-Changer
&lt;/h2&gt;

&lt;p&gt;Remember the good old days when we used "revert with message" to throw an exception in our contracts? Sure, it did the job, but it had some quirks that sometimes led to unexpected pitfalls. Firstly, it lacked clarity and a standardized format for error messages, making it hard for users to grasp what went wrong. On top of that, the extra gas consumption for those string messages meant higher transaction costs - ouch! Not to mention, it didn't allow us to differentiate between various types of errors easily, hampering our error handling strategies. Did you want to include the value of a variable in the error message? Sorry, not supported (you needed external libraries).&lt;/p&gt;

&lt;h2&gt;
  
  
  Welcome Custom Errors!
&lt;/h2&gt;

&lt;p&gt;Now, let's dive into the cool stuff - Custom Errors! Solidity version 0.8.4 gave us the "error" keyword, and it's a game-changer. Similarly to the event keyword that allows us to create and emit customized events that can be uniquely identified. Using this new keyword we can now explicitly define our error types, including their own arguments, and then "emit" an instance of the correct error when we need to revert.&lt;/p&gt;

&lt;h2&gt;
  
  
   Let's See It in Action!
&lt;/h2&gt;

&lt;p&gt;Alright, let's take a sneak peek into a simplified example of custom errors in action:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight solidity"&gt;&lt;code&gt;&lt;span class="c1"&gt;// SPDX-License-Identifier: MIT
&lt;/span&gt;&lt;span class="k"&gt;pragma&lt;/span&gt; &lt;span class="n"&gt;solidity&lt;/span&gt; &lt;span class="o"&gt;^&lt;/span&gt;&lt;span class="mf"&gt;0.8&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="n"&gt;error&lt;/span&gt; &lt;span class="n"&gt;CustomError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;uint256&lt;/span&gt; &lt;span class="n"&gt;number&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="k"&gt;contract&lt;/span&gt; &lt;span class="n"&gt;CustomErrorsExample&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;uint256&lt;/span&gt; &lt;span class="n"&gt;number&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;external&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;number&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nb"&gt;revert&lt;/span&gt; &lt;span class="n"&gt;CustomError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Number must be less than 100."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;number&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="c1"&gt;// Rest of the function's logic.
&lt;/span&gt;    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Look how simple it is to include a message along with all required data: the client will can identify the exact error that was emitted by the revert call, [ABI-] decode the arguments that where passed, and correctly handle it. How awesome is that?&lt;/p&gt;

&lt;h2&gt;
  
  
  Benefits of Custom Errors
&lt;/h2&gt;

&lt;p&gt;The benefits we get from custom errors are just amazing! First and foremost, we can create different error types for various scenarios. This gives us the flexibility to implement targeted and specific error handling strategies in the client. Say goodbye to generic catch-alls! &lt;/p&gt;

&lt;p&gt;But that's not all! Brace yourself for better gas efficiency. Yes, you heard it right! Unlike reverting with a string, contracts that use custom errors are cheaper to deploy, and cheaper to execute, resulting in happier users.&lt;/p&gt;

&lt;p&gt;Finally, by passing extra data with the error message, we provide users with detailed insights into what caused the error. Users no longer need to play the guessing game when something goes wrong - they'll receive all the essential information right there in the error message.&lt;/p&gt;

&lt;h2&gt;
  
  
   Wrap Up
&lt;/h2&gt;

&lt;p&gt;Let's upgrade our error-handling game and dive into custom errors. It's time to craft decentralized applications that are more efficient and more easy to interact with!&lt;/p&gt;

&lt;p&gt;If you want to learn more about Custom Errors in Solidity, check out the official Blog post at &lt;a href="https://soliditylang.org/blog/2021/04/21/custom-errors/"&gt;https://soliditylang.org/blog/2021/04/21/custom-errors/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Happy coding!&lt;/em&gt;&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
