Skip to content
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

fix: Ensure cause is not dropped when converting internal CMMS API error #2008

Merged
merged 1 commit into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 29 additions & 9 deletions src/main/kotlin/org/wfanet/measurement/common/grpc/ErrorInfo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package org.wfanet.measurement.common.grpc

import com.google.protobuf.Any
import com.google.rpc.ErrorInfo
import com.google.rpc.copy
import com.google.rpc.status
import io.grpc.Metadata
import io.grpc.Status
Expand All @@ -37,6 +38,14 @@ val StatusRuntimeException.errorInfo: ErrorInfo?
return getErrorInfo(this.status, this.trailers)
}

/**
* Converts this [Status] to a [StatusRuntimeException] with the specified [errorInfo].
*
* @see Status.asRuntimeException
*/
fun Status.asRuntimeException(errorInfo: ErrorInfo): StatusRuntimeException =
Errors.buildStatusRuntimeException(this, errorInfo)

object Errors {
/** Builds a [StatusRuntimeException] with the specified [errorInfo]. */
fun buildStatusRuntimeException(
Expand All @@ -54,15 +63,26 @@ object Errors {
// Unpack exception to add cause.
// TODO(grpc/grpc-java#10230): Use new API when available.
val exception = StatusProto.toStatusRuntimeException(statusProto)
return exception.status
.run {
if (cause != null) {
withCause(cause)
} else {
this
}
}
.asRuntimeException(exception.trailers)
return if (cause == null) {
exception
} else {
exception.status.withCause(exception).asRuntimeException(exception.trailers)
}
}

/** Builds a [StatusRuntimeException] with the specified [errorInfo]. */
fun buildStatusRuntimeException(status: Status, errorInfo: ErrorInfo): StatusRuntimeException {
val statusProto =
StatusProto.fromStatusAndTrailers(status, null).copy { details += Any.pack(errorInfo) }

// Unpack exception to add cause.
// TODO(grpc/grpc-java#10230): Use new API when available.
val exception = StatusProto.toStatusRuntimeException(statusProto)
return if (status.cause == null) {
exception
} else {
exception.status.withCause(status.cause).asRuntimeException(exception.trailers)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,10 @@

package org.wfanet.measurement.kingdom.service.api.v2alpha

import com.google.protobuf.Any as ProtoAny
import com.google.rpc.errorInfo
import com.google.rpc.status
import io.grpc.Status
import io.grpc.StatusException
import io.grpc.StatusRuntimeException
import io.grpc.protobuf.StatusProto
import org.wfanet.measurement.api.v2alpha.AccountKey
import org.wfanet.measurement.api.v2alpha.CanonicalExchangeKey
import org.wfanet.measurement.api.v2alpha.CanonicalExchangeStepAttemptKey
Expand All @@ -48,6 +45,7 @@ import org.wfanet.measurement.api.v2alpha.ModelRolloutKey
import org.wfanet.measurement.api.v2alpha.ModelShardKey
import org.wfanet.measurement.api.v2alpha.ModelSuiteKey
import org.wfanet.measurement.api.v2alpha.PopulationKey
import org.wfanet.measurement.common.grpc.asRuntimeException
import org.wfanet.measurement.common.grpc.errorInfo
import org.wfanet.measurement.common.identity.externalIdToApiId
import org.wfanet.measurement.internal.kingdom.Account as InternalAccount
Expand All @@ -57,6 +55,8 @@ import org.wfanet.measurement.internal.kingdom.Measurement as InternalMeasuremen
import org.wfanet.measurement.internal.kingdom.ModelLine as InternalModelLine
import org.wfanet.measurement.internal.kingdom.Requisition as InternalRequisition

private const val DOMAIN = "halo.wfanet.org"

/**
* Converts this [Status] to a [StatusRuntimeException] with details from [internalApiException].
* This may replace the error info and description...
Expand All @@ -65,13 +65,14 @@ fun Status.toExternalStatusRuntimeException(
internalApiException: StatusException
): StatusRuntimeException {
val errorInfo = internalApiException.errorInfo

if (errorInfo == null || errorInfo.domain != ErrorCode.getDescriptor().fullName) {
return this.asRuntimeException()
return withCause(internalApiException).asRuntimeException()
}

var errorMessage = this.description ?: "Unknown exception."
val errorCode = ErrorCode.valueOf(errorInfo.reason)
val metadataMap = buildMap {
when (ErrorCode.valueOf(errorInfo.reason)) {
when (errorCode) {
ErrorCode.MEASUREMENT_NOT_FOUND -> {
val measurementName =
MeasurementKey(
Expand Down Expand Up @@ -702,10 +703,13 @@ fun Status.toExternalStatusRuntimeException(
}
}

val statusProto = status {
code = this@toExternalStatusRuntimeException.code.value()
message = errorMessage
details += ProtoAny.pack(errorInfo { metadata.putAll(metadataMap) })
}
return StatusProto.toStatusRuntimeException(statusProto)
return withCause(internalApiException)
.withDescription(errorMessage)
.asRuntimeException(
errorInfo {
domain = DOMAIN
reason = errorCode.name
metadata.putAll(metadataMap)
}
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import com.google.common.truth.extensions.proto.ProtoTruth.assertThat
import com.google.protobuf.ByteString
import com.google.protobuf.Timestamp
import com.google.protobuf.timestamp
import com.google.rpc.errorInfo
import com.google.type.Interval
import com.google.type.interval
import io.grpc.Status
Expand All @@ -35,6 +36,7 @@ import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.JUnit4
import org.mockito.kotlin.any
import org.mockito.kotlin.doThrow
import org.mockito.kotlin.stub
import org.mockito.kotlin.verify
import org.mockito.kotlin.whenever
Expand Down Expand Up @@ -1261,24 +1263,33 @@ class EventGroupsServiceTest {

@Test
fun `getEventGroup throws NOT_FOUND with event group name when event group not found`() {
internalEventGroupsMock.stub {
onBlocking { getEventGroup(any()) }
.thenThrow(
EventGroupNotFoundException(
ExternalId(DATA_PROVIDER_EXTERNAL_ID),
ExternalId(EVENT_GROUP_EXTERNAL_ID),
)
.asStatusRuntimeException(Status.Code.NOT_FOUND, "EventGroup not found.")
val internalApiException =
EventGroupNotFoundException(
ExternalId(DATA_PROVIDER_EXTERNAL_ID),
ExternalId(EVENT_GROUP_EXTERNAL_ID),
)
.asStatusRuntimeException(Status.Code.NOT_FOUND)
internalEventGroupsMock.stub {
onBlocking { getEventGroup(any()) } doThrow internalApiException
}

val exception =
assertFailsWith<StatusRuntimeException> {
withDataProviderPrincipal(DATA_PROVIDER_NAME) {
runBlocking { service.getEventGroup(getEventGroupRequest { name = EVENT_GROUP_NAME }) }
}
}

assertThat(exception.status.code).isEqualTo(Status.Code.NOT_FOUND)
assertThat(exception.errorInfo?.metadataMap).containsEntry("eventGroup", EVENT_GROUP_NAME)
assertThat(exception.cause).hasMessageThat().isEqualTo(internalApiException.message)
assertThat(exception.errorInfo)
.isEqualTo(
errorInfo {
domain = "halo.wfanet.org"
reason = "EVENT_GROUP_NOT_FOUND"
metadata["eventGroup"] = EVENT_GROUP_NAME
}
)
}

@Test
Expand Down