Add tests and use ErrorDetails.matches (#13139)

changelog_begin
changelog_end
This commit is contained in:
pbatko-da 2022-03-03 09:33:37 +01:00 committed by GitHub
parent 6fd6fefce7
commit a84c2cd68f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 56 additions and 9 deletions

View File

@ -68,7 +68,7 @@ object ErrorDetails {
case ErrorInfoDetail(errorCodeId, _) => errorCodeId == errorCode.id case ErrorInfoDetail(errorCodeId, _) => errorCodeId == errorCode.id
case _ => false case _ => false
} }
val matchesMessagePrefix = e.getStatus.getDescription.startsWith(errorCode.id) val matchesMessagePrefix = Option(e.getStatus.getDescription).exists(_.startsWith(errorCode.id))
val matchesStatusCode = errorCode.category.grpcCode.contains(e.getStatus.getCode) val matchesStatusCode = errorCode.category.grpcCode.contains(e.getStatus.getCode)
matchesErrorCodeId && matchesMessagePrefix && matchesStatusCode matchesErrorCodeId && matchesMessagePrefix && matchesStatusCode
} }

View File

@ -0,0 +1,51 @@
// Copyright (c) 2022 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
package com.daml.error.utils
import com.daml.error.ErrorCategory.BackgroundProcessDegradationWarning
import com.daml.error.definitions.LedgerApiErrors
import com.daml.error.{DamlContextualizedErrorLogger, ErrorClass, ErrorCode}
import io.grpc.{Status, StatusRuntimeException}
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers
class ErrorDetailsSpec extends AnyFlatSpec with Matchers {
private val errorLogger = DamlContextualizedErrorLogger.forTesting(getClass)
behavior of classOf[ErrorDetails.type].getName
it should "correctly match exception to error codes " in {
val securitySensitive =
LedgerApiErrors.AuthorizationChecks.Unauthenticated.MissingJwtToken()(errorLogger).asGrpcError
val notSecuritySensitive = LedgerApiErrors.AdminServices.UserNotFound
.Reject(_operation = "operation123", userId = "userId123")(errorLogger)
.asGrpcError
ErrorDetails.matches(
securitySensitive,
LedgerApiErrors.AuthorizationChecks.Unauthenticated,
) shouldBe false
ErrorDetails.matches(
notSecuritySensitive,
LedgerApiErrors.AdminServices.UserNotFound,
) shouldBe true
ErrorDetails.matches(
new StatusRuntimeException(Status.ABORTED),
LedgerApiErrors.AdminServices.UserNotFound,
) shouldBe false
ErrorDetails.matches(new Exception, LedgerApiErrors.AdminServices.UserNotFound) shouldBe false
object NonGrpcErrorCode
extends ErrorCode(
id = "NON_GRPC_ERROR_CODE_123",
BackgroundProcessDegradationWarning,
)(ErrorClass.root())
NonGrpcErrorCode.category.grpcCode shouldBe empty
ErrorDetails.matches(
new StatusRuntimeException(Status.ABORTED),
NonGrpcErrorCode,
) shouldBe false
}
}

View File

@ -4,6 +4,7 @@
package com.daml.ledger.api.benchtool.services package com.daml.ledger.api.benchtool.services
import com.daml.error.definitions.LedgerApiErrors import com.daml.error.definitions.LedgerApiErrors
import com.daml.error.utils.ErrorDetails
import com.daml.ledger.api.benchtool.AuthorizationHelper import com.daml.ledger.api.benchtool.AuthorizationHelper
import com.daml.ledger.api.v1.admin.user_management_service.{ import com.daml.ledger.api.v1.admin.user_management_service.{
CreateUserRequest, CreateUserRequest,
@ -32,9 +33,7 @@ class UserManagementService(channel: Channel, authorizationToken: Option[String]
val rights = userRights(observerPartyNames, signatoryPartyName) val rights = userRights(observerPartyNames, signatoryPartyName)
createUser(userId, rights).recoverWith { createUser(userId, rights).recoverWith {
case e: StatusRuntimeException case e: StatusRuntimeException
if e.getStatus.getDescription.startsWith( if ErrorDetails.matches(e, LedgerApiErrors.AdminServices.UserAlreadyExists) =>
LedgerApiErrors.AdminServices.UserAlreadyExists.id
) =>
logger.info( logger.info(
s"Benchmark user already exists (received error: ${e.getStatus.getDescription}) so granting rights the existing user." s"Benchmark user already exists (received error: ${e.getStatus.getDescription}) so granting rights the existing user."
) )

View File

@ -6,6 +6,7 @@ package com.daml.ledger.api.testtool.infrastructure.participant
import java.util.concurrent.ConcurrentHashMap import java.util.concurrent.ConcurrentHashMap
import com.daml.error.definitions.LedgerApiErrors import com.daml.error.definitions.LedgerApiErrors
import com.daml.error.utils.ErrorDetails
import com.daml.ledger.api.testtool.infrastructure.LedgerServices import com.daml.ledger.api.testtool.infrastructure.LedgerServices
import com.daml.ledger.api.v1.admin.user_management_service.UserManagementServiceGrpc.UserManagementService import com.daml.ledger.api.v1.admin.user_management_service.UserManagementServiceGrpc.UserManagementService
import com.daml.ledger.api.v1.admin.user_management_service.{ import com.daml.ledger.api.v1.admin.user_management_service.{
@ -15,7 +16,6 @@ import com.daml.ledger.api.v1.admin.user_management_service.{
DeleteUserResponse, DeleteUserResponse,
User, User,
} }
import io.grpc.StatusRuntimeException
import scala.concurrent.{ExecutionContext, Future} import scala.concurrent.{ExecutionContext, Future}
@ -70,10 +70,7 @@ trait UserManagementTestContext {
) )
.map(_ => ()) .map(_ => ())
.recover { .recover {
case e: StatusRuntimeException case e if ErrorDetails.matches(e, LedgerApiErrors.AdminServices.UserNotFound) =>
if e.getStatus.getDescription.startsWith(
LedgerApiErrors.AdminServices.UserNotFound.id
) =>
() ()
} }
) )