http: don't log an error if the request is cancelled

Now that we have at least one endpoint that uses context for cancellation we can
encounter this scenario where the returned error is a context.Cancelled or
context.DeadlineExceeded.

If the request.Context().Err() is not nil, then we know the request itself was cancelled, so
we can log a different message at Info level, instad of the error.
This commit is contained in:
Daniel Nephin 2021-07-27 17:06:59 -04:00
parent df04f4fe52
commit cfc829275c
1 changed files with 14 additions and 6 deletions

View File

@ -432,12 +432,20 @@ func (s *HTTPHandlers) wrap(handler endpoint, methods []string) http.HandlerFunc
} }
handleErr := func(err error) { handleErr := func(err error) {
httpLogger.Error("Request error", if req.Context().Err() != nil {
"method", req.Method, httpLogger.Info("Request cancelled",
"url", logURL, "method", req.Method,
"from", req.RemoteAddr, "url", logURL,
"error", err, "from", req.RemoteAddr,
) "error", err)
} else {
httpLogger.Error("Request error",
"method", req.Method,
"url", logURL,
"from", req.RemoteAddr,
"error", err)
}
switch { switch {
case isForbidden(err): case isForbidden(err):
resp.WriteHeader(http.StatusForbidden) resp.WriteHeader(http.StatusForbidden)