[Error codes] Remove .asGrpcStatusFromContext and .asGrpcErrorFromContext from BaseError trait [DPP-606] (#13262)

changelog_begin
changelog_end
This commit is contained in:
pbatko-da 2022-03-16 09:30:07 +01:00 committed by GitHub
parent 0b5ad7a7bd
commit e0965709fe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 27 additions and 63 deletions

View File

@ -52,16 +52,6 @@ trait BaseError extends LocationMixin {
): Unit =
contextualizedErrorLogger.logError(this, extra)
def asGrpcStatusFromContext(implicit
contextualizedErrorLogger: ContextualizedErrorLogger
): Status =
code.asGrpcStatus(this)
def asGrpcErrorFromContext(implicit
contextualizedErrorLogger: ContextualizedErrorLogger
): StatusRuntimeException =
code.asGrpcError(this)
/** Returns retryability information of this particular error
*
* In some cases, error instances would like to provide custom retry intervals.
@ -76,6 +66,20 @@ trait BaseError extends LocationMixin {
def definiteAnswerO: Option[Boolean] = None
}
/** Base class for errors for which error context is known at creation.
*/
trait ContextualizedError extends BaseError {
protected def errorContext: ContextualizedErrorLogger
def asGrpcStatus: Status =
code.asGrpcStatus(this)(errorContext)
def asGrpcError: StatusRuntimeException =
code.asGrpcError(this)(errorContext)
}
trait LocationMixin {
/** Contains the location where the error has been created. */

View File

@ -3,9 +3,7 @@
package com.daml.error.definitions
import com.daml.error.{BaseError, ContextualizedErrorLogger, ErrorCode}
import com.google.rpc.Status
import io.grpc.StatusRuntimeException
import com.daml.error.{ContextualizedError, ContextualizedErrorLogger, ErrorCode}
import scala.jdk.CollectionConverters._
@ -14,20 +12,13 @@ class DamlError(
override val throwableO: Option[Throwable] = None,
)(implicit
override val code: ErrorCode,
loggingContext: ContextualizedErrorLogger,
) extends BaseError {
override val errorContext: ContextualizedErrorLogger,
) extends ContextualizedError {
// Automatically log the error on generation
loggingContext.logError(this, Map())
errorContext.logError(this, Map())
def asGrpcStatus: Status =
code.asGrpcStatus(this)(loggingContext)
def asGrpcError: StatusRuntimeException =
code.asGrpcError(this)(loggingContext)
def rpcStatus(
)(implicit loggingContext: ContextualizedErrorLogger): com.google.rpc.status.Status = {
def rpcStatus(): com.google.rpc.status.Status = {
val status0: com.google.rpc.Status = code.asGrpcStatus(this)
val details: Seq[com.google.protobuf.Any] = status0.getDetailsList.asScala.toSeq
val detailsScalapb = details.map(com.google.protobuf.any.Any.fromJavaProto)

View File

@ -3,7 +3,7 @@
package com.daml.error.definitions
import com.daml.error.{BaseError, ContextualizedErrorLogger}
import com.daml.error.ContextualizedErrorLogger
import com.daml.lf.engine.Error.{Interpretation, Package, Preprocessing, Validation}
import com.daml.lf.engine.{Error => LfError}
import com.daml.lf.interpretation.{Error => LfInterpretationError}
@ -22,7 +22,7 @@ object RejectionGenerators {
errorLoggingContext: ContextualizedErrorLogger
): StatusRuntimeException = {
def processPackageError(err: LfError.Package.Error): BaseError = err match {
def processPackageError(err: LfError.Package.Error): DamlError = err match {
case e: Package.Internal => LedgerApiErrors.InternalError.PackageInternal(e)
case Package.Validation(validationError) =>
LedgerApiErrors.CommandExecution.Package.PackageValidationFailed
@ -40,12 +40,12 @@ object RejectionGenerators {
LedgerApiErrors.InternalError.PackageSelfConsistency(e)
}
def processPreprocessingError(err: LfError.Preprocessing.Error): BaseError = err match {
def processPreprocessingError(err: LfError.Preprocessing.Error): DamlError = err match {
case e: Preprocessing.Internal => LedgerApiErrors.InternalError.Preprocessing(e)
case e => LedgerApiErrors.CommandExecution.Preprocessing.PreprocessingFailed.Reject(e)
}
def processValidationError(err: LfError.Validation.Error): BaseError = err match {
def processValidationError(err: LfError.Validation.Error): DamlError = err match {
// we shouldn't see such errors during submission
case e: Validation.ReplayMismatch => LedgerApiErrors.InternalError.Validation(e)
}
@ -54,7 +54,7 @@ object RejectionGenerators {
err: com.daml.lf.interpretation.Error,
renderedMessage: String,
detailMessage: Option[String],
): BaseError = {
): DamlError = {
// detailMessage is only suitable for server side debugging but not for the user, so don't pass except on internal errors
err match {
@ -137,7 +137,7 @@ object RejectionGenerators {
def processInterpretationError(
err: LfError.Interpretation.Error,
detailMessage: Option[String],
): BaseError =
): DamlError =
err match {
case Interpretation.Internal(location, message, _) =>
LedgerApiErrors.InternalError.Interpretation(location, message, detailMessage)
@ -158,7 +158,7 @@ object RejectionGenerators {
) => // Keeping this around as a string match as daml is not yet generating LfError.InterpreterErrors.Validation
LedgerApiErrors.CommandExecution.Interpreter.AuthorizationError.Reject(e.message)
}
transformed.asGrpcErrorFromContext
transformed.asGrpcError
}
cause match {
@ -166,7 +166,7 @@ object RejectionGenerators {
case x: ErrorCause.LedgerTime =>
LedgerApiErrors.CommandExecution.FailedToDetermineLedgerTime
.Reject(s"Could not find a suitable ledger time after ${x.retries} retries")
.asGrpcErrorFromContext
.asGrpcError
}
}
}

View File

@ -184,22 +184,12 @@ class ErrorCodeSpec
actual = testedErrorCode.asGrpcStatus(testedError)(errorLoggerBig),
expected = expectedStatus,
)
assertStatus(
actual = testedError.asGrpcStatusFromContext(errorLoggerBig),
expected = expectedStatus,
)
assertError(
actual = testedErrorCode.asGrpcError(testedError)(errorLoggerBig),
expectedStatusCode = testedErrorCode.category.grpcCode.get,
expectedMessage = "FOO_ERROR_CODE(8,123corre): cause123",
expectedDetails = details,
)
assertError(
actual = testedError.asGrpcErrorFromContext(errorLoggerBig),
expectedStatusCode = testedErrorCode.category.grpcCode.get,
expectedMessage = "FOO_ERROR_CODE(8,123corre): cause123",
expectedDetails = details,
)
}
"security sensitive" in {
@ -234,10 +224,6 @@ class ErrorCodeSpec
actual = testedErrorCode.asGrpcStatus(testedError)(errorLoggerBig),
expected = expectedStatus,
)
assertStatus(
actual = testedError.asGrpcStatusFromContext(errorLoggerBig),
expected = expectedStatus,
)
assertError(
actual = testedErrorCode.asGrpcError(testedError)(errorLoggerBig),
expectedStatusCode = testedErrorCode.category.grpcCode.get,
@ -245,13 +231,6 @@ class ErrorCodeSpec
"An error occurred. Please contact the operator and inquire about the request 123correlationId",
expectedDetails = Seq(requestInfo, retryInfo),
)
assertError(
actual = testedError.asGrpcErrorFromContext(errorLoggerBig),
expectedStatusCode = testedErrorCode.category.grpcCode.get,
expectedMessage =
"An error occurred. Please contact the operator and inquire about the request 123correlationId",
expectedDetails = Seq(requestInfo, retryInfo),
)
}
}
@ -327,22 +306,12 @@ class ErrorCodeSpec
actual = testedErrorCode.asGrpcStatus(testedError)(errorLoggerOversized),
expected = expectedStatus,
)
assertStatus(
actual = testedError.asGrpcStatusFromContext(errorLoggerOversized),
expected = expectedStatus,
)
assertError(
actual = testedErrorCode.asGrpcError(testedError)(errorLoggerOversized),
expectedStatusCode = testedErrorCode.category.grpcCode.get,
expectedMessage = expectedMessage,
expectedDetails = expectedDetails,
)
assertError(
actual = testedError.asGrpcErrorFromContext(errorLoggerOversized),
expectedStatusCode = testedErrorCode.category.grpcCode.get,
expectedMessage = expectedMessage,
expectedDetails = expectedDetails,
)
}
"log the error message with the correct markers" in {