github.com/Benchkram/bob@v0.0.0-20220321080157-7c8f3876e225/pkg/usererror/error.go (about)

     1  package usererror
     2  
     3  import (
     4  	"fmt"
     5  )
     6  
     7  // Err is a instatiation of E
     8  // to be used for comparison with `errors.As(usererror.Err)`
     9  // DO NOT WRITE TO Err!!
    10  var Err = &E{}
    11  
    12  type E struct {
    13  	// err is the underlying error
    14  	err error
    15  
    16  	// msg contains a useful general message usualy shown to a user,
    17  	// can be colored.
    18  	msg string
    19  
    20  	// summary of a underlying error.
    21  	// Could be the last 5 lines of a build error.
    22  	// For future use!
    23  	summary string // nolint:structcheck, unused
    24  }
    25  
    26  func (e *E) Error() string {
    27  	if e.msg == "" {
    28  		return e.err.Error()
    29  	}
    30  
    31  	return fmt.Sprintf("%s: %s", e.msg, e.err)
    32  }
    33  
    34  func (e *E) Msg() string {
    35  	return e.msg
    36  }
    37  
    38  func (e *E) Unwrap() error {
    39  	return e.err
    40  }
    41  
    42  // Wrap an existing error and annotate it with the user error type
    43  // which is intended to be shown to the user on a cli.
    44  func Wrap(err error) *E {
    45  	return &E{err: err}
    46  }
    47  
    48  func Wrapm(err error, msg string) *E {
    49  	return &E{err: err, msg: msg}
    50  }