error.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. package rx
  2. const invalid_no_except = "An effect that assumed to be a no-exception effect has thrown an error"
  3. func Throw(e Object) Observable {
  4. return Observable { func(_ Scheduler, ob *observer) {
  5. ob.error(e)
  6. } }
  7. }
  8. func (e Observable) NoExcept() Observable {
  9. return Observable { func(sched Scheduler, ob *observer) {
  10. sched.run(e, &observer {
  11. context: ob.context,
  12. next: ob.next,
  13. error: func(_ Object) {
  14. panic(invalid_no_except)
  15. },
  16. complete: ob.complete,
  17. })
  18. } }
  19. }
  20. func (e Observable) Catch(f func(Object) Observable) Observable {
  21. return Observable { func(sched Scheduler, ob *observer) {
  22. sched.run(e, &observer {
  23. context: ob.context,
  24. next: func(x Object) {
  25. ob.next(x)
  26. },
  27. error: func(err Object) {
  28. var caught_effect = f(err)
  29. sched.run(caught_effect, ob)
  30. },
  31. complete: func() {
  32. ob.complete()
  33. },
  34. })
  35. } }
  36. }
  37. func (e Observable) CatchRetry(f func(Object) Observable) Observable {
  38. var try Observable
  39. try = Observable { func(sched Scheduler, ob *observer) {
  40. sched.run(e, &observer {
  41. context: ob.context,
  42. next: func(x Object) {
  43. ob.next(x)
  44. },
  45. error: func(err Object) {
  46. var caught_effect =
  47. f(err).NoExcept().Then(func(retry Object) Observable {
  48. if retry.(bool) {
  49. return try
  50. } else {
  51. return Throw(err)
  52. }
  53. })
  54. sched.run(caught_effect, ob)
  55. },
  56. complete: func() {
  57. ob.complete()
  58. },
  59. })
  60. } }
  61. return try
  62. }
  63. func (e Observable) CatchThrow(error_mapper func(Object)Object) Observable {
  64. return Observable { func(sched Scheduler, ob *observer) {
  65. sched.run(e, &observer {
  66. context: ob.context,
  67. next: ob.next,
  68. error: func(err Object) {
  69. ob.error(error_mapper(err))
  70. },
  71. complete: ob.complete,
  72. })
  73. } }
  74. }