diff --git a/internal/netxlite/errorsx/errwrapper.go b/internal/netxlite/errorsx/errwrapper.go index aa88b2c..cd5d31c 100644 --- a/internal/netxlite/errorsx/errwrapper.go +++ b/internal/netxlite/errorsx/errwrapper.go @@ -1,5 +1,7 @@ package errorsx +import "encoding/json" + // ErrWrapper is our error wrapper for Go errors. The key objective of // this structure is to properly set Failure, which is also returned by // the Error() method, to be one of the OONI failure strings. @@ -60,3 +62,8 @@ func (e *ErrWrapper) Error() string { func (e *ErrWrapper) Unwrap() error { return e.WrappedErr } + +// MarshalJSON converts an ErrWrapper to a JSON value. +func (e *ErrWrapper) MarshalJSON() ([]byte, error) { + return json.Marshal(e.Failure) +} diff --git a/internal/netxlite/errorsx/errwrapper_test.go b/internal/netxlite/errorsx/errwrapper_test.go index 6c7aca0..b2d41e0 100644 --- a/internal/netxlite/errorsx/errwrapper_test.go +++ b/internal/netxlite/errorsx/errwrapper_test.go @@ -1,6 +1,7 @@ package errorsx import ( + "encoding/json" "errors" "io" "testing" @@ -23,4 +24,19 @@ func TestErrWrapper(t *testing.T) { t.Fatal("cannot unwrap error") } }) + + t.Run("MarshalJSON", func(t *testing.T) { + wrappedErr := &ErrWrapper{ + Failure: FailureEOFError, + WrappedErr: io.EOF, + } + data, err := json.Marshal(wrappedErr) + if err != nil { + t.Fatal(err) + } + s := string(data) + if s != "\""+FailureEOFError+"\"" { + t.Fatal("invalid serialization", s) + } + }) }