github.com/authzed/spicedb@v1.32.1-0.20240520085336-ebda56537386/pkg/spiceerrors/bug.go (about)

     1  package spiceerrors
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"strings"
     7  
     8  	"github.com/go-errors/errors"
     9  )
    10  
    11  // IsInTests returns true if go test is running
    12  // Based on: https://stackoverflow.com/a/58945030
    13  func IsInTests() bool {
    14  	for _, arg := range os.Args {
    15  		if strings.HasPrefix(arg, "-test.") {
    16  			return true
    17  		}
    18  	}
    19  	return false
    20  }
    21  
    22  // MustPanic is a special function for panicing when necessary to violate the linter.
    23  func MustPanic(format string, args ...any) {
    24  	panic(fmt.Sprintf(format, args...))
    25  }
    26  
    27  // MustBugf returns an error representing a bug in the system. Will panic if run under testing.
    28  func MustBugf(format string, args ...any) error {
    29  	if IsInTests() {
    30  		panic(fmt.Sprintf(format, args...))
    31  	}
    32  
    33  	e := errors.Errorf(format, args...)
    34  	return fmt.Errorf("BUG: %s", e.ErrorStack())
    35  }