...

Source file src/github.com/GoogleCloudPlatform/k8s-config-connector/pkg/execution/builtin.go

Documentation: github.com/GoogleCloudPlatform/k8s-config-connector/pkg/execution

     1  // Copyright 2022 Google LLC
     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  package execution
    16  
    17  import (
    18  	goerrors "errors"
    19  	"fmt"
    20  	"runtime/debug"
    21  	"strings"
    22  
    23  	"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/errors"
    24  )
    25  
    26  // RecoverWithGenericError is a general purpose function for recovering from panics. A useful error is written to 'err'. See
    27  // RecoverWithInternalError for recovering inside a controller Reconcile loop
    28  func RecoverWithGenericError(err *error) {
    29  	if rec := recover(); rec != nil {
    30  		handleRecovery(err, rec, goerrors.New)
    31  	}
    32  }
    33  
    34  // Recovers with the 'err' value being filled in with an InternalError, used by controllers to enable metric collection
    35  func RecoverWithInternalError(err *error) {
    36  	newError := func(message string) error {
    37  		return errors.NewInternalError("panic", message)
    38  	}
    39  	if rec := recover(); rec != nil {
    40  		handleRecovery(err, rec, newError)
    41  	}
    42  }
    43  
    44  type NewErrorFunc func(message string) error
    45  
    46  func handleRecovery(err *error, rec interface{}, newError NewErrorFunc) {
    47  	stack := debug.Stack()
    48  	formattedStack := string(stack)
    49  	// remove the three functions from the stack trace:
    50  	//   - debug.Stack(...)
    51  	//   - execution.handleRecovery()
    52  	//   - calling function (i.e. one of the Recover functions in this package)
    53  	// each function gets two lines of output
    54  	for i := 0; i < 6; i++ {
    55  		formattedStack = safeRemoveFirstLine(formattedStack)
    56  	}
    57  	// indent all lines of the stack trace to make it easier to follow in a log
    58  	formattedStack = strings.ReplaceAll(formattedStack, "\n", "\n\t")
    59  	*err = newError(fmt.Sprintf("observed a panic: %+v\n%v", rec, formattedStack))
    60  }
    61  
    62  func safeRemoveFirstLine(value string) string {
    63  	idx := strings.Index(value, "\n")
    64  	if idx >= 0 {
    65  		if len(value) > idx+1 {
    66  			idx += 1
    67  		}
    68  		return value[idx:]
    69  	}
    70  	return value
    71  }
    72  

View as plain text