stack_test.go 981 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package xerr
  2. import (
  3. "errors"
  4. "fmt"
  5. "runtime"
  6. "testing"
  7. "github.com/getsentry/sentry-go"
  8. "github.com/stretchr/testify/assert"
  9. "github.com/stretchr/testify/require"
  10. )
  11. func TestStackTraceInSentry(t *testing.T) {
  12. err := WithStack(errors.New("test error"))
  13. stackTrace := sentry.ExtractStacktrace(err)
  14. require.NotNil(t, stackTrace)
  15. assert.Equal(t, 1, len(stackTrace.Frames))
  16. }
  17. func TestWithStackRepeat(t *testing.T) {
  18. err := WithStack(errors.New("test error"))
  19. err = wrapErr(err)
  20. _, ok := err.(*withStack)
  21. require.True(t, ok)
  22. }
  23. func TestWrapStack(t *testing.T) {
  24. err := wrapStack(nil, 0)
  25. require.Nil(t, err, "wrap nil")
  26. err = wrapStack(errors.New("test error"), 0)
  27. require.NotNil(t, err)
  28. assert.Equal(t, "test error", err.Error(), "wrap normal error")
  29. }
  30. func wrapErr(err error) error {
  31. return wrapStack(err, 1)
  32. }
  33. func funcStack(s *stack) string {
  34. frame, _ := runtime.CallersFrames(*s).Next()
  35. return fmt.Sprintf("pkg.xerr.%s", funcname(frame.Function))
  36. }