Skip to content
This repository has been archived by the owner on Dec 1, 2021. It is now read-only.

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

Closed
wants to merge 1 commit into from

Conversation

win-t
Copy link

@win-t win-t commented Jan 15, 2020

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

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}
}

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 aperezg January 15, 2020 08:21
@davecheney
Copy link
Member

davecheney commented Jan 15, 2020 via email

@@ -282,7 +282,11 @@ func Cause(err error) error {
if !ok {
break
}
err = cause.Cause()
if cerr := cause.Cause(); cerr != nil {

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

Also, this is a duplicate of #143

@win-t win-t closed this Nov 25, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants