github.com/kiali/kiali@v1.84.0/mesh/util.go (about)

     1  package mesh
     2  
     3  import (
     4  	nethttp "net/http"
     5  
     6  	"github.com/kiali/kiali/status"
     7  )
     8  
     9  type Response struct {
    10  	Message string
    11  	Code    int
    12  }
    13  
    14  var (
    15  	// StatusGetter var allows test code to mock out this function with a mock
    16  	StatusGetter func() status.StatusInfo = status.Get
    17  )
    18  
    19  // Error panics with InternalServerError (500) and the provided message
    20  func Error(message string) {
    21  	Panic(message, nethttp.StatusInternalServerError)
    22  }
    23  
    24  // BadRequest panics with BadRequest and the provided message
    25  func BadRequest(message string) {
    26  	Panic(message, nethttp.StatusBadRequest)
    27  }
    28  
    29  // Forbidden panics with Forbidden and the provided message
    30  func Forbidden(message string) {
    31  	Panic(message, nethttp.StatusForbidden)
    32  }
    33  
    34  // Panic panics with the provided HTTP response code and message
    35  func Panic(message string, code int) Response {
    36  	panic(Response{
    37  		Message: message,
    38  		Code:    code,
    39  	})
    40  }
    41  
    42  // CheckError panics with the supplied error if it is non-nil
    43  func CheckError(err error) {
    44  	if err != nil {
    45  		panic(err.Error())
    46  	}
    47  }
    48  
    49  // CheckUnavailable panics with StatusServiceUnavailable (503) and the supplied error if it is non-nil
    50  func CheckUnavailable(err error) {
    51  	if err != nil {
    52  		Panic(err.Error(), nethttp.StatusServiceUnavailable)
    53  	}
    54  }
    55  
    56  // IsOK just validates that a telemetry label value is not empty
    57  func IsOK(telemetryVal string) bool {
    58  	return telemetryVal != ""
    59  }