mirror of
https://github.com/digital-asset/daml.git
synced 2024-09-19 16:57:40 +03:00
ErrorCodesVersionSwitcher -> ValueSwitch and push it down to /ledger/error also for KV to use (#11207)
CHANGELOG_BEGIN CHANGELOG_END
This commit is contained in:
parent
04494136bd
commit
652d56999b
20
ledger/error/src/main/scala/com/daml/error/ValueSwitch.scala
Normal file
20
ledger/error/src/main/scala/com/daml/error/ValueSwitch.scala
Normal 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
|
||||
}
|
||||
}
|
@ -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)
|
||||
}
|
@ -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)
|
||||
|
@ -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
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user