Skip to content

Commit

Permalink
Create UnsafeNonFatal for use in the fiber runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
kapunga committed Feb 1, 2025
1 parent ac9f169 commit 58a9c5d
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions core/shared/src/main/scala/cats/effect/unsafe/UnsafeNonFatal.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright 2020-2025 Typelevel
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package cats.effect
package unsafe

import scala.util.control.ControlThrowable

/**
* An alternative to [[scala.util.control.NonFatal]] that does not treat
* [[java.lang.InterruptedException]] as fatal. This is intended for the exclusive use of the
* Cats-Effect runtime. It is not recommended to treat interrupts as non-fatal in application
* code, as handling interrupts gracefully is the responsibility of the runtime, so
* [[UnsafeNonFatal]] should only be used in the fiber runtime.
*/
object UnsafeNonFatal {

/**
* Returns true if the provided `Throwable` is to be considered non-fatal, or false if it is
* to be considered fatal
*/
def apply(t: Throwable): Boolean = t match {
case _: VirtualMachineError | _: ThreadDeath | _: LinkageError | _: ControlThrowable =>
false
case _ => true
}

/**
* Returns Some(t) if RuntimeNonFatal(t) == true, otherwise None
*/
def unapply(t: Throwable): Option[Throwable] = Some(t).filter(apply)
}

0 comments on commit 58a9c5d

Please sign in to comment.