Fix how ErrrorData is marshaled (#2565)

## Description

The error type does not get marshaled which can lead to errors when we try to un-marshal a manifest
that had errors stored in it.

Repro here: https://go.dev/play/p/tgj8oq5CGFd

For now - this disables JSON marshaling and also fixes the error un-marshaling. I have added a regression test
to verify both behaviors.

As a follow-up, I believe we can implement a custom marshaler.

## Does this PR need a docs update or release note?

- [ ]  Yes, it's included
- [ ] 🕐 Yes, but in a later PR
- [x]  No 

## Type of change

<!--- Please check the type of change your PR introduces: --->
- [ ] 🌻 Feature
- [x] 🐛 Bugfix
- [ ] 🗺️ Documentation
- [ ] 🤖 Test
- [ ] 💻 CI/Deployment
- [ ] 🧹 Tech Debt/Cleanup

## Test Plan

<!-- How will this be tested prior to merging.-->
- [ ] 💪 Manual
- [x]  Unit test
- [ ] 💚 E2E
This commit is contained in:
Vaibhav Kamra 2023-02-17 17:43:59 -08:00 committed by GitHub
parent 207232e8d9
commit c3f5e50b9f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 49 additions and 2 deletions

View File

@ -32,8 +32,8 @@ type Errors struct {
// ErrorsData provides the errors data alone, without sync // ErrorsData provides the errors data alone, without sync
// controls, allowing the data to be persisted. // controls, allowing the data to be persisted.
type ErrorsData struct { type ErrorsData struct {
Err error `json:"err"` Err error `json:"-"`
Errs []error `json:"errs"` Errs []error `json:"-"`
FailFast bool `json:"failFast"` FailFast bool `json:"failFast"`
} }

View File

@ -1,7 +1,9 @@
package fault_test package fault_test
import ( import (
"encoding/json"
"errors" "errors"
"fmt"
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
@ -204,3 +206,48 @@ func (suite *FaultErrorsUnitSuite) TestData() {
assert.ElementsMatch(t, n.Errs(), d.Errs) assert.ElementsMatch(t, n.Errs(), d.Errs)
assert.True(t, d.FailFast) assert.True(t, d.FailFast)
} }
func (suite *FaultErrorsUnitSuite) TestMarshalUnmarshal() {
t := suite.T()
// not fail-fast
n := fault.New(false)
require.NotNil(t, n)
n.Add(errors.New("1"))
n.Add(errors.New("2"))
data := n.Data()
jsonStr, err := json.Marshal(data)
require.NoError(t, err)
um := fault.ErrorsData{}
err = json.Unmarshal(jsonStr, &um)
require.NoError(t, err)
}
type legacyErrorsData struct {
Err error `json:"err"`
Errs []error `json:"errs"`
FailFast bool `json:"failFast"`
}
func (suite *FaultErrorsUnitSuite) TestUnmarshalLegacy() {
t := suite.T()
oldData := &legacyErrorsData{
Errs: []error{fmt.Errorf("foo error"), fmt.Errorf("foo error"), fmt.Errorf("foo error")},
}
jsonStr, err := json.Marshal(oldData)
require.NoError(t, err)
t.Logf("jsonStr is %s\n", jsonStr)
um := fault.ErrorsData{}
err = json.Unmarshal(jsonStr, &um)
require.NoError(t, err)
}