From 592d7238fdd066d418ccc95c8569bd95130b756b Mon Sep 17 00:00:00 2001 From: Chris Rankin Date: Tue, 28 Apr 2020 17:02:37 +0100 Subject: [PATCH] CORDA-3685: Prevent ServiceHub.signInitialTransaction from throwing undeclared checked exceptions. (#6111) --- .../net/corda/core/internal/TransactionUtils.kt | 9 +++++++-- .../MissingAttachmentsRuntimeException.kt | 17 +++++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) create mode 100644 core/src/main/kotlin/net/corda/core/serialization/MissingAttachmentsRuntimeException.kt diff --git a/core/src/main/kotlin/net/corda/core/internal/TransactionUtils.kt b/core/src/main/kotlin/net/corda/core/internal/TransactionUtils.kt index dd6528cda3..15413f361b 100644 --- a/core/src/main/kotlin/net/corda/core/internal/TransactionUtils.kt +++ b/core/src/main/kotlin/net/corda/core/internal/TransactionUtils.kt @@ -77,7 +77,12 @@ fun deserialiseComponentGroup(componentGroups: List, try { factory.deserialize(component, clazz.java, context) } catch (e: MissingAttachmentsException) { - throw e + /** + * [ServiceHub.signInitialTransaction] forgets to declare that + * it may throw any checked exceptions. Wrap this one inside + * an unchecked version to avoid breaking Java CorDapps. + */ + throw MissingAttachmentsRuntimeException(e.ids, e.message, e) } catch (e: Exception) { throw TransactionDeserialisationException(groupEnum, internalIndex, e) } @@ -88,7 +93,7 @@ fun deserialiseComponentGroup(componentGroups: List, * Exception raised if an error was encountered while attempting to deserialise a component group in a transaction. */ class TransactionDeserialisationException(groupEnum: ComponentGroupEnum, index: Int, cause: Exception): - Exception("Failed to deserialise group $groupEnum at index $index in transaction: ${cause.message}", cause) + RuntimeException("Failed to deserialise group $groupEnum at index $index in transaction: ${cause.message}", cause) /** * Method to deserialise Commands from its two groups: diff --git a/core/src/main/kotlin/net/corda/core/serialization/MissingAttachmentsRuntimeException.kt b/core/src/main/kotlin/net/corda/core/serialization/MissingAttachmentsRuntimeException.kt new file mode 100644 index 0000000000..ddeeacc618 --- /dev/null +++ b/core/src/main/kotlin/net/corda/core/serialization/MissingAttachmentsRuntimeException.kt @@ -0,0 +1,17 @@ +package net.corda.core.serialization + +import net.corda.core.CordaRuntimeException +import net.corda.core.KeepForDJVM +import net.corda.core.node.services.AttachmentId + +@KeepForDJVM +@CordaSerializable +class MissingAttachmentsRuntimeException(val ids: List, message: String?, cause: Throwable?) + : CordaRuntimeException(message, cause) { + + @Suppress("unused") + constructor(ids: List, message: String?) : this(ids, message, null) + + @Suppress("unused") + constructor(ids: List) : this(ids, null, null) +}