1 package errorsx 2 3 import "github.com/pkg/errors" 4 5 // Cause returns the underlying cause of the error, if possible. 6 // An error value has a cause if it implements the following 7 // interface: 8 // 9 // type causer interface { 10 // Cause() error 11 // } 12 // 13 // If the error does not implement Cause, the original error will 14 // be returned. If the error is nil, nil will be returned without further 15 // investigation. 16 func Cause(err error) error { 17 type causer interface { 18 Cause() error 19 } 20 21 for err != nil { 22 cause, ok := err.(causer) 23 if !ok || cause.Cause() == nil { 24 break 25 } 26 err = cause.Cause() 27 } 28 return err 29 } 30 31 // WithStack mirror pkg/errors.WithStack but does not wrap existing stack 32 // traces. 33 func WithStack(err error) error { 34 if _, ok := err.(StackTracer); ok { 35 return err 36 } 37 38 return errors.WithStack(err) 39 } 40 41 // StatusCodeCarrier can be implemented by an error to support setting status codes in the error itself. 42 type StatusCodeCarrier interface { 43 // StatusCode returns the status code of this error. 44 StatusCode() int 45 } 46 47 // RequestIDCarrier can be implemented by an error to support error contexts. 48 type RequestIDCarrier interface { 49 // RequestID returns the ID of the request that caused the error, if applicable. 50 RequestID() string 51 } 52 53 // ReasonCarrier can be implemented by an error to support error contexts. 54 type ReasonCarrier interface { 55 // Reason returns the reason for the error, if applicable. 56 Reason() string 57 } 58 59 // DebugCarrier can be implemented by an error to support error contexts. 60 type DebugCarrier interface { 61 // Debug returns debugging information for the error, if applicable. 62 Debug() string 63 } 64 65 // StatusCarrier can be implemented by an error to support error contexts. 66 type StatusCarrier interface { 67 // ID returns the error id, if applicable. 68 Status() string 69 } 70 71 // DetailsCarrier can be implemented by an error to support error contexts. 72 type DetailsCarrier interface { 73 // Details returns details on the error, if applicable. 74 Details() map[string]interface{} 75 } 76 77 type StackTracer interface { 78 StackTrace() errors.StackTrace 79 } 80