= util add Throwables helper to help fatal checking of Throwable for Java,alias with Scala's Nonfatal pattern matching. (#24661)
This commit is contained in:
parent
bcc86f7703
commit
33ca78052c
2 changed files with 67 additions and 0 deletions
26
akka-actor-tests/src/test/java/akka/japi/ThrowablesTest.java
Normal file
26
akka-actor-tests/src/test/java/akka/japi/ThrowablesTest.java
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
/**
|
||||
* Copyright (C) 2014-2018 Lightbend Inc. <https://www.lightbend.com>
|
||||
*/
|
||||
package akka.japi;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import scala.util.control.ControlThrowable;
|
||||
|
||||
public class ThrowablesTest {
|
||||
@Test
|
||||
public void testIsNonFatal(){
|
||||
Assert.assertTrue(Throwables.isNonFatal(new IllegalArgumentException("isNonFatal")));
|
||||
}
|
||||
|
||||
private static class ControlThrowableImpl extends Throwable implements ControlThrowable{}
|
||||
|
||||
@Test
|
||||
public void testIsFatal(){
|
||||
Assert.assertTrue(Throwables.isFatal(new StackOverflowError("fatal")));
|
||||
Assert.assertTrue(Throwables.isFatal(new ThreadDeath()));
|
||||
Assert.assertTrue(Throwables.isFatal(new InterruptedException("fatal")));
|
||||
Assert.assertTrue(Throwables.isFatal(new LinkageError("fatal")));
|
||||
Assert.assertTrue(Throwables.isFatal(new ControlThrowableImpl()));
|
||||
}
|
||||
}
|
||||
41
akka-actor/src/main/scala/akka/japi/Throwables.scala
Normal file
41
akka-actor/src/main/scala/akka/japi/Throwables.scala
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
/**
|
||||
* Copyright (C) 2009-2018 Lightbend Inc. <https://www.lightbend.com>
|
||||
*/
|
||||
package akka.japi
|
||||
|
||||
import scala.util.control.NonFatal
|
||||
|
||||
/**
|
||||
* Helper class for determining whether a `Throwable` is fatal or not.
|
||||
* User should only catch the non-fatal one,and keep rethrow the fatal one.
|
||||
*
|
||||
* Fatal errors are errors like `VirtualMachineError`
|
||||
* (for example, `OutOfMemoryError` and `StackOverflowError`, subclasses of `VirtualMachineError`), `ThreadDeath`,
|
||||
* `LinkageError`, `InterruptedException`, `ControlThrowable`.
|
||||
*
|
||||
* Note. this helper keep the same semantic with `NonFatal` in Scala.
|
||||
* For example, all harmless `Throwable`s can be caught by:
|
||||
* {{{
|
||||
* try {
|
||||
* // dangerous stuff
|
||||
* } catch(Throwable e) {
|
||||
* if (Throwables.isNonFatal(e)){
|
||||
* log.error(e, "Something not that bad.");
|
||||
* } else {
|
||||
* throw e;
|
||||
* }
|
||||
* }}}
|
||||
*/
|
||||
object Throwables {
|
||||
/**
|
||||
* Returns true if the provided `Throwable` is to be considered non-fatal,
|
||||
* or false if it is to be considered fatal
|
||||
*/
|
||||
def isNonFatal(throwable: Throwable): Boolean = NonFatal(throwable)
|
||||
|
||||
/**
|
||||
* Returns true if the provided `Throwable` is to be considered fatal,
|
||||
* or false if it is to be considered non-fatal
|
||||
*/
|
||||
def isFatal(throwable: Throwable): Boolean = !isNonFatal(throwable)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue