Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Respect TypeModifierOmitStackTrace in Builder #43

Merged
merged 1 commit into from
Nov 8, 2024

Conversation

g7r
Copy link
Contributor

@g7r g7r commented Nov 7, 2024

Previous implementation of WithCause method didn't respect the TypeModifierOmitStackTrace modifier. A stack trace was erroneously collected if the error that was passed to WithCause didn't have stack trace.

@g7r g7r requested a review from PeterIvanov November 7, 2024 21:51
builder_test.go Outdated
err := NewErrorBuilder(et).
WithCause(testType.NewWithNoMessage()).
Create()
require.Nil(t, err.stackTrace)
Copy link
Collaborator

Choose a reason for hiding this comment

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

This particular case troubles me. I'm not entirely sure how the case of 'no-trace type is used to wrap whatever other error' is used.
On the one hand, such types are mostly used without wrapping other errors - so, if you do wrap, maybe that's exactly why you added the wrapping type, and you only use it this way. And you expect to avoid dragging any nested stack traces along.
On the other hand, such logic loses the original information. You already paid for its collection, and it may be a nasty surprise for user.

Maybe a better path would be not to enhance stack trace, but not to lose the existing one either?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

On the other hand, such logic loses the original information.

We don't lose anything here. The stack trace in cause is still there. Give me a moment, I'll verify it in tests.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It looks like I'm wrong and although technically we don't lose anything, fmt.Sprintf("%+v", err) is not going to print nested stack trace if err doesn't have it's own stack trace. It looks like a bug, IMO.

Copy link
Collaborator

Choose a reason for hiding this comment

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

OK, it may be my mistake in interpreting the test code. Thank you, explicitly testing that part as well would be perfect.

Copy link
Collaborator

@PeterIvanov PeterIvanov Nov 8, 2024

Choose a reason for hiding this comment

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

It looks like a bug, IMO.

It all depends on observable behaviour. Probably, until this point, this made sense.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Okay, I'll use less offensive words then 🙂
This behaviour doesn't seem correct taking into account the changes in the current PR. I should either fix my PR or fix the (*Error).Format method. Let me think a little bit...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@PeterIvanov, I've added a new test to ensure that ErrorBuilder keeps stack trace from cause even if error type explicitly requests stack trace omission.

I had to update testify to get access to Same assertion method.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I've got zero issues with your wording. My point was that the we should change that if it's needed now - but to consider other use cases possibly affected.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@PeterIvanov, please, check out new version. I've tried to address the concerns you've highlighted.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry for the force-push, I had to do it due to rebase. However, I've embraced it as the opportunity to glue all my commits into one 🙂

@g7r g7r force-pushed the always-respect-omit-stack-trace-in-builder branch from f276d20 to 902010f Compare November 8, 2024 12:08
builder.go Outdated
)

func (eb ErrorBuilder) assembleStackTrace() *stackTrace {
switch eb.mode {
case stackTraceCollect:
return eb.collectOriginalStackTrace()
case stackTraceBorrow:
return eb.borrowStackTraceFromCause()
case stackTraceBorrowOnly, stackTraceBorrowOrCollect:
Copy link
Collaborator

Choose a reason for hiding this comment

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

Very minor: perhaps making this into 2 separate cases with a little copy-paste but without another if eb.mode == would improve readability.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You're right.

Previous implementation of `WithCause` method didn't respect the
`TypeModifierOmitStackTrace` modifier. A stack trace was erroneously
collected if the error that was passed to `WithCause` didn't have
stack trace.

I had to upgrade `github.com/stretchr/testify` to a slightly more modern
version to be able to use `Same` assertion.
@g7r g7r force-pushed the always-respect-omit-stack-trace-in-builder branch from 902010f to b1898dc Compare November 8, 2024 12:44
@g7r g7r merged commit 3280086 into master Nov 8, 2024
13 checks passed
@g7r g7r deleted the always-respect-omit-stack-trace-in-builder branch November 8, 2024 12:45
Sign up for free to 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.

2 participants