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

Add StatusException trailers to response metadata #548

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ object ListenerDriver {
completed.await *>
call.sendHeaders(new Metadata) *>
request.await flatMap writeResponse
).onExit(ex => call.close(ListenerDriver.exitToStatus(ex), requestContext.responseMetadata.metadata).ignore)
).tapError(statusException => requestContext.responseMetadata.wrap(_.merge(statusException.getTrailers)).ignore)
.onExit(ex => call.close(ListenerDriver.exitToStatus(ex), requestContext.responseMetadata.metadata).ignore)
.ignore
// Why forkDaemon? we need the driver to keep runnning in the background after we return a listener
// back to grpc-java. If it was just fork, the call to unsafeRun would not return control, so grpc-java
Expand Down
16 changes: 14 additions & 2 deletions e2e/src/main/scalajvm/scalapb/zio_grpc/TestServiceImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ package scalapb.zio_grpc
import scalapb.zio_grpc.testservice.Request
import zio.{Clock, Console, Exit, Promise, ZIO, ZLayer}
import scalapb.zio_grpc.testservice.Response
import io.grpc.{Status, StatusException}
import io.grpc.Metadata.BinaryMarshaller
import io.grpc.{Metadata, Status, StatusException}
import scalapb.zio_grpc.testservice.Request.Scenario
import zio.stream.{Stream, ZStream}
import zio.ZEnvironment
Expand All @@ -29,7 +30,18 @@ package object server {
Response(out = "Res" + request.in.toString)
)
case Scenario.ERROR_NOW =>
ZIO.fail(Status.INTERNAL.withDescription("FOO!").asException())
val metadataKey = Metadata.Key.of(
"foo-bin",
new BinaryMarshaller[String] {
override def toBytes(value: String): Array[Byte] = value.getBytes

override def parseBytes(serialized: Array[Byte]): String = new String(serialized)
}
)
ZIO
.succeed(new Metadata())
.tap(m => ZIO.succeed(m.put(metadataKey, "bar")))
.flatMap(metadata => ZIO.fail(Status.INTERNAL.withDescription("FOO!").asException(metadata)))
case Scenario.DELAY => ZIO.never
case Scenario.DIE => ZIO.die(new RuntimeException("FOO"))
case _ => ZIO.fail(Status.UNKNOWN.asException())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ trait CommonTestServiceSpec {
.unary(Request(Request.Scenario.ERROR_NOW, in = 12))
.exit
)(
fails(hasStatusCode(Status.INTERNAL))
fails(hasStatusCode(Status.INTERNAL) && hasMetadataKey("foo-bin"))
)
},
test("returns response on failures") {
Expand Down
9 changes: 9 additions & 0 deletions e2e/src/test/scala/scalapb/zio_grpc/TestUtils.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package scalapb.zio_grpc

import scala.collection.JavaConverters._

import zio.test.Assertion._
import io.grpc.{Status, StatusException}
import io.grpc.Status.Code
Expand All @@ -18,6 +20,13 @@ object TestUtils {
equalTo(d)
)

def hasMetadataKey(key: String) =
hasField[StatusException, Iterable[String]](
"metadataKey",
e => e.getTrailers.keys.asScala,
contains(key)
)

def collectWithError[R, E, A](
zs: ZStream[R, E, A]
): URIO[R, (List[A], Option[E])] =
Expand Down
Loading