github.com/tobgu/qframe@v0.4.0/qerrors/error.go (about)

     1  package qerrors
     2  
     3  import "fmt"
     4  
     5  // Error holds data identifying an error that occurred
     6  // while executing a qframe operation.
     7  type Error struct {
     8  	source    error
     9  	operation string
    10  	reason    string
    11  }
    12  
    13  // Error returns a string representation of the error.
    14  func (e Error) Error() string {
    15  	result := e.operation
    16  	if e.reason != "" {
    17  		result += ": " + e.reason
    18  	}
    19  
    20  	if e.source != nil {
    21  		result += fmt.Sprintf(" (%s)", e.source)
    22  	}
    23  
    24  	return result
    25  }
    26  
    27  // New creates a new error instance.
    28  func New(operation, reason string, params ...interface{}) Error {
    29  	return Error{operation: operation, reason: fmt.Sprintf(reason, params...)}
    30  }
    31  
    32  // Propagate propagates an existing error with added context.
    33  func Propagate(operation string, err error) Error {
    34  	return Error{operation: operation, source: err}
    35  }
    36  
    37  // Error types:
    38  //   - Type error
    39  //   - Input error (which would basically always be the case...)