@@ -9,6 +9,7 @@ import java.util.concurrent.Semaphore
9
9
class ExceptionTest extends AnyFlatSpec with Matchers {
10
10
class CustomException extends RuntimeException
11
11
class CustomException2 extends RuntimeException
12
+ class CustomException3 (e : Exception ) extends RuntimeException (e)
12
13
13
14
" scoped" should " throw the exception thrown by a joined fork" in {
14
15
val trail = Trail ()
@@ -72,14 +73,41 @@ class ExceptionTest extends AnyFlatSpec with Matchers {
72
73
throw CustomException ()
73
74
}
74
75
}
75
- catch
76
- case e : Exception =>
77
- val suppressed = e.getSuppressed.map(_.getClass.getSimpleName)
78
- trail.add(s " ${e.getClass.getSimpleName}(suppressed= ${suppressed.mkString(" ," )}) " )
76
+ catch case e : Exception => addExceptionWithSuppressedTo(trail, e)
79
77
80
78
trail.get shouldBe Vector (" CustomException(suppressed=CustomException2)" )
81
79
}
82
80
81
+ it should " not add the original exception as suppressed" in {
82
+ val trail = Trail ()
83
+ try
84
+ supervised {
85
+ val f = fork {
86
+ throw new CustomException ()
87
+ }
88
+ f.join()
89
+ }
90
+ catch case e : Exception => addExceptionWithSuppressedTo(trail, e)
91
+
92
+ trail.get shouldBe Vector (" CustomException(suppressed=)" )
93
+ }
94
+
95
+ it should " add an exception as suppressed, even if it wraps the original exception" in {
96
+ val trail = Trail ()
97
+ try
98
+ supervised {
99
+ val f = fork {
100
+ throw new CustomException ()
101
+ }
102
+ try f.join() catch {
103
+ case e : Exception => throw new CustomException3 (e)
104
+ }
105
+ }
106
+ catch case e : Exception => addExceptionWithSuppressedTo(trail, e)
107
+
108
+ trail.get shouldBe Vector (" CustomException(suppressed=CustomException3)" )
109
+ }
110
+
83
111
" joinEither" should " catch the exception with which a fork ends" in {
84
112
val r = supervised {
85
113
val f = forkUnsupervised {
@@ -90,4 +118,9 @@ class ExceptionTest extends AnyFlatSpec with Matchers {
90
118
91
119
r should matchPattern { case Left (e : CustomException ) => }
92
120
}
121
+
122
+ def addExceptionWithSuppressedTo (t : Trail , e : Throwable ): Unit = {
123
+ val suppressed = e.getSuppressed.map(_.getClass.getSimpleName)
124
+ t.add(s " ${e.getClass.getSimpleName}(suppressed= ${suppressed.mkString(" ," )}) " )
125
+ }
93
126
}
0 commit comments