18b2eb37ff
This simplifies serializing errors to `*string`. It did not occur to me before. It seems quite a nice improvement. Part of https://github.com/ooni/probe/issues/1591
43 lines
800 B
Go
43 lines
800 B
Go
package errorsx
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"io"
|
|
"testing"
|
|
)
|
|
|
|
func TestErrWrapper(t *testing.T) {
|
|
t.Run("Error", func(t *testing.T) {
|
|
err := &ErrWrapper{Failure: FailureDNSNXDOMAINError}
|
|
if err.Error() != FailureDNSNXDOMAINError {
|
|
t.Fatal("invalid return value")
|
|
}
|
|
})
|
|
|
|
t.Run("Unwrap", func(t *testing.T) {
|
|
err := &ErrWrapper{
|
|
Failure: FailureEOFError,
|
|
WrappedErr: io.EOF,
|
|
}
|
|
if !errors.Is(err, io.EOF) {
|
|
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)
|
|
}
|
|
})
|
|
}
|