I am an intermediate Rust developer, and you may know me from my posts on subreddits like r/opensource or r/rust or from my various projects on GitHub.
Recently, we decided to do error handling and provide custom error messages for the errors related to each engine code present under the src/engines folder in one of my project’s, websurfx, 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, @xffxff, 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.
For this tutorial, I will be writing a simple scraping program to scrape the example.com webpage, and then we will write code to handle errors with enums based on it. Let’s dive straight into it.
Simple Scraping Program
Let’s first start by explaining the code I have written to scrape the More information href link in the example.com webpage. We will use it throughout the program to write the error-handling code for it.