Skip to content

fix func Cause(error) error returning nil on the root cause error - #221

Closed
win-t wants to merge 1 commit into
pkg:masterfrom
win-t:fix-causer-return-nil
Closed

fix func Cause(error) error returning nil on the root cause error#221
win-t wants to merge 1 commit into
pkg:masterfrom
win-t:fix-causer-return-nil

Conversation

@win-t

Copy link
Copy Markdown

Some error struct have Cause() error method indicating the cause of
the error, but that method could return nil indicating that that itself
is the root cause in the error chain

look at this struct

typemyErrorstruct {
messagestringcauseerror
}
func (m*myError) Error() string { ... }
func (m*myError) Cause() error {
returnm.cause
}
funcNew(messagestring) error {
return&myError{message: message}
}

Some error struct have `Cause() error` method indicating the cause of
the error, but that method could return nil indicating that that itself
is the root cause in the error chain
look at this struct
```go
type myError struct {
message string
cause error
}
func (m *myError) Error() string { ... }
func (m *myError) Cause() error {
return m.cause
}
func New(message string) error {
return &myError{message: message}
}
```
@win-t
win-t requested a review from aperezgJanuary 15, 2020 08:21
@davecheney

davecheney commented Jan 15, 2020 via email

Copy link
Copy Markdown
Member

Comment threaderrors.go
break
}
err = cause.Cause()
if cerr := cause.Cause(); cerr != nil {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can avoid the spurious break control flow by structuring this the other way around:

cerr := cause.Cause()
if cerr == nil {
return err
}
err = cerr

@puellanivis

Copy link
Copy Markdown

Also, this is a duplicate of #143

@win-twin-t closed this Nov 25, 2021
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

@win-t@davecheney@puellanivis