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) {
if req.Context().Err() != nil {
httpLogger.Info("Request cancelled",
"method", req.Method,
"url", logURL,
"from", req.RemoteAddr,
"error", err)
} else {
httpLogger.Error("Request error", httpLogger.Error("Request error",
"method", req.Method, "method", req.Method,
"url", logURL, "url", logURL,
"from", req.RemoteAddr, "from", req.RemoteAddr,
"error", err, "error", err)
) }
switch { switch {
case isForbidden(err): case isForbidden(err):
resp.WriteHeader(http.StatusForbidden) resp.WriteHeader(http.StatusForbidden)