gopkg.in/hedzr/errors.v3@v3.3.1/def.go (about) 1 package errors 2 3 // Error object 4 type Error interface { 5 // Buildable _ 6 Buildable 7 8 // Data returns the wrapped common user data by Buildable.WithData. 9 // 10 // The error objects with a passed Buildable.WithData will be moved 11 // into inner errors set, so its are excluded from Data(). 12 Data() []interface{} //nolint:revive 13 // TaggedData returns the wrapped tagged user data by 14 // Buildable.WithTaggedData. 15 TaggedData() TaggedData 16 // Cause returns the underlying cause of the error, if possible. 17 // An error value has a cause if it implements the following 18 // interface: 19 // 20 // type causer interface { 21 // Cause() error 22 // } 23 // 24 // If an error object does not implement Cause interface, the 25 // original error object will be returned. 26 // If the error is nil, nil will be returned without further 27 // investigation. 28 Cause() error 29 // Causes simply returns the wrapped inner errors. 30 // It doesn't consider an wrapped Code entity is an inner error too. 31 // So if you wanna to extract any inner error objects, use 32 // errors.Unwrap for instead. The errors.Unwrap could extract all 33 // of them one by one: 34 // 35 // var err = errors.New("hello").WithErrors(io.EOF, io.ShortBuffers) 36 // var e error = err 37 // for e != nil { 38 // e = errors.Unwrap(err) 39 // } 40 // 41 Causes() []error 42 } 43 44 // Buildable provides a fluent calling interface to make error building easy. 45 // Buildable is an error interface too. 46 type Buildable interface { 47 // error interface 48 error 49 50 // WithSkip specifies a special number of stack frames that will be ignored. 51 WithSkip(skip int) Buildable 52 // WithMessage formats the error message 53 WithMessage(message string, args ...interface{}) Buildable //nolint:revive 54 // WithCode specifies an error code. 55 // An error code `Code` is a integer number with error interface 56 // supported. 57 WithCode(code Code) Buildable 58 // WithErrors attaches the given errs as inner errors. 59 // 60 // WithErrors is like our old Attach(). 61 // 62 // It wraps the inner errors into underlying container and 63 // represents them all in a singular up-level error object. 64 // The wrapped inner errors can be retrieved with errors.Causes: 65 // 66 // var err = errors.New("hello").WithErrors(io.EOF, io.ShortBuffers) 67 // var errs []error = errors.Causes(err) 68 // 69 // Or, use As() to extract its: 70 // 71 // var errs []error 72 // errors.As(err, &errs) 73 // 74 // Or, use Unwrap() for its: 75 // 76 // var e error = err 77 // for e != nil { 78 // e = errors.Unwrap(err) 79 // } 80 // 81 // WithErrors attach child errors into an error container. 82 // For a container which has IsEmpty() interface, it would not be 83 // attached if it is empty (i.e. no errors). 84 // For a nil error object, it will be ignored. 85 WithErrors(errs ...error) Buildable 86 // WithData appends errs if the general object is a error object. 87 // 88 // StackTrace of errs will be copied to callee so that you can get a 89 // trace output nearer by the last error. 90 // 91 // defer-recover block typically is a better place of WithData(). 92 // 93 // For example: 94 // 95 // defer func() { 96 // if e := recover(); e != nil { 97 // err = errors.New("[recovered] copyTo unsatisfied ([%v] %v -> [%v] %v), causes: %v", 98 // c.indirectType(from.Type()), from, c.indirectType(to.Type()), to, e). 99 // WithData(e) // StackTrace of e -> err 100 // n := log.CalcStackFrames(1) // skip defer-recover frame at first 101 // log.Skip(n).Errorf("%v", err) // skip go-lib frames and defer-recover frame, back to the point throwing panic 102 // } 103 // }() 104 // 105 WithData(errs ...interface{}) Buildable //nolint:revive 106 // WithTaggedData appends user data with tag into internal container. 107 // These data can be retrieved by 108 WithTaggedData(siteScenes TaggedData) Buildable 109 // WithCause sets the underlying error manually if necessary. 110 WithCause(cause error) Buildable 111 112 // WithMaxObjectStringLength set limitation for object stringify length. 113 // 114 // The objects of Data/TaggedData will be limited while its' been formatted with "%+v" 115 WithMaxObjectStringLength(maxlen int) Buildable 116 117 // End could terminate the with-build stream calls without any 118 // returned value. 119 End() 120 121 // Container _ 122 Container 123 124 // FormatWith create a new error based on an exists error template 125 // with the given live args, the new error instance will be formatted. 126 // 127 // While you New an Error with format template without supplying 128 // the args at same time, you'll create an error message template. 129 // You could feed the live args later. 130 // A sample is: 131 // 132 // errTmpl := errors.New("template here: %v") 133 // // ... 134 // err = errTmpl.FormatWith("good day") 135 // println(err) // got: template here: good day 136 // err = errTmpl.FormatWith("bye") 137 // println(err) // got: template here: bye 138 // 139 FormatWith(args ...interface{}) error //nolint:revive 140 } 141 142 type causer interface { 143 // Cause returns the underlying cause of the error, if possible. 144 // An error value has a cause if it implements the following 145 // interface: 146 // 147 // type causer interface { 148 // Cause() error 149 // } 150 // 151 // If an error object does not implement Cause interface, the 152 // original error object will be returned. 153 // If the error is nil, nil will be returned without further 154 // investigation. 155 Cause() error 156 } 157 158 // causers is a tool interface. In your scene, use errors.Causes(err) 159 // to extract the inner errors. Or, use As(): 160 // 161 // err := New("many inner errors").WithErrors(e1,e2,e3) 162 // var errs []error 163 // errors.As(err, &errs) 164 // errs = errors.Causes(err) 165 // 166 // You may extract the inner errors one by one: 167 // 168 // var e error = err 169 // for e != nil { 170 // e = errors.Unwrap(err) 171 // } 172 type causers interface { 173 // Causes _ 174 Causes() []error 175 } 176 177 // Container represents an error container which can hold a group 178 // of inner errors. 179 type Container interface { 180 // IsEmpty returns true if Error is an error container which holded any inner errors. 181 // 182 // IsEmpty tests if it has any attached errors 183 IsEmpty() bool 184 // Defer can be used as a defer function to simplify your codes. 185 // 186 // The codes: 187 // 188 // func some(){ 189 // // as a inner errors container 190 // child := func() (err error) { 191 // errContainer := errors.New("") 192 // defer errContainer.Defer(&err) 193 // 194 // for _, r := range []error{io.EOF, io.ErrClosedPipe, errors.Internal} { 195 // errContainer.Attach(r) 196 // } 197 // 198 // return 199 // } 200 // 201 // err := child() 202 // t.Logf("failed: %+v", err) 203 // } 204 // 205 Defer(err *error) 206 Clear() Container // clear all nested errors, internal states 207 // Attachable _ 208 Attachable 209 } 210 211 // Attachable _ 212 type Attachable interface { 213 // Attach collects the errors except it's nil 214 // 215 // StackTrace of errs will be copied to callee so that you can 216 // get a trace output nearer by the last error. 217 // 218 Attach(errs ...error) 219 } 220 221 // TaggedData _ 222 type TaggedData map[string]interface{} //nolint:revive