Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tudor-da committed Nov 25, 2021
1 parent 32d1598 commit ed9bb70
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 30 deletions.
30 changes: 15 additions & 15 deletions ledger/error/src/main/scala/com/daml/error/ErrorCode.scala
Original file line number Diff line number Diff line change
Expand Up @@ -67,20 +67,20 @@ abstract class ErrorCode(val id: String, val category: ErrorCategory)(implicit
getStatusInfo(err)

// Provide error id and context via ErrorInfo
val errInfoBld = com.google.rpc.ErrorInfo.newBuilder()
if (!code.category.securitySensitive) {
contextMap.foreach { case (k, v) => errInfoBld.putMetadata(k, v) }
errInfoBld.setReason(id)

// TODO error codes: Resolve dependency and use constant
// val definiteAnswerKey = com.daml.ledger.grpc.GrpcStatuses.DefiniteAnswerKey
val definiteAnswerKey = "definite_answer"
err.definiteAnswerO.foreach { definiteAnswer =>
errInfoBld.putMetadata(definiteAnswerKey, definiteAnswer.toString)
}
}

val errInfo = com.google.protobuf.Any.pack(errInfoBld.build())
val maybeErrInfo =
if (!code.category.securitySensitive) {
val errInfoBld = com.google.rpc.ErrorInfo.newBuilder()
contextMap.foreach { case (k, v) => errInfoBld.putMetadata(k, v) }
errInfoBld.setReason(id)

// TODO error codes: Resolve dependency and use constant
// val definiteAnswerKey = com.daml.ledger.grpc.GrpcStatuses.DefiniteAnswerKey
val definiteAnswerKey = "definite_answer"
err.definiteAnswerO.foreach { definiteAnswer =>
errInfoBld.putMetadata(definiteAnswerKey, definiteAnswer.toString)
}
Some(com.google.protobuf.Any.pack(errInfoBld.build()))
} else None

// Build retry info
val retryInfo = err.retryable.map { ri =>
Expand Down Expand Up @@ -126,7 +126,7 @@ abstract class ErrorCode(val id: String, val category: ErrorCategory)(implicit
.setCode(codeGrpc.value())
.setMessage(message)

(Seq(errInfo) ++ retryInfo.toList ++ requestInfo.toList ++ resourceInfo)
(maybeErrInfo.toList ++ retryInfo.toList ++ requestInfo.toList ++ resourceInfo)
.foldLeft(statusBuilder) { case (acc, item) =>
acc.addDetails(item)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,27 @@ object ErrorDetails {
sealed trait ErrorDetail extends Product with Serializable

final case class ResourceInfoDetail(name: String, typ: String) extends ErrorDetail
final case class ErrorInfoDetail(reason: String, metadata: Map[String, String] = Map.empty)
final case class ErrorInfoDetail(reason: String, metadata: Map[String, String])
extends ErrorDetail
final case class RetryInfoDetail(retryDelayInSeconds: Long) extends ErrorDetail
final case class RequestInfoDetail(requestId: String) extends ErrorDetail

def from(anys: Seq[protobuf.Any]): Seq[ErrorDetail] = anys.toList.flatMap {
def from(anys: Seq[protobuf.Any]): Seq[ErrorDetail] = anys.toList.map {
case any if any.is(classOf[ResourceInfo]) =>
val v = any.unpack(classOf[ResourceInfo])
List(ResourceInfoDetail(v.getResourceType, v.getResourceName))
ResourceInfoDetail(v.getResourceType, v.getResourceName)

case any if any.is(classOf[ErrorInfo]) =>
val v = any.unpack(classOf[ErrorInfo])
val reason = v.getReason
if (reason.nonEmpty || v.getMetadataCount > 0)
List(ErrorInfoDetail(reason, v.getMetadataMap.asScala.toMap))
else Nil
ErrorInfoDetail(v.getReason, v.getMetadataMap.asScala.toMap)

case any if any.is(classOf[RetryInfo]) =>
val v = any.unpack(classOf[RetryInfo])
List(RetryInfoDetail(v.getRetryDelay.getSeconds))
RetryInfoDetail(v.getRetryDelay.getSeconds)

case any if any.is(classOf[RequestInfo]) =>
val v = any.unpack(classOf[RequestInfo])
List(RequestInfoDetail(v.getRequestId))
RequestInfoDetail(v.getRequestId)

case any => throw new IllegalStateException(s"Could not unpack value of: |$any|")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package com.daml.ledger.api.auth

import com.daml.error.ErrorCodesVersionSwitcher
import com.daml.ledger.api.auth.interceptor.AuthorizationInterceptor
import com.google.rpc.ErrorInfo
import io.grpc.protobuf.StatusProto
import io.grpc.{Metadata, ServerCall, Status}
import org.mockito.captor.ArgCaptor
Expand Down Expand Up @@ -42,9 +41,7 @@ class AuthorizationInterceptorSpec
actualStatus.getDescription shouldBe "An error occurred. Please contact the operator and inquire about the request <no-correlation-id>"

val actualRpcStatus = StatusProto.fromStatusAndTrailers(actualStatus, actualMetadata)
actualRpcStatus.getDetailsList.size() shouldBe 1
val errorInfo = actualRpcStatus.getDetailsList.get(0).unpack(classOf[ErrorInfo])
errorInfo.getReason shouldBe "INTERNAL_AUTHORIZATION_ERROR"
actualRpcStatus.getDetailsList.size() shouldBe 0
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ class ConversionsSpec extends AsyncWordSpec with Matchers {
v1expectedCode = Status.Code.ABORTED.value(),
v1expectedMessage = "Invalid ledger time: Too late.",
v2expectedCode = Status.Code.FAILED_PRECONDITION.value(),
v2expectedMessage = "INVALID_LEDGER_TIME(9,0): Invalid ledger time: Too late.",
v2expectedMessage = "INVALID_LEDGER_TIME(9,0): Too late.",
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ final class SqlLedgerSpec
case Seq(Completion(`commandId1`, Some(status), _, _, _, _, _)) =>
status.code should be(Status.Code.FAILED_PRECONDITION.value)
status.message should be(
s"INVALID_LEDGER_TIME(9,$submissionId): Invalid ledger time: Ledger time 2021-09-01T18:05:00Z outside of range [2021-09-01T17:59:50Z, 2021-09-01T18:00:30Z]"
s"INVALID_LEDGER_TIME(9,$submissionId): Ledger time 2021-09-01T18:05:00Z outside of range [2021-09-01T17:59:50Z, 2021-09-01T18:00:30Z]"
)
}
}
Expand Down

0 comments on commit ed9bb70

Please sign in to comment.