ErrorCodesVersionSwitcher -> ValueSwitch and push it down to /ledger/error also for KV to use (#11207)

CHANGELOG_BEGIN
CHANGELOG_END
This commit is contained in:
fabiotudone-da 2021-10-12 16:18:23 +02:00 committed by GitHub
parent 04494136bd
commit 652d56999b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 68 additions and 56 deletions

View File

@ -0,0 +1,20 @@
// Copyright (c) 2021 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
package com.daml.error
/** A mechanism to switch between the legacy error codes (v1) and the new self-service error codes (v2).
* 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(
v1: => X,
v2: => X,
): X =
if (enableSelfServiceErrorCodes) {
v2
} else {
v1
}
}

View File

@ -0,0 +1,45 @@
// Copyright (c) 2021 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
package com.daml.error
import io.grpc.{Status, StatusRuntimeException}
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers
class ValueSwitchSpec extends AnyFlatSpec with Matchers {
behavior of classOf[ValueSwitch[_]].getSimpleName
it should "use self-service (v2) error codes" in {
// given
val tested = new ValueSwitch[StatusRuntimeException](enableSelfServiceErrorCodes = true)
// when
val actual =
tested.choose(
v1 = fail("This argument should be evaluated lazily!"),
v2 = aStatusRuntimeException,
)
// then
actual shouldBe aStatusRuntimeException
}
it should "use legacy (v1) error codes" in {
// given
val tested = new ValueSwitch[StatusRuntimeException](enableSelfServiceErrorCodes = false)
// when
val actual =
tested.choose(
v1 = aStatusRuntimeException,
v2 = fail("This argument should be evaluated lazily!"),
)
// then
actual shouldBe aStatusRuntimeException
}
private lazy val aStatusRuntimeException = new StatusRuntimeException(Status.INTERNAL)
}

View File

@ -3,21 +3,8 @@
package com.daml.platform.apiserver
import com.daml.error.ValueSwitch
import io.grpc.StatusRuntimeException
/** A mechanism to switch between the legacy error codes (v1) and the new self-service error codes (v2).
* This class is intended to facilitate transition to self-service error codes.
* Once the previous error codes are removed, this class should be dropped as well.
*/
final class ErrorCodesVersionSwitcher(enableSelfServiceErrorCodes: Boolean) {
def choose(
v1: => StatusRuntimeException,
v2: => StatusRuntimeException,
): StatusRuntimeException = {
if (enableSelfServiceErrorCodes) {
v2
} else {
v1
}
}
}
final class ErrorCodesVersionSwitcher(enableSelfServiceErrorCodes: Boolean)
extends ValueSwitch[StatusRuntimeException](enableSelfServiceErrorCodes)

View File

@ -1,40 +0,0 @@
// Copyright (c) 2021 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
package com.daml.platform.apiserver
import io.grpc.{Status, StatusRuntimeException}
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers
class ErrorCodesVersionSwitcherSpec extends AnyFlatSpec with Matchers {
behavior of classOf[ErrorCodesVersionSwitcher].getSimpleName
it should "use self-service (v2) error codes" in {
// given
val tested = new ErrorCodesVersionSwitcher(enableSelfServiceErrorCodes = true)
val expected = new StatusRuntimeException(Status.INTERNAL)
// when
val actual =
tested.choose(v1 = fail("This argument should be evaluated lazily!"), v2 = expected)
// then
actual shouldBe expected
}
it should "use legacy (v1) error codes" in {
// given
val tested = new ErrorCodesVersionSwitcher(enableSelfServiceErrorCodes = false)
val expected = new StatusRuntimeException(Status.INTERNAL)
// when
val actual =
tested.choose(v1 = expected, v2 = fail("This argument should be evaluated lazily!"))
// then
actual shouldBe expected
}
}