...

Source file src/golang.conradwood.net/go-easyops/common/common_errors.go

Documentation: golang.conradwood.net/go-easyops/common

     1  package common
     2  
     3  import (
     4  	"flag"
     5  	"fmt"
     6  )
     7  
     8  var (
     9  	wrapper                    func(error) error
    10  	new_error                  func(format string, args ...any) error
    11  	err2str                    func(error) string
    12  	err2strWithSingleLineStack func(error) string
    13  	err2strWithStackTrace      func(error) string
    14  	with_stack                 = flag.Bool("ge_err_with_stack", false, "if true print stacktrace on errors")
    15  )
    16  
    17  func Wrap(err error) error {
    18  	if wrapper == nil {
    19  		return err
    20  	}
    21  	return wrapper(err)
    22  }
    23  func Errorf(format string, args ...any) error {
    24  	if new_error == nil {
    25  		return fmt.Errorf(format, args...)
    26  	}
    27  	return new_error(format, args...)
    28  }
    29  func Err2Str(err error) string {
    30  	if err2str == nil {
    31  		return fmt.Sprintf("%s", err)
    32  	}
    33  	if *with_stack {
    34  		return err2strWithStackTrace(err)
    35  	}
    36  	return err2str(err)
    37  }
    38  func RegisterErr(f1, f2, f3 func(error) string, f4 func(error) error, f5 func(format string, args ...any) error) {
    39  	err2str = f1
    40  	err2strWithSingleLineStack = f2
    41  	err2strWithStackTrace = f3
    42  	wrapper = f4
    43  	new_error = f5
    44  }
    45  

View as plain text