Custom exceptions in corda, should either derive from an appropriate closely related java exception, or CordaException, or CordaRuntimeException. They should not inherit just from Exception, or RuntimeException.

Handle PR comments

Add nicer constructors to CordaException and CordaRuntimeException

(cherry picked from commit 89478c8)

Fix ambiguous defaulted constructor

(cherry picked from commit ec9bafe)

Address PR comment

Update a few more custom exceptions
This commit is contained in:
Matthew Nesbit
2017-09-28 13:25:08 +01:00
parent d13bf77473
commit 5fa7381883
24 changed files with 68 additions and 44 deletions

View File

@ -6,6 +6,7 @@ import com.google.common.collect.MutableClassToInstanceMap
import com.google.common.util.concurrent.MoreExecutors
import net.corda.confidential.SwapIdentitiesFlow
import net.corda.confidential.SwapIdentitiesHandler
import net.corda.core.CordaException
import net.corda.core.concurrent.CordaFuture
import net.corda.core.cordapp.CordappProvider
import net.corda.core.flows.*
@ -231,7 +232,7 @@ abstract class AbstractNode(open val configuration: NodeConfiguration,
}
}
private class ServiceInstantiationException(cause: Throwable?) : Exception(cause)
private class ServiceInstantiationException(cause: Throwable?) : CordaException("Service Instantiation Error", cause)
private fun installCordaServices() {
cordappProvider.cordapps.flatMap { it.services }.forEach {
@ -465,7 +466,7 @@ abstract class AbstractNode(open val configuration: NodeConfiguration,
}
// Specific class so that MockNode can catch it.
class DatabaseConfigurationException(msg: String) : Exception(msg)
class DatabaseConfigurationException(msg: String) : CordaException(msg)
protected open fun <T> initialiseDatabasePersistence(insideTransaction: () -> T): T {
val props = configuration.dataSourceProperties

View File

@ -1,6 +1,7 @@
package net.corda.node.internal
import com.codahale.metrics.JmxReporter
import net.corda.core.CordaException
import net.corda.core.concurrent.CordaFuture
import net.corda.core.identity.CordaX500Name
import net.corda.core.identity.PartyAndCertificate
@ -384,6 +385,6 @@ open class Node(override val configuration: FullNodeConfiguration,
}
}
class ConfigurationException(message: String) : Exception(message)
class ConfigurationException(message: String) : CordaException(message)
data class NetworkMapInfo(val address: NetworkHostAndPort, val legalName: CordaX500Name)

View File

@ -1,5 +1,6 @@
package net.corda.node.services.api
import net.corda.core.CordaException
import net.corda.core.concurrent.CordaFuture
import net.corda.core.crypto.SecureHash
import net.corda.core.flows.FlowInitiator
@ -62,9 +63,9 @@ interface NetworkMapCacheInternal : NetworkMapCache {
}
@CordaSerializable
sealed class NetworkCacheError : Exception() {
sealed class NetworkCacheException : CordaException("Network Cache Error") {
/** Indicates a failure to deregister, because of a rejected request from the remote node */
class DeregistrationFailed : NetworkCacheError()
class DeregistrationFailed : NetworkCacheException()
}
interface ServiceHubInternal : ServiceHub {

View File

@ -1,5 +1,6 @@
package net.corda.node.services.network
import net.corda.core.CordaException
import net.corda.core.crypto.DigitalSignature
import net.corda.core.crypto.SignedData
import net.corda.core.crypto.isFulfilledBy
@ -189,7 +190,7 @@ abstract class AbstractNetworkMapService(services: ServiceHubInternal,
}
private fun addSubscriber(subscriber: MessageRecipients) {
if (subscriber !is SingleMessageRecipient) throw NodeMapError.InvalidSubscriber()
if (subscriber !is SingleMessageRecipient) throw NodeMapException.InvalidSubscriber()
subscribers.locked {
if (!containsKey(subscriber)) {
put(subscriber, LastAcknowledgeInfo(mapVersion))
@ -198,12 +199,12 @@ abstract class AbstractNetworkMapService(services: ServiceHubInternal,
}
private fun removeSubscriber(subscriber: MessageRecipients) {
if (subscriber !is SingleMessageRecipient) throw NodeMapError.InvalidSubscriber()
if (subscriber !is SingleMessageRecipient) throw NodeMapException.InvalidSubscriber()
subscribers.locked { remove(subscriber) }
}
private fun processAcknowledge(request: UpdateAcknowledge): Unit {
if (request.replyTo !is SingleMessageRecipient) throw NodeMapError.InvalidSubscriber()
if (request.replyTo !is SingleMessageRecipient) throw NodeMapException.InvalidSubscriber()
subscribers.locked {
val lastVersionAcked = this[request.replyTo]?.mapVersion
if ((lastVersionAcked ?: 0) < request.mapVersion) {
@ -360,13 +361,13 @@ class WireNodeRegistration(raw: SerializedBytes<NodeRegistration>, sig: DigitalS
}
@CordaSerializable
sealed class NodeMapError : Exception() {
sealed class NodeMapException : CordaException("Network Map Protocol Error") {
/** Thrown if the signature on the node info does not match the public key for the identity */
class InvalidSignature : NodeMapError()
class InvalidSignature : NodeMapException()
/** Thrown if the replyTo of a subscription change message is not a single message recipient */
class InvalidSubscriber : NodeMapError()
class InvalidSubscriber : NodeMapException()
}
@CordaSerializable

View File

@ -21,7 +21,7 @@ import net.corda.core.utilities.NetworkHostAndPort
import net.corda.core.utilities.loggerFor
import net.corda.core.utilities.parsePublicKeyBase58
import net.corda.core.utilities.toBase58String
import net.corda.node.services.api.NetworkCacheError
import net.corda.node.services.api.NetworkCacheException
import net.corda.node.services.api.NetworkMapCacheInternal
import net.corda.node.services.api.ServiceHubInternal
import net.corda.node.services.messaging.MessagingService
@ -135,7 +135,7 @@ open class PersistentNetworkMapCache(private val serviceHub: ServiceHubInternal)
data = NetworkMapService.UpdateAcknowledge(req.mapVersion, network.myAddress).serialize().bytes)
network.send(ackMessage, req.replyTo)
processUpdatePush(req)
} catch (e: NodeMapError) {
} catch (e: NodeMapException) {
logger.warn("Failure during node map update due to bad update: ${e.javaClass.name}")
} catch (e: Exception) {
logger.error("Exception processing update from network map service", e)
@ -202,7 +202,7 @@ open class PersistentNetworkMapCache(private val serviceHub: ServiceHubInternal)
val address = getPartyInfo(mapParty)?.let { network.getAddressOfParty(it) } ?:
throw IllegalArgumentException("Can't deregister for updates, don't know the party: $mapParty")
val future = network.sendRequest<SubscribeResponse>(NetworkMapService.SUBSCRIPTION_TOPIC, req, address).map {
if (it.confirmed) Unit else throw NetworkCacheError.DeregistrationFailed()
if (it.confirmed) Unit else throw NetworkCacheException.DeregistrationFailed()
}
_registrationFuture.captureLater(future.map { null })
return future
@ -213,7 +213,7 @@ open class PersistentNetworkMapCache(private val serviceHub: ServiceHubInternal)
val reg = req.wireReg.verified()
processRegistration(reg)
} catch (e: SignatureException) {
throw NodeMapError.InvalidSignature()
throw NodeMapException.InvalidSignature()
}
}

View File

@ -6,6 +6,7 @@ import com.google.common.hash.HashCode
import com.google.common.hash.Hashing
import com.google.common.hash.HashingInputStream
import com.google.common.io.CountingInputStream
import net.corda.core.CordaRuntimeException
import net.corda.core.internal.AbstractAttachment
import net.corda.core.contracts.Attachment
import net.corda.core.crypto.SecureHash
@ -58,7 +59,7 @@ class NodeAttachmentService(metrics: MetricRegistry) : AttachmentStorage, Single
}
@CordaSerializable
class HashMismatchException(val expected: SecureHash, val actual: SecureHash) : RuntimeException("File $expected hashed to $actual: corruption in attachment store?")
class HashMismatchException(val expected: SecureHash, val actual: SecureHash) : CordaRuntimeException("File $expected hashed to $actual: corruption in attachment store?")
/**
* Wraps a stream and hashes data as it is read: if the entire stream is consumed, then at the end the hash of

View File

@ -9,6 +9,7 @@ import com.codahale.metrics.Gauge
import com.esotericsoftware.kryo.KryoException
import com.google.common.collect.HashMultimap
import com.google.common.util.concurrent.MoreExecutors
import net.corda.core.CordaException
import net.corda.core.concurrent.CordaFuture
import net.corda.core.crypto.SecureHash
import net.corda.core.crypto.random63BitValue
@ -641,6 +642,6 @@ class StateMachineManager(val serviceHub: ServiceHubInternal,
}
}
class SessionRejectException(val rejectMessage: String, val logMessage: String) : Exception() {
class SessionRejectException(val rejectMessage: String, val logMessage: String) : CordaException(rejectMessage) {
constructor(message: String) : this(message, message)
}

View File

@ -23,6 +23,7 @@ import net.corda.core.messaging.StateMachineUpdate
import net.corda.core.utilities.loggerFor
import net.corda.client.jackson.JacksonSupport
import net.corda.client.jackson.StringToMethodCallParser
import net.corda.core.CordaException
import net.corda.node.internal.Node
import net.corda.node.internal.StartedNode
import net.corda.node.services.messaging.CURRENT_RPC_CONTEXT
@ -258,7 +259,7 @@ object InteractiveShell {
}
}
class NoApplicableConstructor(val errors: List<String>) : Exception() {
class NoApplicableConstructor(val errors: List<String>) : CordaException(this.toString()) {
override fun toString() = (listOf("No applicable constructor for flow. Problems were:") + errors).joinToString(System.lineSeparator())
}

View File

@ -1,5 +1,6 @@
package net.corda.node.utilities.registration
import net.corda.core.CordaException
import net.corda.core.serialization.CordaSerializable
import org.bouncycastle.pkcs.PKCS10CertificationRequest
import java.security.cert.Certificate
@ -14,4 +15,4 @@ interface NetworkRegistrationService {
}
@CordaSerializable
class CertificateRequestException(message: String) : Exception(message)
class CertificateRequestException(message: String) : CordaException(message)