github.com/muhammadn/cortex@v1.9.1-0.20220510110439-46bb7000d03d/pkg/ingester/errors.go (about)

     1  package ingester
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  
     7  	"github.com/prometheus/prometheus/pkg/labels"
     8  	"github.com/weaveworks/common/httpgrpc"
     9  )
    10  
    11  type validationError struct {
    12  	err       error // underlying error
    13  	errorType string
    14  	code      int
    15  	noReport  bool // if true, error will be counted but not reported to caller
    16  	labels    labels.Labels
    17  }
    18  
    19  func makeLimitError(errorType string, err error) error {
    20  	return &validationError{
    21  		errorType: errorType,
    22  		err:       err,
    23  		code:      http.StatusBadRequest,
    24  	}
    25  }
    26  
    27  func makeNoReportError(errorType string) error {
    28  	return &validationError{
    29  		errorType: errorType,
    30  		noReport:  true,
    31  	}
    32  }
    33  
    34  func makeMetricValidationError(errorType string, labels labels.Labels, err error) error {
    35  	return &validationError{
    36  		errorType: errorType,
    37  		err:       err,
    38  		code:      http.StatusBadRequest,
    39  		labels:    labels,
    40  	}
    41  }
    42  
    43  func makeMetricLimitError(errorType string, labels labels.Labels, err error) error {
    44  	return &validationError{
    45  		errorType: errorType,
    46  		err:       err,
    47  		code:      http.StatusBadRequest,
    48  		labels:    labels,
    49  	}
    50  }
    51  
    52  func (e *validationError) Error() string {
    53  	if e.err == nil {
    54  		return e.errorType
    55  	}
    56  	if e.labels == nil {
    57  		return e.err.Error()
    58  	}
    59  	return fmt.Sprintf("%s for series %s", e.err.Error(), e.labels.String())
    60  }
    61  
    62  // returns a HTTP gRPC error than is correctly forwarded over gRPC, with no reference to `e` retained.
    63  func grpcForwardableError(userID string, code int, e error) error {
    64  	return httpgrpc.ErrorFromHTTPResponse(&httpgrpc.HTTPResponse{
    65  		Code: int32(code),
    66  		Body: []byte(wrapWithUser(e, userID).Error()),
    67  	})
    68  }
    69  
    70  // wrapWithUser prepends the user to the error. It does not retain a reference to err.
    71  func wrapWithUser(err error, userID string) error {
    72  	return fmt.Errorf("user=%s: %s", userID, err)
    73  }