Skip to content

{types,graphql_test}: Adds Result.ErrorsJoined implementation. #717

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

Open
wants to merge 12 commits into
base: master
Choose a base branch
from

Conversation

chris-ramon
Copy link
Member

@chris-ramon chris-ramon commented Mar 28, 2025

Details

  • Depends on: .circleci: updates go version #718
  • types: adds Result.ErrorsJoined method.
  • graphql_test: adds Result.ErrorsJoined 100& test coverage unit tests.
  • .circleci/config.yml: upgrades circleci go versions.

Added in order to be able to join errors because currently Result.Errors returns an slice of errors and is common use case to have it consolidated into a single error.

This PR leverages the %w error formatting verb in order to wrap errors.
Ref: https://go.dev/doc/go1.13#error_wrapping

Test Plan

✔️ Tested that Result.ErrorsJoined works as expected via go test -cover ...:

go clean -testcache && go test -cover -coverprofile=coverage.out ./... && go tool cover -html=coverage.out -o coverage.html; sed -i'' -e's/black/whitesmoke/g' coverage.html && open coverage.html

Screenshot from 2025-03-28 16-11-33

@chris-ramon chris-ramon self-assigned this Mar 28, 2025
Copy link

mentatbot bot commented Mar 28, 2025

I'm getting to work. See my steps and track my cost here.

  • Wake on any new activity.
    I always see new activity while actively working. When paused, I'll only respond when tagged unless this box is checked.

Copy link

@mentatbot mentatbot bot left a comment

Choose a reason for hiding this comment

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

Thank you for submitting this PR! I've reviewed the changes and have some comments below.

types.go Outdated
Comment on lines 23 to 41
// ErrorsJoined joins and returns the result errors.
func (r *Result) ErrorsJoined() error {
if r.Errors == nil {
return nil
}

var result error
for _, err := range r.Errors {
if result == nil {
result = fmt.Errorf("%w", err)

continue
}

result = fmt.Errorf("%w: %w", err, result)
}

return result
}
Copy link

Choose a reason for hiding this comment

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

I noticed that the errors are being joined in reverse order (the first error in the slice ends up at the end of the error message). Is this intentional?

Also, it might be worth adding a bit more documentation about the behavior, particularly that:

  1. It returns nil if there are no errors
  2. The error wrapping preserves the original error types (so errors.Is would work with the wrapped errors)
  3. The formatting of the joined error (with : as separator)

Copy link
Member Author

Choose a reason for hiding this comment

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

I noticed that the errors are being joined in reverse order (the first error in the slice ends up at the end of the error message). Is this intentional?

Yes because we want to show the last error first, similar how stack traces work in order to be aware what happened with the program bottom-up.


Also, it might be worth adding a bit more documentation about the behavior, particularly that:

  1. It returns nil if there are no errors
  2. The error wrapping preserves the original error types (so errors.Is would work with the wrapped errors)
  3. The formatting of the joined error (with : as separator)

Ok sounds good, answering inline:

  1. It returns nil if there are no errors

Great call, but it is redundant to add it as comment since the zero value of error is nil and is common understanding that behavior therefore not added as a part of the code comments.

  1. The error wrapping preserves the original error types (so errors.Is would work with the wrapped errors)

Ok, that's a great suggestion, for the test, on the implementation side there's no use case for leveraging errors.Is, I went ahead and updated it in the following git commit: 9bd0b67

  1. The formatting of the joined error (with : as separator)

Ok, that's great suggestion as well, I went ahead and added it as part of the code comment, added via the git commit: b2f18a1

graphql_test.go Outdated
expected := errors.New("second error: first error")

if err := r.ErrorsJoined(); err != nil {
if !reflect.DeepEqual(err.Error(), expected.Error()) {
Copy link

Choose a reason for hiding this comment

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

Minor suggestion: When comparing error messages, you can directly compare the strings rather than using reflect.DeepEqual. It's simpler and more explicit about what you're comparing.

Suggested change
if !reflect.DeepEqual(err.Error(), expected.Error()) {
if err.Error() != expected.Error() {

Copy link
Member Author

Choose a reason for hiding this comment

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

Great call, addressed via the comment above: #717 (comment)

Comment on lines +282 to +290
func TestResultErrorsJoinedSuccess(t *testing.T) {
r := graphql.Result{
Errors: []gqlerrors.FormattedError{
{Message: "first error"},
{Message: "second error"},
},
}

expected := errors.New("second error: first error")
Copy link

Choose a reason for hiding this comment

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

It might be worth adding a test that verifies the error wrapping behavior works correctly with errors.Is or errors.As. This would ensure that the wrapping with %w is functioning as expected.

Copy link
Member Author

Choose a reason for hiding this comment

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

Great call, it was addressed it via: 9bd0b67

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.

1 participant