...

Source file src/github.com/chaos-mesh/chaos-mesh/pkg/dashboard/apiserver/utils/error.go

Documentation: github.com/chaos-mesh/chaos-mesh/pkg/dashboard/apiserver/utils

     1  // Copyright 2021 Chaos Mesh Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  // http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  //
    15  
    16  package utils
    17  
    18  import (
    19  	"fmt"
    20  	"net/http"
    21  	"strings"
    22  
    23  	"github.com/gin-gonic/gin"
    24  	"github.com/joomcode/errorx"
    25  	apierrors "k8s.io/apimachinery/pkg/api/errors"
    26  )
    27  
    28  var (
    29  	ErrNS             = errorx.NewNamespace("error.api")
    30  	ErrUnknown        = ErrNS.NewType("unknown")               // 500
    31  	ErrBadRequest     = ErrNS.NewType("bad_request")           // 400
    32  	ErrNotFound       = ErrNS.NewType("resource_not_found")    // 404
    33  	ErrInternalServer = ErrNS.NewType("internal_server_error") // 500
    34  	// Custom
    35  	ErrNoClusterPrivilege   = ErrNS.NewType("no_cluster_privilege")   // 401
    36  	ErrNoNamespacePrivilege = ErrNS.NewType("no_namespace_privilege") // 401
    37  )
    38  
    39  type APIError struct {
    40  	Code     int    `json:"code"`
    41  	Type     string `json:"type"`
    42  	Message  string `json:"message"`
    43  	FullText string `json:"full_text"`
    44  }
    45  
    46  func SetAPIError(c *gin.Context, err *errorx.Error) {
    47  	typeName := errorx.GetTypeName(err)
    48  
    49  	var code int
    50  	switch typeName {
    51  	case ErrBadRequest.FullName():
    52  		code = http.StatusBadRequest
    53  	case ErrNoClusterPrivilege.FullName(), ErrNoNamespacePrivilege.FullName():
    54  		code = http.StatusUnauthorized
    55  	case ErrNotFound.FullName():
    56  		code = http.StatusNotFound
    57  	case ErrUnknown.FullName(), ErrInternalServer.FullName():
    58  		code = http.StatusInternalServerError
    59  	default:
    60  		code = http.StatusInternalServerError
    61  	}
    62  
    63  	apiError := APIError{
    64  		Code:     code,
    65  		Type:     typeName,
    66  		Message:  err.Error(),
    67  		FullText: fmt.Sprintf("%+v", err),
    68  	}
    69  
    70  	Log.Error(err.Cause(), typeName)
    71  	c.AbortWithStatusJSON(code, &apiError)
    72  }
    73  
    74  func SetAPImachineryError(c *gin.Context, err error) {
    75  	if apierrors.IsForbidden(err) && strings.Contains(err.Error(), "at the cluster scope") {
    76  		SetAPIError(c, ErrNoClusterPrivilege.WrapWithNoMessage(err))
    77  
    78  		return
    79  	} else if apierrors.IsForbidden(err) && strings.Contains(err.Error(), "in the namespace") {
    80  		SetAPIError(c, ErrNoNamespacePrivilege.WrapWithNoMessage(err))
    81  
    82  		return
    83  	} else if apierrors.IsNotFound(err) {
    84  		SetAPIError(c, ErrNotFound.WrapWithNoMessage(err))
    85  
    86  		return
    87  	}
    88  
    89  	SetAPIError(c, ErrInternalServer.WrapWithNoMessage(err))
    90  }
    91