github.com/argoproj/argo-cd/v3@v3.2.1/util/errors/errors.go (about) 1 package errors 2 3 import ( 4 "os" 5 "testing" 6 7 log "github.com/sirupsen/logrus" 8 "github.com/stretchr/testify/require" 9 ) 10 11 const ( 12 // ErrorGeneric is returned for generic error 13 ErrorGeneric = 20 14 ) 15 16 type Handler struct { 17 t *testing.T 18 } 19 20 func NewHandler(t *testing.T) *Handler { 21 t.Helper() 22 return &Handler{t: t} 23 } 24 25 // FailOnErr fails the test if there is an error. It returns the first value so you can use it if you cast it: 26 // text := FailOrErr(Foo).(string) 27 func (h *Handler) FailOnErr(v any, err error) any { 28 h.t.Helper() 29 require.NoError(h.t, err) 30 return v 31 } 32 33 // CheckError logs a fatal message and exits with ErrorGeneric if err is not nil 34 func CheckError(err error) { 35 if err != nil { 36 Fatal(ErrorGeneric, err) 37 } 38 } 39 40 // Fatal is a wrapper for logrus.Fatal() to exit with custom code 41 func Fatal(exitcode int, args ...any) { 42 exitfunc := func() { 43 os.Exit(exitcode) 44 } 45 log.RegisterExitHandler(exitfunc) 46 log.Fatal(args...) 47 } 48 49 // Fatalf is a wrapper for logrus.Fatalf() to exit with custom code 50 func Fatalf(exitcode int, format string, args ...any) { 51 exitfunc := func() { 52 os.Exit(exitcode) 53 } 54 log.RegisterExitHandler(exitfunc) 55 log.Fatalf(format, args...) 56 }