github.com/IBM-Blockchain/fabric-operator@v1.0.4/pkg/operatorerrors/errors.go (about)

     1  /*
     2   * Copyright contributors to the Hyperledger Fabric Operator project
     3   *
     4   * SPDX-License-Identifier: Apache-2.0
     5   *
     6   * Licensed under the Apache License, Version 2.0 (the "License");
     7   * you may not use this file except in compliance with the License.
     8   * You may obtain a copy of the License at:
     9   *
    10   * 	  http://www.apache.org/licenses/LICENSE-2.0
    11   *
    12   * Unless required by applicable law or agreed to in writing, software
    13   * distributed under the License is distributed on an "AS IS" BASIS,
    14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    15   * See the License for the specific language governing permissions and
    16   * limitations under the License.
    17   */
    18  
    19  package operatorerrors
    20  
    21  import (
    22  	"fmt"
    23  
    24  	"github.com/go-logr/logr"
    25  )
    26  
    27  const (
    28  	InvalidDeploymentCreateRequest     = 1
    29  	InvalidDeploymentUpdateRequest     = 2
    30  	InvalidServiceCreateRequest        = 3
    31  	InvalidServiceUpdateRequest        = 4
    32  	InvalidPVCCreateRequest            = 5
    33  	InvalidPVCUpdateRequest            = 6
    34  	InvalidConfigMapCreateRequest      = 7
    35  	InvalidConfigMapUpdateRequest      = 8
    36  	InvalidServiceAccountCreateRequest = 9
    37  	InvalidServiceAccountUpdateRequest = 10
    38  	InvalidRoleCreateRequest           = 11
    39  	InvalidRoleUpdateRequest           = 12
    40  	InvalidRoleBindingCreateRequest    = 13
    41  	InvalidRoleBindingUpdateRequest    = 14
    42  	InvalidPeerInitSpec                = 15
    43  	InvalidOrdererType                 = 16
    44  	InvalidOrdererNodeCreateRequest    = 17
    45  	InvalidOrdererNodeUpdateRequest    = 18
    46  	InvalidOrdererInitSpec             = 19
    47  	CAInitilizationFailed              = 20
    48  	OrdererInitilizationFailed         = 21
    49  	PeerInitilizationFailed            = 22
    50  	MigrationFailed                    = 23
    51  	FabricPeerMigrationFailed          = 24
    52  	FabricOrdererMigrationFailed       = 25
    53  	InvalidCustomResourceCreateRequest = 26
    54  	FabricCAMigrationFailed            = 27
    55  )
    56  
    57  var (
    58  	BreakingErrors = map[int]*struct{}{
    59  		InvalidDeploymentCreateRequest:     nil,
    60  		InvalidDeploymentUpdateRequest:     nil,
    61  		InvalidServiceCreateRequest:        nil,
    62  		InvalidServiceUpdateRequest:        nil,
    63  		InvalidPVCCreateRequest:            nil,
    64  		InvalidPVCUpdateRequest:            nil,
    65  		InvalidConfigMapCreateRequest:      nil,
    66  		InvalidConfigMapUpdateRequest:      nil,
    67  		InvalidServiceAccountCreateRequest: nil,
    68  		InvalidServiceAccountUpdateRequest: nil,
    69  		InvalidRoleCreateRequest:           nil,
    70  		InvalidRoleUpdateRequest:           nil,
    71  		InvalidRoleBindingCreateRequest:    nil,
    72  		InvalidRoleBindingUpdateRequest:    nil,
    73  		InvalidPeerInitSpec:                nil,
    74  		InvalidOrdererType:                 nil,
    75  		InvalidOrdererInitSpec:             nil,
    76  		CAInitilizationFailed:              nil,
    77  		OrdererInitilizationFailed:         nil,
    78  		PeerInitilizationFailed:            nil,
    79  		FabricPeerMigrationFailed:          nil,
    80  		FabricOrdererMigrationFailed:       nil,
    81  		InvalidCustomResourceCreateRequest: nil,
    82  	}
    83  )
    84  
    85  type OperatorError struct {
    86  	Code    int
    87  	Message string
    88  }
    89  
    90  func (e *OperatorError) Error() string {
    91  	return e.String()
    92  }
    93  
    94  func (e *OperatorError) String() string {
    95  	return fmt.Sprintf("Code: %d - %s", e.Code, e.Message)
    96  }
    97  
    98  func New(code int, msg string) *OperatorError {
    99  	return &OperatorError{
   100  		Code:    code,
   101  		Message: msg,
   102  	}
   103  }
   104  
   105  func Wrap(err error, code int, msg string) *OperatorError {
   106  	return &OperatorError{
   107  		Code:    code,
   108  		Message: fmt.Sprintf("%s: %s", msg, err.Error()),
   109  	}
   110  }
   111  
   112  func IsBreakingError(err error, msg string, log logr.Logger) error {
   113  	oerr := IsOperatorError(err)
   114  	if oerr == nil {
   115  		return err
   116  	}
   117  	_, breakingError := BreakingErrors[oerr.Code]
   118  	if breakingError {
   119  		if log != nil {
   120  			log.Error(err, fmt.Sprintf("Breaking Error: %s", msg))
   121  		}
   122  		return nil
   123  	}
   124  	return err
   125  }
   126  
   127  func GetErrorCode(err error) int {
   128  	oerr := IsOperatorError(err)
   129  	if oerr == nil {
   130  		return 0
   131  	}
   132  
   133  	return oerr.Code
   134  }
   135  
   136  type Causer interface {
   137  	Cause() error
   138  }
   139  
   140  // GetCause gets the root cause of the error
   141  func IsOperatorError(err error) *OperatorError {
   142  	for err != nil {
   143  		switch err.(type) {
   144  		case *OperatorError:
   145  			return err.(*OperatorError)
   146  		case Causer:
   147  			err = err.(Causer).Cause()
   148  		default:
   149  			return nil
   150  		}
   151  	}
   152  	return nil
   153  }