Skip to content

Commit

Permalink
[Self-service error codes] ValueSwitch parameterized method
Browse files Browse the repository at this point in the history
CHANGELOG_BEGIN
CHANGELOG_END
  • Loading branch information
tudor-da committed Nov 8, 2021
1 parent 0823601 commit 936eb0e
Show file tree
Hide file tree
Showing 13 changed files with 49 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import io.grpc.StatusRuntimeException
import scala.concurrent.Future

class ErrorCodesVersionSwitcher(enableSelfServiceErrorCodes: Boolean)
extends ValueSwitch[StatusRuntimeException](enableSelfServiceErrorCodes) {
extends ValueSwitch(enableSelfServiceErrorCodes) {

def chooseAsFailedFuture[T](
v1: => StatusRuntimeException,
Expand Down
4 changes: 2 additions & 2 deletions ledger/error/src/main/scala/com/daml/error/ValueSwitch.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ package com.daml.error
* This class is intended to facilitate transition to self-service error codes.
* Once the previous error codes are removed, this class and its specializations should be dropped as well.
*/
class ValueSwitch[X](enableSelfServiceErrorCodes: Boolean) {
def choose(
class ValueSwitch(enableSelfServiceErrorCodes: Boolean) {
def choose[X](
v1: => X,
v2: => X,
): X =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import org.scalatest.matchers.should.Matchers

class ValueSwitchSpec extends AnyFlatSpec with Matchers {

behavior of classOf[ValueSwitch[_]].getSimpleName
behavior of classOf[ValueSwitch].getSimpleName

it should "use self-service (v2) error codes" in {
// given
val tested = new ValueSwitch[StatusRuntimeException](enableSelfServiceErrorCodes = true)
val tested = new ValueSwitch(enableSelfServiceErrorCodes = true)

// when
val actual =
Expand All @@ -28,7 +28,7 @@ class ValueSwitchSpec extends AnyFlatSpec with Matchers {

it should "use legacy (v1) error codes" in {
// given
val tested = new ValueSwitch[StatusRuntimeException](enableSelfServiceErrorCodes = false)
val tested = new ValueSwitch(enableSelfServiceErrorCodes = false)

// when
val actual =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -346,10 +346,7 @@ final class GrpcHealthServiceSpec
// Negative tests returning errors should explicitly instantiate it
private def errorCodesVersionSwitcherMock: ErrorCodesVersionSwitcher =
new ErrorCodesVersionSwitcher(false) {
override def choose(
v1: => StatusRuntimeException,
v2: => StatusRuntimeException,
): StatusRuntimeException = {
override def choose[X](v1: => X, v2: => X): X = {
val _ = (v1, v2)
fail("Should not be called")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ import com.daml.lf.{crypto, data}

import com.fasterxml.jackson.databind.ObjectMapper
import com.google.protobuf.Empty
import com.google.rpc.status.Status

import scala.annotation.nowarn
import scala.collection.mutable
Expand Down Expand Up @@ -473,7 +472,7 @@ private[state] object Conversions {
@nowarn("msg=deprecated")
def decodeTransactionRejectionEntry(
entry: DamlTransactionRejectionEntry,
errorVersionSwitch: ValueSwitch[Status],
errorVersionSwitch: ValueSwitch,
)(implicit loggingContext: ContextualizedErrorLogger): FinalReason =
FinalReason(entry.getReasonCase match {
case DamlTransactionRejectionEntry.ReasonCase.INVALID_LEDGER_TIME =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import com.daml.logging.{ContextualizedLogger, LoggingContext}

import com.google.common.io.BaseEncoding
import com.google.protobuf.ByteString
import com.google.rpc.status.Status

import scala.jdk.CollectionConverters._

Expand Down Expand Up @@ -59,7 +58,7 @@ object KeyValueConsumption {
def logEntryToUpdate(
entryId: DamlLogEntryId,
entry: DamlLogEntry,
errorVersionSwitch: ValueSwitch[Status],
errorVersionSwitch: ValueSwitch,
recordTimeForUpdate: Option[Timestamp] = None,
)(loggingContext: LoggingContext): List[Update] = {
implicit val logContext: LoggingContext = loggingContext
Expand Down Expand Up @@ -250,7 +249,7 @@ object KeyValueConsumption {
private def transactionRejectionEntryToUpdate(
recordTime: Timestamp,
rejEntry: DamlTransactionRejectionEntry,
errorVersionSwitch: ValueSwitch[Status],
errorVersionSwitch: ValueSwitch,
)(implicit loggingContext: ContextualizedErrorLogger): Update = {
val reason = Conversions.decodeTransactionRejectionEntry(rejEntry, errorVersionSwitch)
Update.CommandRejected(
Expand Down Expand Up @@ -343,7 +342,7 @@ object KeyValueConsumption {
private[kvutils] def outOfTimeBoundsEntryToUpdate(
recordTime: Timestamp,
outOfTimeBoundsEntry: DamlOutOfTimeBoundsEntry,
errorVersionSwitch: ValueSwitch[Status],
errorVersionSwitch: ValueSwitch,
)(implicit loggingContext: LoggingContext): Option[Update] = {
val timeBounds = parseTimeBounds(outOfTimeBoundsEntry)
val deduplicated = timeBounds.deduplicateUntil.exists(recordTime <= _)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import com.daml.lf.data.Time
import com.daml.lf.data.Time.Timestamp
import com.daml.logging.LoggingContext
import com.daml.metrics.{Metrics, Timed}
import com.google.rpc.status.Status

/** Adapts a [[LedgerReader]] instance to [[ReadService]].
* Performs translation between the offsets required by the underlying reader and [[ReadService]]:
Expand All @@ -34,7 +33,7 @@ class KeyValueParticipantStateReader private[api] (
logEntryToUpdate: (
DamlLogEntryId,
DamlLogEntry,
ValueSwitch[Status],
ValueSwitch,
Option[Timestamp],
) => LoggingContext => List[Update],
timeUpdatesProvider: TimeUpdatesProvider,
Expand All @@ -43,7 +42,7 @@ class KeyValueParticipantStateReader private[api] (

import KeyValueParticipantStateReader._

private val errorVersionSwitch = new ValueSwitch[Status](enableSelfServiceErrorCodes)
private val errorVersionSwitch = new ValueSwitch(enableSelfServiceErrorCodes)

override def ledgerInitialConditions(): Source[LedgerInitialConditions, NotUsed] =
Source.single(createLedgerInitialConditions())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ private[kvutils] object TransactionRejections {
tooEarlyUntil: Option[Timestamp],
tooLateFrom: Option[Timestamp],
rejectionEntry: DamlTransactionRejectionEntry,
errorVersionSwitch: ValueSwitch[Status],
errorVersionSwitch: ValueSwitch,
)(implicit loggingContext: ContextualizedErrorLogger): Update.CommandRejected = {
Update.CommandRejected(
recordTime = recordTime,
Expand Down Expand Up @@ -69,7 +69,7 @@ private[kvutils] object TransactionRejections {
def duplicateCommandsRejectionUpdate(
recordTime: Timestamp,
rejectionEntry: DamlTransactionRejectionEntry,
errorVersionSwitch: ValueSwitch[Status],
errorVersionSwitch: ValueSwitch,
)(implicit loggingContext: ContextualizedErrorLogger): Update.CommandRejected = {
val definiteAnswer = rejectionEntry.getDefiniteAnswer
Update.CommandRejected(
Expand All @@ -90,7 +90,7 @@ private[kvutils] object TransactionRejections {
@nowarn("msg=deprecated")
def rejectionReasonNotSetStatus(
entry: DamlTransactionRejectionEntry,
errorVersionSwitch: ValueSwitch[Status],
errorVersionSwitch: ValueSwitch,
)(implicit loggingContext: ContextualizedErrorLogger): Status =
errorVersionSwitch.choose(
V1.status(entry, Code.UNKNOWN, "No reason set for rejection"),
Expand All @@ -101,7 +101,7 @@ private[kvutils] object TransactionRejections {
def invalidParticipantStateStatus(
entry: DamlTransactionRejectionEntry,
rejection: InvalidParticipantState,
errorVersionSwitch: ValueSwitch[Status],
errorVersionSwitch: ValueSwitch,
)(implicit loggingContext: ContextualizedErrorLogger): Status = {
val details = rejection.getDetails
val metadata = rejection.getMetadataMap.asScala.toMap
Expand All @@ -120,7 +120,7 @@ private[kvutils] object TransactionRejections {
def partiesNotKnownOnLedgerStatus(
entry: DamlTransactionRejectionEntry,
rejection: PartiesNotKnownOnLedger,
errorVersionSwitch: ValueSwitch[Status],
errorVersionSwitch: ValueSwitch,
)(implicit loggingContext: ContextualizedErrorLogger): Status = {
val parties = rejection.getPartiesList
errorVersionSwitch.choose(
Expand All @@ -138,7 +138,7 @@ private[kvutils] object TransactionRejections {
def submittingPartyNotKnownOnLedgerStatus(
entry: DamlTransactionRejectionEntry,
rejection: SubmittingPartyNotKnownOnLedger,
errorVersionSwitch: ValueSwitch[Status],
errorVersionSwitch: ValueSwitch,
)(implicit loggingContext: ContextualizedErrorLogger): Status = {
val submitter = rejection.getSubmitterParty
errorVersionSwitch.choose(
Expand All @@ -155,7 +155,7 @@ private[kvutils] object TransactionRejections {
@nowarn("msg=deprecated")
def causalMonotonicityViolatedStatus(
entry: DamlTransactionRejectionEntry,
errorVersionSwitch: ValueSwitch[Status],
errorVersionSwitch: ValueSwitch,
)(implicit loggingContext: ContextualizedErrorLogger): Status =
errorVersionSwitch.choose(
V1.status(
Expand All @@ -170,7 +170,7 @@ private[kvutils] object TransactionRejections {
def recordTimeOutOfRangeStatus(
entry: DamlTransactionRejectionEntry,
rejection: RecordTimeOutOfRange,
errorVersionSwitch: ValueSwitch[Status],
errorVersionSwitch: ValueSwitch,
)(implicit loggingContext: ContextualizedErrorLogger): Status = {
val minRecordTime = Instant
.ofEpochSecond(
Expand Down Expand Up @@ -199,7 +199,7 @@ private[kvutils] object TransactionRejections {
@nowarn("msg=deprecated")
def externallyDuplicateKeysStatus(
entry: DamlTransactionRejectionEntry,
errorVersionSwitch: ValueSwitch[Status],
errorVersionSwitch: ValueSwitch,
)(implicit loggingContext: ContextualizedErrorLogger): Status =
errorVersionSwitch.choose(
V1.status(
Expand All @@ -213,7 +213,7 @@ private[kvutils] object TransactionRejections {
@nowarn("msg=deprecated")
def externallyInconsistentKeysStatus(
entry: DamlTransactionRejectionEntry,
errorVersionSwitch: ValueSwitch[Status],
errorVersionSwitch: ValueSwitch,
)(implicit loggingContext: ContextualizedErrorLogger): Status =
errorVersionSwitch.choose(
V1.status(
Expand All @@ -227,7 +227,7 @@ private[kvutils] object TransactionRejections {
@nowarn("msg=deprecated")
def externallyInconsistentContractsStatus(
entry: DamlTransactionRejectionEntry,
errorVersionSwitch: ValueSwitch[Status],
errorVersionSwitch: ValueSwitch,
)(implicit loggingContext: ContextualizedErrorLogger): Status =
errorVersionSwitch.choose(
V1.status(
Expand All @@ -242,7 +242,7 @@ private[kvutils] object TransactionRejections {
def missingInputStateStatus(
entry: DamlTransactionRejectionEntry,
rejection: MissingInputState,
errorVersionSwitch: ValueSwitch[Status],
errorVersionSwitch: ValueSwitch,
)(implicit loggingContext: ContextualizedErrorLogger): Status = {
val key = rejection.getKey.toString
errorVersionSwitch.choose(
Expand All @@ -259,7 +259,7 @@ private[kvutils] object TransactionRejections {
@nowarn("msg=deprecated")
def internallyInconsistentKeysStatus(
entry: DamlTransactionRejectionEntry,
errorVersionSwitch: ValueSwitch[Status],
errorVersionSwitch: ValueSwitch,
)(implicit loggingContext: ContextualizedErrorLogger): Status =
errorVersionSwitch.choose(
V1.status(
Expand All @@ -273,7 +273,7 @@ private[kvutils] object TransactionRejections {
@nowarn("msg=deprecated")
def internallyDuplicateKeysStatus(
entry: DamlTransactionRejectionEntry,
errorVersionSwitch: ValueSwitch[Status],
errorVersionSwitch: ValueSwitch,
)(implicit loggingContext: ContextualizedErrorLogger): Status =
errorVersionSwitch.choose(
V1.status(
Expand All @@ -288,7 +288,7 @@ private[kvutils] object TransactionRejections {
def validationFailureStatus(
entry: DamlTransactionRejectionEntry,
rejection: ValidationFailure,
errorVersionSwitch: ValueSwitch[Status],
errorVersionSwitch: ValueSwitch,
)(implicit loggingContext: ContextualizedErrorLogger): Status = {
val details = rejection.getDetails
errorVersionSwitch.choose(
Expand All @@ -300,7 +300,7 @@ private[kvutils] object TransactionRejections {
@nowarn("msg=deprecated")
def duplicateCommandStatus(
entry: DamlTransactionRejectionEntry,
errorVersionSwitch: ValueSwitch[Status],
errorVersionSwitch: ValueSwitch,
)(implicit loggingContext: ContextualizedErrorLogger): Status =
errorVersionSwitch.choose(
V1.status(
Expand All @@ -315,7 +315,7 @@ private[kvutils] object TransactionRejections {
def submitterCannotActViaParticipantStatus(
entry: DamlTransactionRejectionEntry,
rejection: SubmitterCannotActViaParticipant,
errorVersionSwitch: ValueSwitch[Status],
errorVersionSwitch: ValueSwitch,
)(implicit loggingContext: ContextualizedErrorLogger): Status = {
val submitter = rejection.getSubmitterParty
val participantId = rejection.getParticipantId
Expand All @@ -338,7 +338,7 @@ private[kvutils] object TransactionRejections {
def invalidLedgerTimeStatus(
entry: DamlTransactionRejectionEntry,
rejection: InvalidLedgerTime,
errorVersionSwitch: ValueSwitch[Status],
errorVersionSwitch: ValueSwitch,
)(implicit loggingContext: ContextualizedErrorLogger): Status = {
val details = rejection.getDetails
val ledgerTime = rejection.getLedgerTime
Expand Down Expand Up @@ -381,7 +381,7 @@ private[kvutils] object TransactionRejections {
def resourceExhaustedStatus(
entry: DamlTransactionRejectionEntry,
rejection: ResourcesExhausted,
errorVersionSwitch: ValueSwitch[Status],
errorVersionSwitch: ValueSwitch,
)(implicit loggingContext: ContextualizedErrorLogger): Status = {
val details = rejection.getDetails
errorVersionSwitch.choose(
Expand All @@ -398,7 +398,7 @@ private[kvutils] object TransactionRejections {
def partyNotKnownOnLedgerStatus(
entry: DamlTransactionRejectionEntry,
rejection: PartyNotKnownOnLedger,
errorVersionSwitch: ValueSwitch[Status],
errorVersionSwitch: ValueSwitch,
)(implicit loggingContext: ContextualizedErrorLogger): Status = {
val details = rejection.getDetails
errorVersionSwitch.choose(
Expand All @@ -415,7 +415,7 @@ private[kvutils] object TransactionRejections {
def inconsistentStatus(
entry: DamlTransactionRejectionEntry,
rejection: Inconsistent,
errorVersionSwitch: ValueSwitch[Status],
errorVersionSwitch: ValueSwitch,
)(implicit loggingContext: ContextualizedErrorLogger): Status = {
val details = rejection.getDetails
errorVersionSwitch.choose(
Expand All @@ -432,7 +432,7 @@ private[kvutils] object TransactionRejections {
def disputedStatus(
entry: DamlTransactionRejectionEntry,
rejection: Disputed,
errorVersionSwitch: ValueSwitch[Status],
errorVersionSwitch: ValueSwitch,
)(implicit loggingContext: ContextualizedErrorLogger): Status = {
val details = rejection.getDetails
errorVersionSwitch.choose(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ object KVTest {

private[kvutils] val metrics = new Metrics(new MetricRegistry)
private[kvutils] val errorVersionSwitch =
new ValueSwitch[com.google.rpc.status.Status](enableSelfServiceErrorCodes = false)
new ValueSwitch(enableSelfServiceErrorCodes = false)

private def initialTestState: KVTestState = {
val engine = Engine.DevEngine()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,7 @@ class ConversionsSpec extends AnyWordSpec with Matchers with OptionValues {
}

private def checkErrors(
errorVersionSwitch: ValueSwitch[Status],
errorVersionSwitch: ValueSwitch,
submitterInfo: DamlSubmitterInfo,
rejection: Rejection,
expectedCode: Code,
Expand Down Expand Up @@ -638,8 +638,8 @@ class ConversionsSpec extends AnyWordSpec with Matchers with OptionValues {
)
.build

private lazy val v1ErrorSwitch = new ValueSwitch[Status](enableSelfServiceErrorCodes = false)
private lazy val v2ErrorSwitch = new ValueSwitch[Status](enableSelfServiceErrorCodes = true)
private lazy val v1ErrorSwitch = new ValueSwitch(enableSelfServiceErrorCodes = false)
private lazy val v2ErrorSwitch = new ValueSwitch(enableSelfServiceErrorCodes = true)

private[this] val txVersion = TransactionVersion.StableVersions.max

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class KVUtilsTransactionSpec extends AnyWordSpec with Matchers with Inside {
private implicit val loggingContext: LoggingContext = LoggingContext.ForTesting

private val errorVersionSwitch =
new ValueSwitch[com.google.rpc.status.Status](enableSelfServiceErrorCodes = false)
new ValueSwitch(enableSelfServiceErrorCodes = false)

private val alice = party("Alice")
private val bob = party("Bob")
Expand Down
Loading

0 comments on commit 936eb0e

Please sign in to comment.