Instrumentation: Handle context.Canceled (#75867)

Ref #68480

Co-authored-by: Giuseppe Guerra <giuseppe.guerra@grafana.com>
This commit is contained in:
Marcus Efraimsson
2023-10-10 12:28:39 +02:00
committed by GitHub
parent aee8c91ac8
commit 90631360eb
8 changed files with 72 additions and 0 deletions

View File

@@ -2,6 +2,7 @@ package response
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
@@ -18,6 +19,9 @@ import (
"github.com/grafana/grafana/pkg/util/errutil"
)
var errRequestCanceledBase = errutil.ClientClosedRequest("api.requestCanceled",
errutil.WithPublicMessage("Request canceled"))
// Response is an HTTP response interface.
type Response interface {
// WriteTo writes to a context.
@@ -286,12 +290,18 @@ func Err(err error) *NormalResponse {
// The signature is equivalent to that of Error which allows us to
// rename this to Error when we're confident that that would be safe to
// do.
// If the error provided is not an errutil.Error and is/wraps context.Canceled
// the function returns an Err(errRequestCanceledBase).
func ErrOrFallback(status int, message string, err error) *NormalResponse {
grafanaErr := errutil.Error{}
if errors.As(err, &grafanaErr) {
return Err(err)
}
if errors.Is(err, context.Canceled) {
return Err(errRequestCanceledBase.Errorf("response: request canceled: %w", err))
}
return Error(status, message, err)
}