Skip to content

Commit

Permalink
Fix ClassNotFound handling (corda#5078)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tudor Malene authored and shamsasari committed May 2, 2019
1 parent 268f6db commit 25f3358
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import java.util.regex.Pattern
import kotlin.collections.ArrayList
import kotlin.collections.component1
import kotlin.collections.component2
import kotlin.reflect.KClass

/**
* A TransactionBuilder is a transaction class that's mutable (unlike the others which are all immutable). It is
Expand Down Expand Up @@ -169,6 +170,13 @@ open class TransactionBuilder(
wireTx
}

// Returns the first exception in the hierarchy that matches one of the [types].
private tailrec fun Throwable.rootClassNotFoundCause(vararg types: KClass<*>): Throwable = when {
this::class in types -> this
this.cause == null -> this
else -> this.cause!!.rootClassNotFoundCause(*types)
}

/**
* @return true if a new dependency was successfully added.
*/
Expand All @@ -178,7 +186,8 @@ open class TransactionBuilder(
// The transaction verified successfully without adding any extra dependency.
false
} catch (e: Throwable) {
val rootError = e.rootCause
val rootError = e.rootClassNotFoundCause(ClassNotFoundException::class, NoClassDefFoundError::class)

when {
// Handle various exceptions that can be thrown during verification and drill down the wrappings.
// Note: this is a best effort to preserve backwards compatibility.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ internal inline fun <T> ifThrowsAppend(strToAppendFn: () -> String, block: () ->
try {
return block()
} catch (th: Throwable) {
if (th is AMQPNotSerializableException) {
th.classHierarchy.add(strToAppendFn())
} else {
th.setMessage("${strToAppendFn()} -> ${th.message}")
when (th) {
is AMQPNotSerializableException -> th.classHierarchy.add(strToAppendFn())
// Do not overwrite the message of these exceptions as it may be used.
is ClassNotFoundException -> {}
is NoClassDefFoundError -> {}
else -> th.setMessage("${strToAppendFn()} -> ${th.message}")
}
throw th
}
Expand Down

0 comments on commit 25f3358

Please sign in to comment.