github.com/opentofu/opentofu@v1.7.1/internal/encryption/method/aesgcm/panic.go (about)

     1  package aesgcm
     2  
     3  import "fmt"
     4  
     5  // handlePanic runs the specified function and returns its result value or returned error. If a panic occurs, it returns the
     6  // panic as an error.
     7  func handlePanic(f func() ([]byte, error)) (result []byte, err error) {
     8  	result, e := func() ([]byte, error) {
     9  		defer func() {
    10  			var ok bool
    11  			e := recover()
    12  			if e == nil {
    13  				return
    14  			}
    15  			if err, ok = e.(error); !ok {
    16  				// In case the panic is not an error
    17  				err = fmt.Errorf("%v", e)
    18  			}
    19  		}()
    20  		return f()
    21  	}()
    22  	if err != nil {
    23  		return nil, err
    24  	}
    25  	return result, e
    26  }