How To Wrap Your Errors With Enums When Using Error-Stack
<p>I am an intermediate Rust developer, and you may know me from my posts on subreddits like <a href="https://reddit.com/r/opensource" rel="noopener ugc nofollow" target="_blank">r/opensource</a> or <a href="https://reddit.com/r/rust" rel="noopener ugc nofollow" target="_blank">r/rust</a> or from my various projects on GitHub.</p>
<p>Recently, we decided to do error handling and provide custom error messages for the errors related to each engine code present under the <code>src/engines</code> folder in one of my project’s, <a href="https://github.com/neon-mmd/websurfx" rel="noopener ugc nofollow" target="_blank">websurfx</a>, when using error-stack. We wanted to use enums for it, and we found that the error-stack project provided no guide, tutorial, or examples. But one of our maintainers, <a href="https://github.com/xffxff" rel="noopener ugc nofollow" target="_blank">@xffxff</a>, provided a cool solution to this problem that helped me learn much, so I decided to share it with you all. You’ll learn how you can wrap errors with enums when using error-stack, so stick around until the end of the article.</p>
<p> </p>
<p><a href="https://www.pexels.com/photo/a-person-trimming-a-wood-plank-with-and-edge-1-6654031/" rel="noopener ugc nofollow" target="_blank">Image by cottonbro on Pexels</a></p>
<p>For this tutorial, I will be writing a simple scraping program to scrape the <a href="https://example.com/" rel="noopener ugc nofollow" target="_blank">example.com</a> webpage, and then we will write code to handle errors with enums based on it. Let’s dive straight into it.</p>
<h1>Simple Scraping Program</h1>
<p>Let’s first start by explaining the code I have written to scrape the <code>More information href link</code> in the <a href="https://example.com/" rel="noopener ugc nofollow" target="_blank">example.com</a> webpage. We will use it throughout the program to write the <code>error-handling</code> code for it.</p>
<p><a href="https://betterprogramming.pub/how-to-wrap-your-errors-with-enums-when-using-error-stack-77b122016e6e">Read More</a></p>