github.com/abemedia/go-don@v0.2.2-0.20240329015135-be88e32bb73b/errors_test.go (about)

     1  package don_test
     2  
     3  import (
     4  	"encoding/xml"
     5  	"errors"
     6  	"fmt"
     7  	"testing"
     8  
     9  	"github.com/abemedia/go-don"
    10  	_ "github.com/abemedia/go-don/encoding/json"
    11  	_ "github.com/abemedia/go-don/encoding/text"
    12  	_ "github.com/abemedia/go-don/encoding/xml"
    13  	_ "github.com/abemedia/go-don/encoding/yaml"
    14  	"github.com/goccy/go-json"
    15  	"github.com/google/go-cmp/cmp"
    16  	"github.com/valyala/fasthttp"
    17  	"gopkg.in/yaml.v3"
    18  )
    19  
    20  func TestError_Is(t *testing.T) {
    21  	if !errors.Is(don.Error(errTest, 0), errTest) {
    22  		t.Error("should match wrapped error")
    23  	}
    24  	if !errors.Is(don.Error(errTest, fasthttp.StatusBadRequest), don.ErrBadRequest) {
    25  		t.Error("should match status error")
    26  	}
    27  }
    28  
    29  func TestError_Unwrap(t *testing.T) {
    30  	if errors.Unwrap(don.Error(errTest, 0)) != errTest { //nolint:errorlint
    31  		t.Error("should unwrap wrapped error")
    32  	}
    33  }
    34  
    35  func TestError_StatusCode(t *testing.T) {
    36  	if don.Error(don.ErrBadRequest, 0).StatusCode() != fasthttp.StatusBadRequest {
    37  		t.Error("should respect wrapped error's status code")
    38  	}
    39  	if don.Error(don.ErrBadRequest, fasthttp.StatusConflict).StatusCode() != fasthttp.StatusConflict {
    40  		t.Error("should ignore wrapped error's status code if explicitly set")
    41  	}
    42  }
    43  
    44  func TestError_MarshalText(t *testing.T) {
    45  	b, _ := don.Error(errTest, 0).MarshalText()
    46  	if diff := cmp.Diff("test", string(b)); diff != "" {
    47  		t.Error(diff)
    48  	}
    49  	b, _ = don.Error(&testError{}, 0).MarshalText()
    50  	if diff := cmp.Diff("custom", string(b)); diff != "" {
    51  		t.Error(diff)
    52  	}
    53  }
    54  
    55  func TestError_MarshalJSON(t *testing.T) {
    56  	b, _ := json.Marshal(don.Error(errTest, 0))
    57  	if diff := cmp.Diff(`{"message":"test"}`, string(b)); diff != "" {
    58  		t.Error(diff)
    59  	}
    60  	b, _ = json.Marshal(don.Error(&testError{}, 0))
    61  	if diff := cmp.Diff(`{"custom":"test"}`, string(b)); diff != "" {
    62  		t.Error(diff)
    63  	}
    64  }
    65  
    66  func TestError_MarshalXML(t *testing.T) {
    67  	b, _ := xml.Marshal(don.Error(errTest, 0))
    68  	if diff := cmp.Diff("<message>test</message>", string(b)); diff != "" {
    69  		t.Error(diff)
    70  	}
    71  	b, _ = xml.Marshal(don.Error(&testError{}, 0))
    72  	if diff := cmp.Diff("<custom>test</custom>", string(b)); diff != "" {
    73  		t.Error(diff)
    74  	}
    75  }
    76  
    77  func TestError_MarshalYAML(t *testing.T) {
    78  	b, _ := yaml.Marshal(don.Error(errTest, 0))
    79  	if diff := cmp.Diff("message: test\n", string(b)); diff != "" {
    80  		t.Error(diff)
    81  	}
    82  	b, _ = yaml.Marshal(don.Error(&testError{}, 0))
    83  	if diff := cmp.Diff("custom: test\n", string(b)); diff != "" {
    84  		t.Error(diff)
    85  	}
    86  }
    87  
    88  var errTest = errors.New("test")
    89  
    90  type testError struct{}
    91  
    92  func (e *testError) Error() string {
    93  	return "test"
    94  }
    95  
    96  func (e *testError) MarshalText() ([]byte, error) {
    97  	return []byte("custom"), nil
    98  }
    99  
   100  func (e *testError) MarshalJSON() ([]byte, error) {
   101  	return []byte(fmt.Sprintf(`{"custom":%q}`, e.Error())), nil
   102  }
   103  
   104  func (e *testError) MarshalXML(enc *xml.Encoder, start xml.StartElement) error {
   105  	start.Name = xml.Name{Local: "custom"}
   106  	return enc.EncodeElement(e.Error(), start)
   107  }
   108  
   109  func (e *testError) MarshalYAML() (any, error) {
   110  	return map[string]string{"custom": e.Error()}, nil
   111  }