Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions core-tests/shared/src/test/scala/zio/internal/IsFatalSpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package zio.internal

import zio.test.Assertion._
import zio.test._
import zio.ZIOBaseSpec

object IsFatalSpec extends ZIOBaseSpec {
def spec =
suite("IsFatal")(
suite("empty")(
test("apply(VirtualMachineError)") {
val isFatal = IsFatal.empty
assert(isFatal(new OutOfMemoryError()))(isTrue) &&
assert(isFatal(new StackOverflowError()))(isTrue)
},
test("apply(Non-VirtualMachineError)") {
val isFatal = IsFatal.empty
assert(isFatal(new RuntimeException()))(isFalse) &&
assert(isFatal(new IllegalArgumentException()))(isFalse)
},
test("|") {
val tag = IsFatal(classOf[RuntimeException])
assert(IsFatal.empty | tag)(equalTo(tag)) &&
assert(tag | IsFatal.empty)(equalTo(tag))
}
),
suite("Tag")(
test("matches assignable types") {
val isFatal = IsFatal(classOf[RuntimeException])
assert(isFatal(new RuntimeException()))(isTrue) &&
assert(isFatal(new IllegalArgumentException()))(isTrue) &&
assert(isFatal(new Exception()))(isFalse)
}
),
suite("Both")(
test("matches any of the two") {
val first = IsFatal(classOf[RuntimeException])
val second = IsFatal(classOf[Exception])
val combined = first | second
assert(combined(new RuntimeException()))(isTrue) &&
assert(combined(new IllegalArgumentException()))(isTrue) &&
assert(combined(new Exception()))(isTrue) &&
assert(combined(new Throwable()))(isFalse)
}
)
)
}
15 changes: 8 additions & 7 deletions core/shared/src/main/scala/zio/internal/IsFatal.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,21 @@ sealed trait IsFatal extends (Throwable => Boolean) { self =>
import IsFatal._

def apply(t: Throwable): Boolean =
if (t.isInstanceOf[StackOverflowError]) true
if (t.isInstanceOf[VirtualMachineError]) true
else
self match {
case _: Empty.type => false
case Both(left, right) => left(t) || right(t)
case Empty => false
case Single(tag) => tag.isAssignableFrom(t.getClass)
}

def |(that: IsFatal): IsFatal =
(self, that) match {
case (self, Empty) => self
case (Empty, that) => that
case (self, that) => Both(self, that)
}
if (self eq Empty) that
else
that match {
case _: Empty.type => self
case _ => Both(self, that)
}
}

object IsFatal {
Expand Down