github.com/kubewharf/katalyst-core@v0.5.3/pkg/util/general/error.go (about)

     1  /*
     2  Copyright 2022 The Katalyst Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package general
    18  
    19  import (
    20  	"encoding/json"
    21  	"errors"
    22  	"fmt"
    23  
    24  	"google.golang.org/grpc/codes"
    25  	"google.golang.org/grpc/status"
    26  )
    27  
    28  // common errors
    29  var (
    30  	ErrNotFound    = fmt.Errorf("not found")
    31  	ErrKeyNotExist = errors.New("key does not exist")
    32  )
    33  
    34  // IsUnmarshalTypeError check whether is json unmarshal type error
    35  func IsUnmarshalTypeError(err error) bool {
    36  	if _, ok := err.(*json.UnmarshalTypeError); ok {
    37  		return true
    38  	}
    39  	return false
    40  }
    41  
    42  func IsErrNotFound(err error) bool {
    43  	return err == ErrNotFound
    44  }
    45  
    46  func IsErrKeyNotExist(err error) bool {
    47  	return err == ErrKeyNotExist
    48  }
    49  
    50  func IsUnimplementedError(err error) bool {
    51  	// Sources:
    52  	// https://github.com/grpc/grpc/blob/master/doc/statuscodes.md
    53  	// https://github.com/container-storage-interface/spec/blob/master/spec.md
    54  	st, ok := status.FromError(err)
    55  	if !ok {
    56  		// This is not gRPC error. The operation must have failed before gRPC
    57  		// method was called, otherwise we would get gRPC error.
    58  		// We don't know if any previous volume operation is in progress, be on the safe side.
    59  		return false
    60  	}
    61  	switch st.Code() {
    62  	case codes.Unimplemented:
    63  		return true
    64  	}
    65  	return false
    66  }