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

feat ExistStack: Add a public method to determine if the error has been appended to the stack. #239

Closed
wants to merge 6 commits into from

Conversation

wbflooksky
Copy link

non-intrusive:
Add a public method to determine if the error has been appended to the stack.
Avoid adding the stack repeatedly for errors.

non-intrusive:
Add a public method to determine if the error has been appended to the stack.
Avoid adding the stack repeatedly for errors.
@wbflooksky
Copy link
Author

fix: #210

@wbflooksky
Copy link
Author

fix: #102

errors.go Outdated
if _, ok := err.(causer); !ok {
return false
}
return ExistStack(err.Cause())

Choose a reason for hiding this comment

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

Here, you attempt to call the receiver method Cause on a thing of type error, which does not expose that implementation.

Comment on lines +118 to +120
type causer interface {
Cause() error
}

Choose a reason for hiding this comment

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

Just checking Cause() error ignores the possibility of an Unwrap() error which is kind of the preferred way of unwrapping errors now. So, this would only work with errors from this package, and those designed specifically for pre-unwrap compatibility with this package, which is no longer a strong argument since go1.13.

errors.go Outdated
if _, ok := err.(causer); !ok {
return false
}
return ExistStack(err.Cause())

Choose a reason for hiding this comment

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

🤔 Not sure if the Go compiler is smart enough to rework end-recursion into the more efficient for-loop.

@wbflooksky
Copy link
Author

@puellanivis already fix

if value, ok := err.(causer); ok {
return ExistStack(err.Cause())
} else {
return false

Choose a reason for hiding this comment

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

Every code path in the preceding if … { … } block returns from the function, therefore there is no reason to use an else block.

Considering using:

cause, ok := err.(causer)
if !ok {
  return false
}
return ExistStack(cause.Cause())

Please check the implementation of Cause(err error) below, and try and mirror that as much as possible.

Copy link
Author

Choose a reason for hiding this comment

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

Thank you very much for your help

want bool
}{
{io.EOF, false},
{Wrap(io.EOF, "read error"), true},

Choose a reason for hiding this comment

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

Missing test cases: WithStack, WithMessage, and errors.New.

@davecheney
Copy link
Member

Thank you for this but I do not intend to merge it. I encourage you to fork the package to add additional methods. Thank you

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