github.com/kiali/kiali@v1.84.0/handlers/errors.go (about)

     1  package handlers
     2  
     3  import (
     4  	"net/http"
     5  	"strings"
     6  
     7  	"k8s.io/apimachinery/pkg/api/errors"
     8  
     9  	"github.com/kiali/kiali/business"
    10  	"github.com/kiali/kiali/log"
    11  )
    12  
    13  // Helper method to adjust error code in the handler's response
    14  // It helps for business methods that can respond AccessibleError and NotFound cases
    15  // Some handlers can use a direct response
    16  func handleErrorResponse(w http.ResponseWriter, err error, extraMesg ...string) {
    17  	errorMsg := err.Error()
    18  	if len(extraMesg) > 0 {
    19  		errorMsg = strings.Join(extraMesg, ";")
    20  	}
    21  	log.Error(errorMsg)
    22  	if business.IsAccessibleError(err) {
    23  		RespondWithError(w, http.StatusForbidden, errorMsg)
    24  	} else if errors.IsNotFound(err) {
    25  		RespondWithError(w, http.StatusNotFound, errorMsg)
    26  	} else if errors.IsServiceUnavailable(err) {
    27  		RespondWithError(w, http.StatusServiceUnavailable, errorMsg)
    28  	} else if statusError, isStatus := err.(*errors.StatusError); isStatus {
    29  		errorMsg = statusError.ErrStatus.Message
    30  		RespondWithError(w, http.StatusInternalServerError, errorMsg)
    31  	} else {
    32  		RespondWithError(w, http.StatusInternalServerError, errorMsg)
    33  	}
    34  }