public_test.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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,
  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. // code
  31. s.codeErr = WithCode(s.newErr, ErrBasic)
  32. s.codeOptionErr = WithCode(s.newErr, ErrBasic, MsgOption("cover message"))
  33. s.codeOuterErr = WithCode(s.outerErr, ErrBasic)
  34. s.codeNilErr = WithCode(nil, ErrBasic)
  35. s.stackErr = WithStack(errors.New("stack error"))
  36. }
  37. func (s *TestPublicSuite) TestNew() {
  38. s.Equal("new error", s.newErr.Error())
  39. s.Equal("new error", s.newFmtErr.Error())
  40. }
  41. func (s *TestPublicSuite) TestWrap() {
  42. s.Equal("wrap error: new error", s.wrapErr.Error())
  43. s.Equal("wrap error: new error", s.wrapFmtErr.Error())
  44. s.Equal("wrap error: outer error", s.wrapOuterErr.Error())
  45. s.Nil(s.wrapNilErr)
  46. }
  47. func (s *TestPublicSuite) TestWithCode() {
  48. s.Equal(s.errBasicMsg, s.codeErr.Error())
  49. s.Equal("cover message", s.codeOptionErr.Error())
  50. s.Equal("basic error", s.codeOuterErr.Error())
  51. s.Equal("basic error", s.codeNilErr.Error())
  52. }
  53. func (s *TestPublicSuite) TestUnWrap() {
  54. s.Equal(s.newErr, UnWrap(s.newErr))
  55. s.Equal(s.newErr, UnWrap(s.wrapErr))
  56. s.Equal(s.outerErr, UnWrap(s.wrapOuterErr))
  57. s.Nil(UnWrap(s.wrapNilErr))
  58. s.Equal(s.newErr, UnWrap(s.codeErr))
  59. s.Equal(s.outerErr, UnWrap(s.codeOuterErr))
  60. s.Equal(nil, UnWrap(s.codeNilErr))
  61. }
  62. func (s *TestPublicSuite) TestIs() {
  63. s.True(Is(s.newErr, s.newErr))
  64. s.True(Is(s.wrapErr, s.newErr))
  65. s.True(Is(s.codeErr, s.newErr))
  66. s.False(Is(s.newErr, s.outerErr))
  67. s.False(Is(s.wrapErr, s.outerErr))
  68. s.False(Is(s.codeErr, s.outerErr))
  69. }
  70. func (s *TestPublicSuite) TestAs() {
  71. var originErr *fundamental
  72. s.True(As(s.wrapErr, &originErr))
  73. s.Equal(UnWrap(s.wrapErr), originErr)
  74. var codeErr *withCode
  75. s.False(As(s.wrapErr, &codeErr))
  76. }
  77. func (s *TestPublicSuite) TestOuterMsg() {
  78. s.Equal("new error", outerMsg(s.newErr))
  79. s.Equal("wrap error", outerMsg(s.wrapErr))
  80. s.Equal(s.errBasicMsg, outerMsg(s.codeErr))
  81. s.Equal("outer error", outerMsg(s.outerErr))
  82. s.Equal("", outerMsg(nil))
  83. }
  84. func (s *TestPublicSuite) TestParseCode() {
  85. code := ParseCode(s.codeErr)
  86. s.Equal(ErrBasic, code.BusinessCode)
  87. s.Equal(http.StatusOK, code.HttpCode)
  88. s.Equal(s.errBasicMsg, code.Msg)
  89. s.Equal(s.newErr, code.cause)
  90. err := ParseCode(Wrap(s.codeErr, "wrap error"))
  91. s.Equal(ErrBasic, err.BusinessCode)
  92. s.Equal(http.StatusOK, err.HttpCode)
  93. s.Equal("wrap error", err.Msg)
  94. s.Equal(s.newErr, err.cause)
  95. }
  96. func (s *TestPublicSuite) TestIsCode() {
  97. s.True(IsCode(s.codeErr, ErrBasic))
  98. s.False(IsCode(s.codeErr, ErrDb))
  99. s.False(IsCode(s.wrapErr, ErrBasic))
  100. }
  101. func (s *TestPublicSuite) TestSetServiceCode() {
  102. SetAppCode(300000)
  103. NewOK(2001, "ok")
  104. err := WithCode(nil, 2001)
  105. s.Equal(302001, ParseCode(err).BusinessCode)
  106. }
  107. func (s *TestPublicSuite) TestWithStack() {
  108. s.Equal("stack error", s.stackErr.Error())
  109. fslog.Error(s.stackErr)
  110. }
  111. func TestPublic(t *testing.T) {
  112. suite.Run(t, &TestPublicSuite{})
  113. }
  114. func TestServiceCode(t *testing.T) {
  115. SetAppCode(1)
  116. NewInternalError(ErrBasic, "basic error")
  117. code := ParseCode(WithCode(nil, ErrBasic))
  118. assert.Equal(t, "basic error", code.Msg)
  119. assert.Equal(t, 10001, code.BusinessCode)
  120. assert.Equal(t, http.StatusInternalServerError, code.HttpCode)
  121. SetAppCode(10)
  122. NewBadRequest(ErrDb, "db error")
  123. code = ParseCode(WithCode(nil, ErrDb))
  124. assert.Equal(t, "db error", code.Msg)
  125. assert.Equal(t, 100002, code.BusinessCode)
  126. assert.Equal(t, http.StatusBadRequest, code.HttpCode)
  127. SetAppCode(100000)
  128. NewInternalError(ErrParam, "param error")
  129. code = ParseCode(WithCode(nil, ErrParam))
  130. assert.Equal(t, "param error", code.Msg)
  131. assert.Equal(t, 100003, code.BusinessCode)
  132. assert.Equal(t, http.StatusInternalServerError, code.HttpCode)
  133. SetAppCode(1000)
  134. NewConflict(ErrRetry, "retry error")
  135. code = ParseCode(WithCode(nil, ErrRetry))
  136. assert.Equal(t, 100004, code.BusinessCode)
  137. assert.Equal(t, http.StatusConflict, code.HttpCode)
  138. assert.Equal(t, "retry error", code.Msg)
  139. }