From cfc829275c2122e2a343953979bacb29c952c97f Mon Sep 17 00:00:00 2001 From: Daniel Nephin Date: Tue, 27 Jul 2021 17:06:59 -0400 Subject: [PATCH] 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. --- agent/http.go | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/agent/http.go b/agent/http.go index 60195f2a5..409ecbda4 100644 --- a/agent/http.go +++ b/agent/http.go @@ -432,12 +432,20 @@ func (s *HTTPHandlers) wrap(handler endpoint, methods []string) http.HandlerFunc } handleErr := func(err error) { - httpLogger.Error("Request error", - "method", req.Method, - "url", logURL, - "from", req.RemoteAddr, - "error", err, - ) + if req.Context().Err() != nil { + httpLogger.Info("Request cancelled", + "method", req.Method, + "url", logURL, + "from", req.RemoteAddr, + "error", err) + } else { + httpLogger.Error("Request error", + "method", req.Method, + "url", logURL, + "from", req.RemoteAddr, + "error", err) + } + switch { case isForbidden(err): resp.WriteHeader(http.StatusForbidden)