hashicorp / go-retryablehttp

Retryable HTTP client in Go
Mozilla Public License 2.0
1.99k stars 251 forks source link

Reduce log level for errors due to context cancellation #165

Open KyleKotowick opened 2 years ago

KyleKotowick commented 2 years ago

If I make a retryable HTTP request with a context, and I cancel that context before the request is complete, this library will log that at the ERROR log level. The resulting log looks something like:

2022-05-05T22:36:11.888-0400 ERROR go-retryablehttp@v0.7.1/client.go:637 request failed {"error": "Get \"https://mydomain.com\": context canceled", "method": "GET", "url": "https://mydomain.com"}

However, there are legitimate reasons to want to cancel the context and any pending HTTP requests as part of "normal operation". For example, if I'm making this HTTP request in response to a different HTTP request I received, and that different request was cancelled, I would want to cancel this HTTP request since it's no longer needed. In such a case, the fact that this request gets cancelled should not be considered an "error".

It would be quite helpful for log hygiene if the code could do an errors.Is(err, context.Cancel) check and, if it returns true, log this at a DEBUG or INFO level instead of at the ERROR level.