@@ -8,17 +8,20 @@ import (
88 "dev.gaijin.team/go/golib/fields"
99)
1010
11+ // Err represents a custom error type that supports error chaining and structured metadata fields.
1112type Err struct {
1213 errs []error
1314 fields fields.List
1415}
1516
16- // New creates new instance of Err.
17+ // New returns a new Err with the given reason and optional fields.
18+ // The returned error can be further wrapped or annotated with additional fields.
1719func New (reason string , f ... fields.Field ) * Err {
1820 return From (errors .New (reason ), f ... ) //nolint:err113
1921}
2022
21- // NewFrom creates new instance of Err that wraps origin error.
23+ // NewFrom returns a new Err with the given reason, wrapping the provided error, and optional fields.
24+ // If wrapped is nil, it behaves like New.
2225func NewFrom (reason string , wrapped error , f ... fields.Field ) * Err {
2326 if wrapped == nil {
2427 return New (reason , f ... )
@@ -30,9 +33,8 @@ func NewFrom(reason string, wrapped error, f ...fields.Field) *Err {
3033 }
3134}
3235
33- // From transforms existing error to Err. It is not wrapping operation - unwrap
34- // will not return origin error. Passing nil to this function will result with
35- // empty error.
36+ // From converts any error to an Err, optionally adding fields. This is not true wrapping;
37+ // unwrapping will not return the original error. Passing nil results in an Err with reason "error(nil)".
3638func From (origin error , f ... fields.Field ) * Err {
3739 if origin == nil {
3840 origin = errors .New ("error(nil)" ) //nolint:err113
@@ -44,11 +46,8 @@ func From(origin error, f ...fields.Field) *Err {
4446 }
4547}
4648
47- // Wrap creates new instance of Err that wraps provided error with source one.
48- //
49- // Example:
50- //
51- // e.New("e1").Wrap(errors.New("e2")) // e1: e2
49+ // Wrap returns a new Err that wraps the provided error with the current Err as context.
50+ // Additional fields can be attached. If err is nil, it is replaced with an Err for "error(nil)".
5251func (e * Err ) Wrap (err error , f ... fields.Field ) * Err {
5352 if err == nil {
5453 err = errors .New ("error(nil)" ) //nolint:err113
@@ -60,7 +59,7 @@ func (e *Err) Wrap(err error, f ...fields.Field) *Err {
6059 }
6160}
6261
63- // Error returns string representation of the error .
62+ // Error returns the string representation of the Err, including reason, fields, and wrapped errors .
6463func (e * Err ) Error () string {
6564 b := & strings.Builder {}
6665 writeTo (b , e )
@@ -96,31 +95,31 @@ func writeTo(b *strings.Builder, err error) {
9695 }
9796}
9897
99- // Clone creates a new instance of Err with the same error, wrapped error, and
100- // cloned fields container.
98+ // Clone returns a new Err with the same error, wrapped error, and a cloned fields container.
10199func (e * Err ) Clone () * Err {
102100 return & Err {
103101 errs : slices .Clone (e .errs ),
104102 fields : slices .Clone (e .fields ),
105103 }
106104}
107105
108- // WithFields creates new error with source error as origin and provided fields as its fields.
106+ // WithFields returns a new Err with the same error and the provided fields.
109107func (e * Err ) WithFields (f ... fields.Field ) * Err {
110108 return From (e , f ... )
111109}
112110
113- // WithField alike [ Err.WithFields], but creates an error with single field added .
111+ // WithField returns a new Err with the same error and a single additional field .
114112func (e * Err ) WithField (key string , val any ) * Err {
115113 return e .WithFields (fields .F (key , val ))
116114}
117115
118- // Fields returns fields of the error .
116+ // Fields returns the metadata fields attached to the Err .
119117func (e * Err ) Fields () fields.List {
120118 return e .fields
121119}
122120
123- // Reason returns reason string of the error without fields and wrapped errors.
121+ // Reason returns the reason string of the Err, without fields or wrapped errors.
122+ // If the Err is nil, returns "(*e.Err)(nil)". If empty, returns "(*e.Err)(empty)".
124123func (e * Err ) Reason () string {
125124 if e == nil {
126125 return "(*e.Err)(nil)"
@@ -133,8 +132,9 @@ func (e *Err) Reason() string {
133132 return e .errs [0 ].Error ()
134133}
135134
136- // Unwrap implemented only for purposes of compatibility with [errors.Is] and
137- // [errors.As] methods.
135+ // Unwrap returns the underlying errors for compatibility with errors.Is and errors.As.
136+ //
137+ // Deprecated: This method is for internal use only. Prefer using the errors package directly.
138138func (e * Err ) Unwrap () []error {
139139 return e .errs
140140}
0 commit comments