public_test.go 4.5 KB

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