public_test.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. package fserr
  2. import (
  3. "errors"
  4. "github.com/stretchr/testify/assert"
  5. "github.com/stretchr/testify/suite"
  6. "net/http"
  7. "testing"
  8. )
  9. type TestPublicSuite struct {
  10. suite.Suite
  11. outerErr,
  12. newErr, newFmtErr,
  13. wrapErr, wrapFmtErr, wrapOuterErr, wrapNilErr,
  14. codeErr, codeOptionErr, codeOuterErr, codeNilErr error
  15. errBasicMsg string
  16. }
  17. func (s *TestPublicSuite) SetupTest() {
  18. s.outerErr = errors.New("outer error")
  19. s.errBasicMsg = "basic error"
  20. NewOK(ErrBasic, s.errBasicMsg)
  21. // new
  22. s.newErr = New("new error")
  23. s.newFmtErr = New("new %s", "error")
  24. // wrap
  25. s.wrapErr = Wrap(s.newErr, "wrap error")
  26. s.wrapFmtErr = Wrap(s.newErr, "wrap %s", "error")
  27. s.wrapOuterErr = Wrap(s.outerErr, "wrap error")
  28. s.wrapNilErr = Wrap(nil, "wrap error")
  29. // code
  30. s.codeErr = WithCode(s.newErr, ErrBasic)
  31. s.codeOptionErr = WithCode(s.newErr, ErrBasic, MsgOption("cover message"))
  32. s.codeOuterErr = WithCode(s.outerErr, ErrBasic)
  33. s.codeNilErr = WithCode(nil, ErrBasic)
  34. }
  35. func (s *TestPublicSuite) TestNew() {
  36. s.Equal("new error", s.newErr.Error())
  37. s.Equal("new error", s.newFmtErr.Error())
  38. }
  39. func (s *TestPublicSuite) TestWrap() {
  40. s.Equal("wrap error: new error", s.wrapErr.Error())
  41. s.Equal("wrap error: new error", s.wrapFmtErr.Error())
  42. s.Equal("wrap error: outer error", s.wrapOuterErr.Error())
  43. s.Nil(s.wrapNilErr)
  44. }
  45. func (s *TestPublicSuite) TestWithCode() {
  46. s.Equal(s.errBasicMsg, s.codeErr.Error())
  47. s.Equal("cover message", s.codeOptionErr.Error())
  48. s.Equal("basic error", s.codeOuterErr.Error())
  49. s.Equal("basic error", s.codeNilErr.Error())
  50. }
  51. func (s *TestPublicSuite) TestUnWrap() {
  52. s.Equal(s.newErr, UnWrap(s.newErr))
  53. s.Equal(s.newErr, UnWrap(s.wrapErr))
  54. s.Equal(s.outerErr, UnWrap(s.wrapOuterErr))
  55. s.Nil(UnWrap(s.wrapNilErr))
  56. s.Equal(s.newErr, UnWrap(s.codeErr))
  57. s.Equal(s.outerErr, UnWrap(s.codeOuterErr))
  58. s.Equal(nil, UnWrap(s.codeNilErr))
  59. }
  60. func (s *TestPublicSuite) TestIs() {
  61. s.True(Is(s.newErr, s.newErr))
  62. s.True(Is(s.wrapErr, s.newErr))
  63. s.True(Is(s.codeErr, s.newErr))
  64. s.False(Is(s.newErr, s.outerErr))
  65. s.False(Is(s.wrapErr, s.outerErr))
  66. s.False(Is(s.codeErr, s.outerErr))
  67. }
  68. func (s *TestPublicSuite) TestAs() {
  69. var originErr *fundamental
  70. s.True(As(s.wrapErr, &originErr))
  71. s.Equal(UnWrap(s.wrapErr), originErr)
  72. var codeErr *withCode
  73. s.False(As(s.wrapErr, &codeErr))
  74. }
  75. func (s *TestPublicSuite) TestOuterMsg() {
  76. s.Equal("new error", outerMsg(s.newErr))
  77. s.Equal("wrap error", outerMsg(s.wrapErr))
  78. s.Equal(s.errBasicMsg, outerMsg(s.codeErr))
  79. s.Equal("outer error", outerMsg(s.outerErr))
  80. s.Equal("", outerMsg(nil))
  81. }
  82. func (s *TestPublicSuite) TestParseCode() {
  83. code := ParseCode(s.codeErr)
  84. s.Equal(ErrBasic, code.BusinessCode)
  85. s.Equal(http.StatusOK, code.HttpCode)
  86. s.Equal(s.errBasicMsg, code.Msg)
  87. s.Equal(s.newErr, code.cause)
  88. err := ParseCode(Wrap(s.codeErr, "wrap error"))
  89. s.Equal(ErrBasic, err.BusinessCode)
  90. s.Equal(http.StatusOK, err.HttpCode)
  91. s.Equal("wrap error", err.Msg)
  92. s.Equal(s.newErr, err.cause)
  93. }
  94. func (s *TestPublicSuite) TestIsCode() {
  95. s.True(IsCode(s.codeErr, ErrBasic))
  96. s.False(IsCode(s.codeErr, ErrDb))
  97. s.False(IsCode(s.wrapErr, ErrBasic))
  98. }
  99. func (s *TestPublicSuite) TestSetServiceCode() {
  100. SetAppCode(300000)
  101. NewOK(2001, "ok")
  102. err := WithCode(nil, 2001)
  103. s.Equal(302001, ParseCode(err).BusinessCode)
  104. }
  105. func TestPublic(t *testing.T) {
  106. suite.Run(t, &TestPublicSuite{})
  107. }
  108. func TestServiceCode(t *testing.T) {
  109. SetAppCode(1)
  110. NewInternalError(ErrBasic, "basic error")
  111. code := ParseCode(WithCode(nil, ErrBasic))
  112. assert.Equal(t, "basic error", code.Msg)
  113. assert.Equal(t, 10101, code.BusinessCode)
  114. assert.Equal(t, http.StatusInternalServerError, code.HttpCode)
  115. SetAppCode(10)
  116. NewBadRequest(ErrDb, "db error")
  117. code = ParseCode(WithCode(nil, ErrDb))
  118. assert.Equal(t, "db error", code.Msg)
  119. assert.Equal(t, 100102, code.BusinessCode)
  120. assert.Equal(t, http.StatusBadRequest, code.HttpCode)
  121. SetAppCode(100000)
  122. NewInternalError(ErrParam, "param error")
  123. code = ParseCode(WithCode(nil, ErrParam))
  124. assert.Equal(t, "param error", code.Msg)
  125. assert.Equal(t, 100103, code.BusinessCode)
  126. assert.Equal(t, http.StatusInternalServerError, code.HttpCode)
  127. SetAppCode(1000)
  128. NewConflict(ErrRetry, "retry error")
  129. code = ParseCode(WithCode(nil, ErrRetry))
  130. assert.Equal(t, 100104, code.BusinessCode)
  131. assert.Equal(t, http.StatusConflict, code.HttpCode)
  132. assert.Equal(t, "retry error", code.Msg)
  133. }