github.com/advanderveer/restic@v0.8.1-0.20171209104529-42a8c19aaea6/internal/errors/fatal.go (about)

     1  package errors
     2  
     3  import "fmt"
     4  
     5  // fatalError is an error that should be printed to the user, then the program
     6  // should exit with an error code.
     7  type fatalError string
     8  
     9  func (e fatalError) Error() string {
    10  	return string(e)
    11  }
    12  
    13  func (e fatalError) Fatal() bool {
    14  	return true
    15  }
    16  
    17  // Fataler is an error which should be printed to the user directly.
    18  // Afterwards, the program should exit with an error.
    19  type Fataler interface {
    20  	Fatal() bool
    21  }
    22  
    23  // IsFatal returns true if err is a fatal message that should be printed to the
    24  // user. Then, the program should exit.
    25  func IsFatal(err error) bool {
    26  	e, ok := err.(Fataler)
    27  	return ok && e.Fatal()
    28  }
    29  
    30  // Fatal returns a wrapped error which implements the Fataler interface.
    31  func Fatal(s string) error {
    32  	return Wrap(fatalError(s), "Fatal")
    33  }
    34  
    35  // Fatalf returns an error which implements the Fataler interface.
    36  func Fatalf(s string, data ...interface{}) error {
    37  	return fatalError(fmt.Sprintf(s, data...))
    38  }