mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Instrumentation: Handle context.Canceled (#75867)
Ref #68480 Co-authored-by: Giuseppe Guerra <giuseppe.guerra@grafana.com>
This commit is contained in:
committed by
GitHub
parent
aee8c91ac8
commit
90631360eb
@@ -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)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user