66 "strings"
77)
88
9- // An instance of error
10- // At the moment of creation, Error collects information based on context, creation modifiers and type it belongs to
11- // Error is mostly immutable, and distinct errors composition is achieved through wrap
9+ // Error is an instance of error object,
10+ // At the moment of creation, Error collects information based on context, creation modifiers and type it belongs to.
11+ // Error is mostly immutable, and distinct errors composition is achieved through wrap.
1212type Error struct {
1313 message string
1414 errorType * Type
@@ -21,10 +21,10 @@ type Error struct {
2121
2222var _ fmt.Formatter = (* Error )(nil )
2323
24- // Add a dynamic property to error instance
25- // If an error already contained another value for the same property, it is overwritten
26- // It is a caller's responsibility to accumulate and update a property, if needed
27- // Dynamic properties is a brittle mechanism and should therefore be used with care and in a simple and robust manner
24+ // WithProperty adds a dynamic property to error instance.
25+ // If an error already contained another value for the same property, it is overwritten.
26+ // It is a caller's responsibility to accumulate and update a property, if needed.
27+ // Dynamic properties is a brittle mechanism and should therefore be used with care and in a simple and robust manner.
2828func (e * Error ) WithProperty (key Property , value interface {}) * Error {
2929 if e .properties == nil {
3030 e .properties = make (map [Property ]interface {}, 1 )
@@ -34,9 +34,9 @@ func (e *Error) WithProperty(key Property, value interface{}) *Error {
3434 return e
3535}
3636
37- // Adds multiple additional related (hidden, suppressed) errors to be used exclusively in error output
38- // Note that these errors make no other effect whatsoever: their traits, types, properties etc. are lost on the observer
39- // Consider using errorx.DecorateMany instead
37+ // WithUnderlyingErrors adds multiple additional related (hidden, suppressed) errors to be used exclusively in error output.
38+ // Note that these errors make no other effect whatsoever: their traits, types, properties etc. are lost on the observer.
39+ // Consider using errorx.DecorateMany instead.
4040func (e * Error ) WithUnderlyingErrors (errs ... error ) * Error {
4141 for _ , err := range errs {
4242 if err == nil {
@@ -49,10 +49,10 @@ func (e *Error) WithUnderlyingErrors(errs ...error) *Error {
4949 return e
5050}
5151
52- // Extract a dynamic property value from an error
53- // A property may belong to this error, or be extracted from the original cause
52+ // Property extracts a dynamic property value from an error.
53+ // A property may belong to this error, or be extracted from the original cause.
5454// The transparency rules are respected to some extent: both the original cause and the transparent wrapper
55- // may have accessible properties, but an opaque wrapper hides the original properties
55+ // may have accessible properties, but an opaque wrapper hides the original properties.
5656func (e * Error ) Property (key Property ) (interface {}, bool ) {
5757 cause := e
5858 for cause != nil {
@@ -71,10 +71,10 @@ func (e *Error) Property(key Property) (interface{}, bool) {
7171 return nil , false
7272}
7373
74- // Check if an error possesses the expected trait
75- // Trait check works just as a type check would: opaque wrap hides the traits of the cause
76- // Traits are always a property of a type rather than of an instance, so trait check is an alternative to a type check
77- // This alternative is preferable, though, as it is less brittle and generally creates less of a dependency
74+ // HasTrait checks if an error possesses the expected trait.
75+ // Trait check works just as a type check would: opaque wrap hides the traits of the cause.
76+ // Traits are always a property of a type rather than of an instance, so trait check is an alternative to a type check.
77+ // This alternative is preferable, though, as it is less brittle and generally creates less of a dependency.
7878func (e * Error ) HasTrait (key Trait ) bool {
7979 cause := e
8080 for cause != nil {
@@ -89,9 +89,9 @@ func (e *Error) HasTrait(key Trait) bool {
8989 return false
9090}
9191
92- // A proper type check for an error
92+ // IsOfType is a proper type check for an error.
9393// It takes the transparency and error types hierarchy into account,
94- // so that type check against any supertype of the original cause passes
94+ // so that type check against any supertype of the original cause passes.
9595func (e * Error ) IsOfType (t * Type ) bool {
9696 cause := e
9797 for cause != nil {
@@ -105,12 +105,12 @@ func (e *Error) IsOfType(t *Type) bool {
105105 return false
106106}
107107
108- // Returns the exact type of this error
109- // With transparent wrapping, such as in Decorate(), returns the type of the original cause
110- // The result is always not nil, even if the resulting type is impossible to successfully type check against
111- // NB: the exact error type may fail an equality check where a IsOfType() check would succeed
112- // This may happen if a type is checked against one of its supertypes, for example
113- // Therefore, handle direct type checks with care or avoid it altogether and use TypeSwitch() or IsForType() instead
108+ // Type returns the exact type of this error.
109+ // With transparent wrapping, such as in Decorate(), returns the type of the original cause.
110+ // The result is always not nil, even if the resulting type is impossible to successfully type check against.
111+ // NB: the exact error type may fail an equality check where a IsOfType() check would succeed.
112+ // This may happen if a type is checked against one of its supertypes, for example.
113+ // Therefore, handle direct type checks with care or avoid it altogether and use TypeSwitch() or IsForType() instead.
114114func (e * Error ) Type () * Type {
115115 cause := e
116116 for cause != nil {
@@ -124,27 +124,27 @@ func (e *Error) Type() *Type {
124124 return foreignType
125125}
126126
127- // Returns a message of this particular error, disregarding the cause
128- // The result of this method, like a result of an Error() method, should never be used to infer the meaning of an error
129- // In most cases, message is only used as a part of formatting to print error contents into a log file
130- // Manual extraction may be required, however, to transform an error into another format - say, API response
127+ // Message returns a message of this particular error, disregarding the cause.
128+ // The result of this method, like a result of an Error() method, should never be used to infer the meaning of an error.
129+ // In most cases, message is only used as a part of formatting to print error contents into a log file.
130+ // Manual extraction may be required, however, to transform an error into another format - say, API response.
131131func (e * Error ) Message () string {
132132 return e .message
133133}
134134
135- // Returns the immediate (wrapped) cause of current error
136- // This method could be used to dig for root cause of the error, but it is not advised to do so
137- // Errors should not require a complex navigation through causes to be properly handled, and the need to do so is a code smell
138- // Manually extracting cause defeats features such as properties of wrap, behaviour of properties etc
139- // This method is, therefore, reserved for system utilities, not for general use
135+ // Cause returns the immediate (wrapped) cause of current error.
136+ // This method could be used to dig for root cause of the error, but it is not advised to do so.
137+ // Errors should not require a complex navigation through causes to be properly handled, and the need to do so is a code smell.
138+ // Manually extracting cause defeats features such as properties of wrap, behaviour of properties etc.
139+ // This method is, therefore, reserved for system utilities, not for general use.
140140func (e * Error ) Cause () error {
141141 return e .cause
142142}
143143
144- // Implements the Formatter interface
145- // Supports %v or %s for simple message output, and %+v for full output complete with a stack trace
146- // In is nearly always preferable to use %+v format
147- // If a stack trace is not required, it should be omitted at the moment of creation rather in formatting
144+ // Format implements the Formatter interface.
145+ // Supports %v or %s for simple message output, and %+v for full output complete with a stack trace.
146+ // In is nearly always preferable to use %+v format.
147+ // If a stack trace is not required, it should be omitted at the moment of creation rather in formatting.
148148func (e * Error ) Format (s fmt.State , verb rune ) {
149149 message := e .fullMessage ()
150150 switch verb {
@@ -158,8 +158,8 @@ func (e *Error) Format(s fmt.State, verb rune) {
158158 }
159159}
160160
161- // Implements the error interface
162- // A result is the same as with %s formatter and does not contain a stack trace
161+ // Error implements the error interface.
162+ // A result is the same as with %s formatter and does not contain a stack trace.
163163func (e * Error ) Error () string {
164164 return e .fullMessage ()
165165}
0 commit comments