github.com/alloyzeus/go-azfl@v0.0.0-20231220071816-9740126a2d07/azerrs/wrap.go (about) 1 package errors 2 3 // Unwrappable is an error which holds inner error, which usually 4 // provides the details of this error. 5 type Unwrappable interface { 6 error 7 Unwrap() error 8 } 9 10 // Wrap creates a new error by providing context message to another error. 11 // It's recommended for the message to describe what the program did which 12 // caused the error. 13 // 14 // err := fetch("head") 15 // if err != nil { return errors.Wrap("fetching head", err) } 16 // 17 // err = fetch("body") 18 // if err != nil { return errors.Wrap("fetching body", err) } 19 // 20 func Wrap(contextMessage string, detailingError error) error { 21 return &errorWrap{contextMessage, detailingError} 22 } 23 24 var _ Unwrappable = &errorWrap{} 25 26 type errorWrap struct { 27 msg string 28 wrapped error 29 } 30 31 func (e *errorWrap) Error() string { 32 if e.msg != "" { 33 if e.wrapped != nil { 34 return e.msg + ": " + e.wrapped.Error() 35 } 36 return e.msg 37 } 38 if e.wrapped != nil { 39 return e.wrapped.Error() 40 } 41 return "" 42 } 43 44 func (e *errorWrap) Unwrap() error { 45 return e.wrapped 46 }