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

fix to return combined multiple errors #1598

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

Conversation

eyasy1217
Copy link

@eyasy1217 eyasy1217 commented May 6, 2024

closes #1515

MarshalJSON returns combined json marshal errors in AdditionalProperties.

e.g.

Before
error marshaling 'unsupportedType1': json: unsupported type: chan int

After
error marshaling 'unsupportedType1': json: unsupported type: chan int 'unsupportedType2': json: unsupported type: func()

(requierd go 1.20)

@eyasy1217 eyasy1217 marked this pull request as ready for review May 6, 2024 04:12
Copy link
Collaborator

@jamietanna jamietanna left a comment

Choose a reason for hiding this comment

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

Thanks for this!

I was wondering if maybe instead of this, we have i.e.

	var errs []error
	for fieldName, field := range a.AdditionalProperties {
		object[fieldName], err := json.Marshal(field)
		if err != nil {
			errs = append(errs, fmt.Errorf("'%s': %w", fieldName, err))
		}
	}
	err := errors.Join(errs...)
	if err != nil {
		return nil, fmt.Errorf("error marshaling %w", joinedError)
	}

Which then uses errors.Join in a slightly easier way?

@eyasy1217
Copy link
Author

Thanks for this!

I was wondering if maybe instead of this, we have i.e.

	var errs []error
	for fieldName, field := range a.AdditionalProperties {
		object[fieldName], err := json.Marshal(field)
		if err != nil {
			errs = append(errs, fmt.Errorf("'%s': %w", fieldName, err))
		}
	}
	err := errors.Join(errs...)
	if err != nil {
		return nil, fmt.Errorf("error marshaling %w", joinedError)
	}

Which then uses errors.Join in a slightly easier way?

It looks to me like this is doing something similar to the internal code of errors.Join
🤔

@jamietanna
Copy link
Collaborator

Yep, but with an array rather than joining in the loop will probably be a little more efficient, and only does the joining once

@eyasy1217
Copy link
Author

That's right.
🚀 52f6cdc

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.

Combine multiple errors
2 participants