github.com/mailgun/holster/v4@v4.20.0/errors/typederror.go (about)

     1  package errors
     2  
     3  // TypedError is used to decorate an error with classification metadata.
     4  // Contains error class and type decoration.
     5  type TypedError struct {
     6  	error
     7  	cls string
     8  	typ string
     9  }
    10  
    11  // NewWithType returns an error decorated with error class and type.
    12  // Best practice: use public constants for class and type.
    13  func NewWithType(msg, cls, typ string) *TypedError {
    14  	return &TypedError{
    15  		error: New(msg),
    16  		cls:   cls,
    17  		typ:   typ,
    18  	}
    19  }
    20  
    21  // WrapWithType returns a wrapped error decorated with class and type.
    22  // Class is a category of error types.
    23  // Type is the specific error condition.
    24  // Best practice: use public constants for class and type.
    25  func WrapWithType(err error, cls, typ string) *TypedError {
    26  	return &TypedError{
    27  		error: err,
    28  		cls:   cls,
    29  		typ:   typ,
    30  	}
    31  }
    32  
    33  func (e *TypedError) Class() string {
    34  	return e.cls
    35  }
    36  
    37  func (e *TypedError) Type() string {
    38  	return e.typ
    39  }