Handling the expected – timeouts and errors
When building a CLI that calls external commands or sends HTTP requests to an external API, with data that is passed in by the user, it’s a good idea to expect the unexpected. In a perfect world, you can guard against bad data. I’m sure you are familiar with the phrase garbage in, garbage out. You can create tests that also ensure that your code is covered for as many bad cases as you can think of. However, timeouts and errors happen. It’s the nature of software, and as you come across them within your development and also in production, you can modify your code to handle new cases.
Timeouts with external command processes
Let’s first discuss how to handle timeouts when calling external commands. The timeout code exists within the examples/timeout.go
file. The following is the entire method, which calls the timeout
command. If you take a look at the timeout
command code, located within cmd/timeout...