Free AI web copilot to create summaries, insights and extended knowledge, download it at here
1710
Abstract
e error message, and the URL is essential information for debugging.</p>
<figure id="e56d">
<div>
<div>
<iframe class="gist-iframe" src="/gist/jerryan999/170d7ca4abd593db3ea4ea5c2de1079d.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
</div>
</div>
</figure></iframe></div></div></figure><p id="1e72">There are three tips of best practice to use this error handling strategy.</p><ul><li>First, the error message should be descriptive enough to help the user understand the problem.</li><li>Second, because error messages are frequently chained together, the message should not be capitalized, and newlines should be avoided</li><li>Third, the function caller and callee have different responsibilities to handle the error. The callee reports the attempted operation and the argument value related to the context of the error. The caller adds further context that it has, but the callee does not, such as the URL in the case <code>html.Parse </code>above.</li></ul><h1 id="aaed">Retry</h1><p id="ba8f">The second error handling strategy is retry. It is suitable for transient error cases, and the caller can retry the operation.</p><p id="bfc7">There are a lot of retry strategies, such as fixed backoff, exponential backoff, and random backoff. If you have interested in this topic, you can read more about it by searching the web.</p><p id="16fe">Here we implement a simple retry strategy that retries with exponential backoff for demonstrating the purpose.</p>
<figure id="22d9">
<div>
<div>
<iframe class="gist
Options
-iframe" src="/gist/jerryan999/170d7ca4abd593db3ea4ea5c2de1079d.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
</div>
</div>
</figure></iframe></div></div></figure><h1 id="fc1c">Log the error and continue.</h1><p id="a47d">In some cases, it’s ok to tolerate an error. In such situations, the error is logged, and the program continues.</p>
<figure id="fc56">
<div>
<div>
<iframe class="gist-iframe" src="/gist/jerryan999/170d7ca4abd593db3ea4ea5c2de1079d.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
</div>
</div>
</figure></iframe></div></div></figure><h1 id="5bce">Log and stop the program.</h1><p id="59ae">The fourth error handling strategy is to stop the program. The main function should generally use this strategy to terminate the program.</p>
<figure id="2c94">
<div>
<div>
<iframe class="gist-iframe" src="/gist/jerryan999/170d7ca4abd593db3ea4ea5c2de1079d.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
</div>
</div>
</figure></iframe></div></div></figure><h1 id="2c87">Summary</h1><p id="f0a4">Error handling in Go is essential. Now, let’s start to enjoy the happy journey of error handling with the four strategies.</p><p id="c2b9">I hope you enjoyed reading this. If you’d like to support me as a writer, consider becoming<a href="https://jerryan.medium.com/membership"> a Medium member</a>. You’ll also get unlimited access to every story on Medium.</p></article></body>