You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

107 lines
3.4 KiB

  1. package common
  2. import (
  3. fmt "fmt"
  4. "testing"
  5. "github.com/stretchr/testify/assert"
  6. )
  7. func TestErrorPanic(t *testing.T) {
  8. type pnk struct {
  9. msg string
  10. }
  11. capturePanic := func() (err Error) {
  12. defer func() {
  13. if r := recover(); r != nil {
  14. err = ErrorWrap(r, "This is the message in ErrorWrap(r, message).")
  15. }
  16. return
  17. }()
  18. panic(pnk{"something"})
  19. return nil
  20. }
  21. var err = capturePanic()
  22. assert.Equal(t, pnk{"something"}, err.Cause())
  23. assert.Equal(t, pnk{"something"}, err.T())
  24. assert.Equal(t, "This is the message in ErrorWrap(r, message).", err.Message())
  25. assert.Equal(t, "Error{`This is the message in ErrorWrap(r, message).` (cause: {something})}", fmt.Sprintf("%v", err))
  26. assert.Contains(t, fmt.Sprintf("%#v", err), "Message: This is the message in ErrorWrap(r, message).")
  27. assert.Contains(t, fmt.Sprintf("%#v", err), "Stack Trace:\n 0")
  28. }
  29. func TestErrorWrapSomething(t *testing.T) {
  30. var err = ErrorWrap("something", "formatter%v%v", 0, 1)
  31. assert.Equal(t, "something", err.Cause())
  32. assert.Equal(t, "something", err.T())
  33. assert.Equal(t, "formatter01", err.Message())
  34. assert.Equal(t, "Error{`formatter01` (cause: something)}", fmt.Sprintf("%v", err))
  35. assert.Regexp(t, `Message: formatter01\n`, fmt.Sprintf("%#v", err))
  36. assert.Contains(t, fmt.Sprintf("%#v", err), "Stack Trace:\n 0")
  37. }
  38. func TestErrorWrapNothing(t *testing.T) {
  39. var err = ErrorWrap(nil, "formatter%v%v", 0, 1)
  40. assert.Equal(t, nil, err.Cause())
  41. assert.Equal(t, nil, err.T())
  42. assert.Equal(t, "formatter01", err.Message())
  43. assert.Equal(t, "Error{`formatter01`}", fmt.Sprintf("%v", err))
  44. assert.Regexp(t, `Message: formatter01\n`, fmt.Sprintf("%#v", err))
  45. assert.Contains(t, fmt.Sprintf("%#v", err), "Stack Trace:\n 0")
  46. }
  47. func TestErrorNewError(t *testing.T) {
  48. var err = NewError("formatter%v%v", 0, 1)
  49. assert.Equal(t, nil, err.Cause())
  50. assert.Equal(t, "formatter%v%v", err.T())
  51. assert.Equal(t, "formatter01", err.Message())
  52. assert.Equal(t, "Error{`formatter01`}", fmt.Sprintf("%v", err))
  53. assert.Regexp(t, `Message: formatter01\n`, fmt.Sprintf("%#v", err))
  54. assert.NotContains(t, fmt.Sprintf("%#v", err), "Stack Trace")
  55. }
  56. func TestErrorNewErrorWithStacktrace(t *testing.T) {
  57. var err = NewError("formatter%v%v", 0, 1).Stacktrace()
  58. assert.Equal(t, nil, err.Cause())
  59. assert.Equal(t, "formatter%v%v", err.T())
  60. assert.Equal(t, "formatter01", err.Message())
  61. assert.Equal(t, "Error{`formatter01`}", fmt.Sprintf("%v", err))
  62. assert.Regexp(t, `Message: formatter01\n`, fmt.Sprintf("%#v", err))
  63. assert.Contains(t, fmt.Sprintf("%#v", err), "Stack Trace:\n 0")
  64. }
  65. func TestErrorNewErrorWithTrace(t *testing.T) {
  66. var err = NewError("formatter%v%v", 0, 1)
  67. err.Trace("trace %v", 1)
  68. err.Trace("trace %v", 2)
  69. err.Trace("trace %v", 3)
  70. assert.Equal(t, nil, err.Cause())
  71. assert.Equal(t, "formatter%v%v", err.T())
  72. assert.Equal(t, "formatter01", err.Message())
  73. assert.Equal(t, "Error{`formatter01`}", fmt.Sprintf("%v", err))
  74. assert.Regexp(t, `Message: formatter01\n`, fmt.Sprintf("%#v", err))
  75. dump := fmt.Sprintf("%#v", err)
  76. assert.NotContains(t, dump, "Stack Trace")
  77. assert.Regexp(t, `common/errors_test\.go:[0-9]+ - trace 1`, dump)
  78. assert.Regexp(t, `common/errors_test\.go:[0-9]+ - trace 2`, dump)
  79. assert.Regexp(t, `common/errors_test\.go:[0-9]+ - trace 3`, dump)
  80. }
  81. func TestErrorWrapError(t *testing.T) {
  82. var err1 error = NewError("my message")
  83. var err2 error = ErrorWrap(err1, "another message")
  84. assert.Equal(t, err1, err2)
  85. }