github.com/argoproj/argo-cd@v1.8.7/util/errors/errors.go (about)

     1  package errors
     2  
     3  import (
     4  	"os"
     5  
     6  	log "github.com/sirupsen/logrus"
     7  )
     8  
     9  const (
    10  	// ErrorCommandSpecific is reserved for command specific indications
    11  	ErrorCommandSpecific = 1
    12  	// ErrorConnectionFailure is returned on connection failure to API endpoint
    13  	ErrorConnectionFailure = 11
    14  	// ErrorAPIResponse is returned on unexpected API response, i.e. authorization failure
    15  	ErrorAPIResponse = 12
    16  	// ErrorResourceDoesNotExist is returned when the requested resource does not exist
    17  	ErrorResourceDoesNotExist = 13
    18  	// ErrorGeneric is returned for generic error
    19  	ErrorGeneric = 20
    20  )
    21  
    22  // CheckError logs a fatal message and exits with ErrorGeneric if err is not nil
    23  func CheckError(err error) {
    24  	if err != nil {
    25  		Fatal(ErrorGeneric, err)
    26  	}
    27  }
    28  
    29  // CheckErrorWithCode is a convenience function to exit if an error is non-nil and exit if it was
    30  func CheckErrorWithCode(err error, exitcode int) {
    31  	if err != nil {
    32  		Fatal(exitcode, err)
    33  	}
    34  }
    35  
    36  // FailOnErr panics if there is an error. It returns the first value so you can use it if you cast it:
    37  // text := FailOrErr(Foo)).(string)
    38  func FailOnErr(v interface{}, err error) interface{} {
    39  	CheckError(err)
    40  	return v
    41  }
    42  
    43  // Fatal is a wrapper for logrus.Fatal() to exit with custom code
    44  func Fatal(exitcode int, args ...interface{}) {
    45  	exitfunc := func() {
    46  		os.Exit(exitcode)
    47  	}
    48  	log.RegisterExitHandler(exitfunc)
    49  	log.Fatal(args...)
    50  }
    51  
    52  // Fatalf is a wrapper for logrus.Fatalf() to exit with custom code
    53  func Fatalf(exitcode int, format string, args ...interface{}) {
    54  	exitfunc := func() {
    55  		os.Exit(exitcode)
    56  	}
    57  	log.RegisterExitHandler(exitfunc)
    58  	log.Fatalf(format, args...)
    59  }