exception_test.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package exception
  2. import (
  3. "testing"
  4. "fmt"
  5. "github.com/pkg/errors"
  6. )
  7. func TestFromError(t *testing.T) {
  8. err := errors.New("a random error")
  9. var e Throwable = FromError(err)
  10. if e.Cause() != nil {
  11. t.Error("Exceptions converted via FromError should have a nil cause.")
  12. }
  13. if e.Message() != err.Error() {
  14. t.Error("Exceptions converted via FromError should use original err.Error() as its message.")
  15. }
  16. expected := "Exception: a random error"
  17. if e.Error() != expected {
  18. t.Error(
  19. "e.Error() FAIL\n Actual Value: %v\nExpected Value: %s",
  20. e.Error(), expected)
  21. }
  22. }
  23. func TestNew(t *testing.T) {
  24. var e Throwable = New("an exception", nil)
  25. t.Log(e.Error())
  26. }
  27. func g(n int) Throwable {
  28. if n == 0 {
  29. return f()
  30. } else {
  31. return g(n-1)
  32. }
  33. }
  34. func f() Throwable {
  35. return FromError(errors.New("from f"))
  36. }
  37. func ExampleNewWithStackTraceDepthLimit() {
  38. var e Throwable = NewWithStackTraceDepthLimit("an exception", nil, 10)
  39. fmt.Println(e.StackTrace())
  40. // Output: FIXME
  41. }
  42. func ExampleNew() {
  43. var e Throwable = New("an exception", nil)
  44. fmt.Println(e.StackTrace())
  45. // Output: FIXME
  46. }
  47. func ExampleRecursiveCalls() {
  48. var e Throwable = g(3)
  49. fmt.Println(e.StackTrace())
  50. // Output: FIXME
  51. }