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

Added errors.Last() #1

Merged
merged 1 commit into from
Feb 2, 2023
Merged

Added errors.Last() #1

merged 1 commit into from
Feb 2, 2023

Conversation

thrawn01
Copy link
Contributor

@thrawn01 thrawn01 commented Feb 1, 2023

Purpose

errors.As() find the first error in the chain that matches the target. This is sub optimal if you wrap multiple errors with errors.Wrap() as trying to find the original error with a stack trace will return the first error in the chain. So I wrote errors.Last() which returns the last error in the chain that matches the target.

func TestLast(t *testing.T) {
	err := errors.New("bottom")
	err = errors.Wrap(err, "last")
	err = errors.Wrap(err, "second")
	err = errors.Wrap(err, "first")
	err = fmt.Errorf("wrapped: %w", err)

	// errors.As() returns the "first" error in the chain with a stack trace
	var first callstack.HasStackTrace
	assert.True(t, errors.As(err, &first))
	assert.Equal(t, "first: second: last: bottom", first.(error).Error())

	// errors.Last() returns the last error in the chain with a stack trace
	var last callstack.HasStackTrace
	assert.True(t, errors.Last(err, &last))
	assert.Equal(t, "last: bottom", last.(error).Error())

	// If no stack trace is found, then should not set target and should return false
	assert.False(t, errors.Last(errors.New("no stack"), &last))
	assert.Equal(t, "last: bottom", last.(error).Error())
}

@Takumi2008
Copy link

Change looks good. I'm curious what would be the use case for wanting the last error that matches the target?

// NOTE: Last() is much slower than As(). Therefore As() should always be used
// unless you absolutely need Last() to retrieve the last error in the error chain
// that matches the target.
func Last(err error, target any) bool {
Copy link

Choose a reason for hiding this comment

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

The "last" and "first" concepts seem ambiguous. I interpretted "last error" as the last error in chronology. And the "first" being the first to occur.

Maybe: errors.Origin()?

Copy link
Member

Choose a reason for hiding this comment

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

Or RootCause both Last and First are confusing.

Copy link

Choose a reason for hiding this comment

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

Too late. He merged. 😭

@thrawn01 thrawn01 merged commit f75ac50 into main Feb 2, 2023
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.

4 participants