-
Notifications
You must be signed in to change notification settings - Fork 205
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[DPP-656] Assert on self-service error code details in ErrorFactoriesSpec #11289
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -4,21 +4,22 @@ | |||
package com.daml | ||||
|
||||
import com.daml.error.{ | ||||
DamlContextualizedErrorLogger, | ||||
ContextualizedErrorLogger, | ||||
DamlContextualizedErrorLogger, | ||||
ErrorCodesVersionSwitcher, | ||||
} | ||||
import com.daml.ledger.api.domain.LedgerId | ||||
import com.daml.logging.{ContextualizedLogger, LoggingContext} | ||||
import com.daml.platform.server.api.validation.ErrorFactories | ||||
import com.daml.platform.server.api.validation.ErrorFactories._ | ||||
import com.google.rpc.Status | ||||
import com.google.rpc.{ErrorInfo, RequestInfo, ResourceInfo, RetryInfo, Status} | ||||
import io.grpc.Status.Code | ||||
import io.grpc.StatusRuntimeException | ||||
import io.grpc.protobuf.StatusProto | ||||
import org.scalatest.matchers.should.Matchers | ||||
import org.scalatest.prop.TableDrivenPropertyChecks | ||||
import org.scalatest.wordspec.AnyWordSpec | ||||
import com.google.protobuf | ||||
|
||||
import scala.jdk.CollectionConverters._ | ||||
|
||||
|
@@ -30,6 +31,9 @@ class ErrorFactoriesSpec extends AnyWordSpec with Matchers with TableDrivenPrope | |||
private implicit val contextualizedErrorLogger: ContextualizedErrorLogger = | ||||
new DamlContextualizedErrorLogger(logger, loggingContext, Some(correlationId)) | ||||
|
||||
private val DefaultTraceIdRequestInfo: ErrorDetails.RequestInfoDetail = | ||||
ErrorDetails.RequestInfoDetail("trace-id") | ||||
|
||||
"ErrorFactories" should { | ||||
"return the DuplicateCommandException" in { | ||||
assertVersionedError(_.duplicateCommandException)( | ||||
|
@@ -39,6 +43,10 @@ class ErrorFactoriesSpec extends AnyWordSpec with Matchers with TableDrivenPrope | |||
v2_code = Code.ALREADY_EXISTS, | ||||
v2_message = | ||||
s"DUPLICATE_COMMAND(10,$correlationId): A command with the given command id has already been successfully processed", | ||||
v2_details = Seq[ErrorDetails.ErrorDetail]( | ||||
ErrorDetails.ErrorInfoDetail("DUPLICATE_COMMAND"), | ||||
DefaultTraceIdRequestInfo, | ||||
), | ||||
) | ||||
} | ||||
|
||||
|
@@ -50,6 +58,10 @@ class ErrorFactoriesSpec extends AnyWordSpec with Matchers with TableDrivenPrope | |||
v2_code = Code.PERMISSION_DENIED, | ||||
v2_message = | ||||
s"An error occurred. Please contact the operator and inquire about the request $correlationId", | ||||
v2_details = Seq[ErrorDetails.ErrorDetail]( | ||||
ErrorDetails.ErrorInfoDetail("PERMISSION_DENIED"), | ||||
DefaultTraceIdRequestInfo, | ||||
), | ||||
) | ||||
} | ||||
|
||||
|
@@ -68,6 +80,10 @@ class ErrorFactoriesSpec extends AnyWordSpec with Matchers with TableDrivenPrope | |||
v2_code = Code.NOT_FOUND, | ||||
v2_message = | ||||
s"LEDGER_CONFIGURATION_NOT_FOUND(11,$correlationId): The ledger configuration is not available.", | ||||
v2_details = Seq[ErrorDetails.ErrorDetail]( | ||||
ErrorDetails.ErrorInfoDetail("LEDGER_CONFIGURATION_NOT_FOUND"), | ||||
DefaultTraceIdRequestInfo, | ||||
), | ||||
) | ||||
} | ||||
} | ||||
|
@@ -105,6 +121,10 @@ class ErrorFactoriesSpec extends AnyWordSpec with Matchers with TableDrivenPrope | |||
v2_code = Code.INVALID_ARGUMENT, | ||||
v2_message = | ||||
s"INVALID_FIELD(8,$correlationId): The submitted command has a field with invalid value: Invalid field my field: my message", | ||||
v2_details = Seq[ErrorDetails.ErrorDetail]( | ||||
ErrorDetails.ErrorInfoDetail("INVALID_FIELD"), | ||||
DefaultTraceIdRequestInfo, | ||||
), | ||||
) | ||||
} | ||||
} | ||||
|
@@ -117,6 +137,10 @@ class ErrorFactoriesSpec extends AnyWordSpec with Matchers with TableDrivenPrope | |||
v2_code = Code.UNAUTHENTICATED, | ||||
v2_message = | ||||
s"An error occurred. Please contact the operator and inquire about the request $correlationId", | ||||
v2_details = Seq[ErrorDetails.ErrorDetail]( | ||||
ErrorDetails.ErrorInfoDetail("UNAUTHENTICATED"), | ||||
DefaultTraceIdRequestInfo, | ||||
), | ||||
) | ||||
} | ||||
|
||||
|
@@ -137,6 +161,10 @@ class ErrorFactoriesSpec extends AnyWordSpec with Matchers with TableDrivenPrope | |||
v2_code = Code.NOT_FOUND, | ||||
v2_message = | ||||
s"LEDGER_ID_MISMATCH(11,$correlationId): Ledger ID 'received' not found. Actual Ledger ID is 'expected'.", | ||||
v2_details = Seq[ErrorDetails.ErrorDetail]( | ||||
ErrorDetails.ErrorInfoDetail("LEDGER_ID_MISMATCH"), | ||||
DefaultTraceIdRequestInfo, | ||||
), | ||||
) | ||||
} | ||||
} | ||||
|
@@ -156,6 +184,10 @@ class ErrorFactoriesSpec extends AnyWordSpec with Matchers with TableDrivenPrope | |||
v1_details = Seq.empty, | ||||
v2_code = Code.OUT_OF_RANGE, | ||||
v2_message = s"PARTICIPANT_PRUNED_DATA_ACCESSED(12,$correlationId): my message", | ||||
v2_details = Seq[ErrorDetails.ErrorDetail]( | ||||
ErrorDetails.ErrorInfoDetail("PARTICIPANT_PRUNED_DATA_ACCESSED"), | ||||
DefaultTraceIdRequestInfo, | ||||
), | ||||
) | ||||
} | ||||
|
||||
|
@@ -166,6 +198,10 @@ class ErrorFactoriesSpec extends AnyWordSpec with Matchers with TableDrivenPrope | |||
v1_details = Seq.empty, | ||||
v2_code = Code.OUT_OF_RANGE, | ||||
v2_message = s"REQUESTED_OFFSET_OUT_OF_RANGE(12,$correlationId): my message", | ||||
v2_details = Seq[ErrorDetails.ErrorDetail]( | ||||
ErrorDetails.ErrorInfoDetail("REQUESTED_OFFSET_OUT_OF_RANGE"), | ||||
DefaultTraceIdRequestInfo, | ||||
), | ||||
) | ||||
} | ||||
|
||||
|
@@ -183,6 +219,11 @@ class ErrorFactoriesSpec extends AnyWordSpec with Matchers with TableDrivenPrope | |||
v1_details = expectedDetails, | ||||
v2_code = Code.UNAVAILABLE, | ||||
v2_message = s"SERVICE_NOT_RUNNING(1,$correlationId): Service has been shut down.", | ||||
v2_details = Seq[ErrorDetails.ErrorDetail]( | ||||
ErrorDetails.ErrorInfoDetail("SERVICE_NOT_RUNNING"), | ||||
DefaultTraceIdRequestInfo, | ||||
ErrorDetails.RetryInfoDetail(1), | ||||
), | ||||
) | ||||
} | ||||
} | ||||
|
@@ -195,6 +236,10 @@ class ErrorFactoriesSpec extends AnyWordSpec with Matchers with TableDrivenPrope | |||
v2_code = Code.NOT_FOUND, | ||||
v2_message = | ||||
s"LEDGER_CONFIGURATION_NOT_FOUND(11,$correlationId): The ledger configuration is not available.", | ||||
v2_details = Seq[ErrorDetails.ErrorDetail]( | ||||
ErrorDetails.ErrorInfoDetail("LEDGER_CONFIGURATION_NOT_FOUND"), | ||||
DefaultTraceIdRequestInfo, | ||||
), | ||||
) | ||||
} | ||||
|
||||
|
@@ -213,6 +258,10 @@ class ErrorFactoriesSpec extends AnyWordSpec with Matchers with TableDrivenPrope | |||
v2_code = Code.INVALID_ARGUMENT, | ||||
v2_message = | ||||
s"MISSING_FIELD(8,$correlationId): The submitted command is missing a mandatory field: my field", | ||||
v2_details = Seq[ErrorDetails.ErrorDetail]( | ||||
ErrorDetails.ErrorInfoDetail("MISSING_FIELD"), | ||||
DefaultTraceIdRequestInfo, | ||||
), | ||||
) | ||||
} | ||||
} | ||||
|
@@ -232,6 +281,10 @@ class ErrorFactoriesSpec extends AnyWordSpec with Matchers with TableDrivenPrope | |||
v2_code = Code.INVALID_ARGUMENT, | ||||
v2_message = | ||||
s"INVALID_ARGUMENT(8,$correlationId): The submitted command has invalid arguments: my message", | ||||
v2_details = Seq[ErrorDetails.ErrorDetail]( | ||||
ErrorDetails.ErrorInfoDetail("INVALID_ARGUMENT"), | ||||
DefaultTraceIdRequestInfo, | ||||
), | ||||
) | ||||
} | ||||
} | ||||
|
@@ -245,29 +298,76 @@ class ErrorFactoriesSpec extends AnyWordSpec with Matchers with TableDrivenPrope | |||
|
||||
private def assertVersionedError( | ||||
error: ErrorFactories => StatusRuntimeException | ||||
)(v1_code: Code, v1_message: String, v1_details: Seq[Any], v2_code: Code, v2_message: String) = { | ||||
)( | ||||
v1_code: Code, | ||||
v1_message: String, | ||||
v1_details: Seq[Any], | ||||
v2_code: Code, | ||||
v2_message: String, | ||||
v2_details: Seq[ErrorDetails.ErrorDetail], | ||||
): Unit = { | ||||
val errorFactoriesV1 = ErrorFactories(new ErrorCodesVersionSwitcher(false)) | ||||
val errorFactoriesV2 = ErrorFactories(new ErrorCodesVersionSwitcher(true)) | ||||
assertV1Error(error(errorFactoriesV1))(v1_code, v1_message, v1_details) | ||||
assertV2Error(error(errorFactoriesV2))(v2_code, v2_message) | ||||
assertV2Error(error(errorFactoriesV2))(v2_code, v2_message, v2_details) | ||||
} | ||||
|
||||
private def assertV1Error( | ||||
statusRuntimeException: StatusRuntimeException | ||||
)(expectedCode: Code, expectedMessage: String, expectedDetails: Seq[Any]) = { | ||||
)(expectedCode: Code, expectedMessage: String, expectedDetails: Seq[Any]): Unit = { | ||||
val status = StatusProto.fromThrowable(statusRuntimeException) | ||||
status.getCode shouldBe expectedCode.value() | ||||
status.getMessage shouldBe expectedMessage | ||||
status.getDetailsList.asScala shouldBe expectedDetails | ||||
val _ = status.getDetailsList.asScala shouldBe expectedDetails | ||||
Comment on lines
318
to
+321
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You could use:
like:
It's just a matter of preference though. |
||||
} | ||||
|
||||
private def assertV2Error( | ||||
statusRuntimeException: StatusRuntimeException | ||||
)(expectedCode: Code, expectedMessage: String) = { | ||||
)( | ||||
expectedCode: Code, | ||||
expectedMessage: String, | ||||
expectedDetails: Seq[ErrorDetails.ErrorDetail], | ||||
): Unit = { | ||||
val status = StatusProto.fromThrowable(statusRuntimeException) | ||||
status.getCode shouldBe expectedCode.value() | ||||
status.getMessage shouldBe expectedMessage | ||||
// TODO error codes: Assert error details | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🙌 |
||||
val details = status.getDetailsList.asScala.toSeq | ||||
val _ = ErrorDetails.from(details) should contain theSameElementsAs (expectedDetails) | ||||
// TODO error codes: Assert logging | ||||
} | ||||
} | ||||
|
||||
object ErrorDetails { | ||||
|
||||
sealed trait ErrorDetail | ||||
|
||||
final case class ResourceInfoDetail(name: String, typ: String) extends ErrorDetail | ||||
|
||||
final case class ErrorInfoDetail(reason: 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.map(from) | ||||
} | ||||
|
||||
private def from(any: protobuf.Any): ErrorDetail = { | ||||
if (any.is(classOf[ResourceInfo])) { | ||||
val v = any.unpack(classOf[ResourceInfo]) | ||||
ResourceInfoDetail(v.getResourceType, v.getResourceName) | ||||
} else if (any.is(classOf[ErrorInfo])) { | ||||
val v = any.unpack(classOf[ErrorInfo]) | ||||
ErrorInfoDetail(v.getReason) | ||||
} else if (any.is(classOf[RetryInfo])) { | ||||
val v = any.unpack(classOf[RetryInfo]) | ||||
RetryInfoDetail(v.getRetryDelay.getSeconds) | ||||
} else if (any.is(classOf[RequestInfo])) { | ||||
val v = any.unpack(classOf[RequestInfo]) | ||||
RequestInfoDetail(v.getRequestId) | ||||
} else { | ||||
throw new IllegalStateException(s"Could not unpack value of: |$any|") | ||||
} | ||||
} | ||||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be camel case, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wrote them like this in the initial implementation invoking better readability. However, I don't have a strong opinion here and I dislike as well random snake-case in Scala files. Feel free to change if necessary or use a different naming scheme/signature for improving readability