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.

101 lines
3.1 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. }()
  17. panic(pnk{"something"})
  18. }
  19. var err = capturePanic()
  20. assert.Equal(t, pnk{"something"}, err.Data())
  21. assert.Equal(t, "{something}", fmt.Sprintf("%v", err))
  22. assert.Contains(t, fmt.Sprintf("%#v", err), "This is the message in ErrorWrap(r, message).")
  23. assert.Contains(t, fmt.Sprintf("%#v", err), "Stack Trace:\n 0")
  24. }
  25. func TestErrorWrapSomething(t *testing.T) {
  26. var err = ErrorWrap("something", "formatter%v%v", 0, 1)
  27. assert.Equal(t, "something", err.Data())
  28. assert.Equal(t, "something", fmt.Sprintf("%v", err))
  29. assert.Regexp(t, `formatter01\n`, fmt.Sprintf("%#v", err))
  30. assert.Contains(t, fmt.Sprintf("%#v", err), "Stack Trace:\n 0")
  31. }
  32. func TestErrorWrapNothing(t *testing.T) {
  33. var err = ErrorWrap(nil, "formatter%v%v", 0, 1)
  34. assert.Equal(t,
  35. FmtError{"formatter%v%v", []interface{}{0, 1}},
  36. err.Data())
  37. assert.Equal(t, "formatter01", fmt.Sprintf("%v", err))
  38. assert.Contains(t, fmt.Sprintf("%#v", err), `Data: common.FmtError{format:"formatter%v%v", args:[]interface {}{0, 1}}`)
  39. assert.Contains(t, fmt.Sprintf("%#v", err), "Stack Trace:\n 0")
  40. }
  41. func TestErrorNewError(t *testing.T) {
  42. var err = NewError("formatter%v%v", 0, 1)
  43. assert.Equal(t,
  44. FmtError{"formatter%v%v", []interface{}{0, 1}},
  45. err.Data())
  46. assert.Equal(t, "formatter01", fmt.Sprintf("%v", err))
  47. assert.Contains(t, fmt.Sprintf("%#v", err), `Data: common.FmtError{format:"formatter%v%v", args:[]interface {}{0, 1}}`)
  48. assert.NotContains(t, fmt.Sprintf("%#v", err), "Stack Trace")
  49. }
  50. func TestErrorNewErrorWithStacktrace(t *testing.T) {
  51. var err = NewError("formatter%v%v", 0, 1).Stacktrace()
  52. assert.Equal(t,
  53. FmtError{"formatter%v%v", []interface{}{0, 1}},
  54. err.Data())
  55. assert.Equal(t, "formatter01", fmt.Sprintf("%v", err))
  56. assert.Contains(t, fmt.Sprintf("%#v", err), `Data: common.FmtError{format:"formatter%v%v", args:[]interface {}{0, 1}}`)
  57. assert.Contains(t, fmt.Sprintf("%#v", err), "Stack Trace:\n 0")
  58. }
  59. func TestErrorNewErrorWithTrace(t *testing.T) {
  60. var err = NewError("formatter%v%v", 0, 1)
  61. err.Trace(0, "trace %v", 1)
  62. err.Trace(0, "trace %v", 2)
  63. err.Trace(0, "trace %v", 3)
  64. assert.Equal(t,
  65. FmtError{"formatter%v%v", []interface{}{0, 1}},
  66. err.Data())
  67. assert.Equal(t, "formatter01", fmt.Sprintf("%v", err))
  68. assert.Contains(t, fmt.Sprintf("%#v", err), `Data: common.FmtError{format:"formatter%v%v", args:[]interface {}{0, 1}}`)
  69. dump := fmt.Sprintf("%#v", err)
  70. assert.NotContains(t, dump, "Stack Trace")
  71. assert.Regexp(t, `common/errors_test\.go:[0-9]+ - trace 1`, dump)
  72. assert.Regexp(t, `common/errors_test\.go:[0-9]+ - trace 2`, dump)
  73. assert.Regexp(t, `common/errors_test\.go:[0-9]+ - trace 3`, dump)
  74. }
  75. func TestErrorWrapError(t *testing.T) {
  76. var err1 error = NewError("my message")
  77. var err2 error = ErrorWrap(err1, "another message")
  78. assert.Equal(t, err1, err2)
  79. }