-
Notifications
You must be signed in to change notification settings - Fork 7
/
MultipleException.scala
62 lines (51 loc) · 1.79 KB
/
MultipleException.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package com.thoughtworks.future
import java.io.{PrintStream, PrintWriter}
import scalaz.Semigroup
object MultipleException {
implicit object multipleExceptionThrowableSemigroup extends Semigroup[Throwable] {
override def append(f1: Throwable, f2: => Throwable): Throwable =
f1 match {
case MultipleException(exceptionSet1) =>
f2 match {
case MultipleException(exceptionSet2) => MultipleException(exceptionSet1 ++ exceptionSet2)
case e: Throwable => MultipleException(exceptionSet1 + e)
}
case _: Throwable =>
f2 match {
case MultipleException(exceptionSet2) => MultipleException(exceptionSet2 + f1)
case `f1` => f1
case e: Throwable => MultipleException(Set(f1, e))
}
}
}
}
final case class MultipleException(throwableSet: Set[Throwable]) extends RuntimeException("Multiple exceptions found") {
override def toString: String = throwableSet.mkString("\n")
override def printStackTrace(): Unit = {
for (throwable <- throwableSet) {
throwable.printStackTrace()
}
}
override def printStackTrace(s: PrintStream): Unit = {
for (throwable <- throwableSet) {
throwable.printStackTrace(s)
}
}
override def printStackTrace(s: PrintWriter): Unit = {
for (throwable <- throwableSet) {
throwable.printStackTrace(s)
}
}
override def getStackTrace: Array[StackTraceElement] = synchronized {
super.getStackTrace match {
case null =>
setStackTrace(throwableSet.flatMap(_.getStackTrace)(collection.breakOut))
super.getStackTrace
case stackTrace =>
stackTrace
}
}
override def fillInStackTrace(): this.type = {
this
}
}