Skip to content

Commit

Permalink
Updated based on review feedback.
Browse files Browse the repository at this point in the history
  • Loading branch information
drmontgomery committed Feb 26, 2020
1 parent 547727e commit 72f4c2b
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class GrpcExceptionHandlerSpec
}
}

final class RichErrorHeader(data: String) extends ModeledCustomHeader[RichErrorHeader] {
final case class RichErrorHeader(data: String) extends ModeledCustomHeader[RichErrorHeader] {
override def renderInRequests = false
override def renderInResponses = true
override val companion = RichErrorHeader
Expand Down Expand Up @@ -137,8 +137,8 @@ class GrpcExceptionHandlerSpec
statusHeader.map(_.value()) should be(Some("3"))
val statusMessageHeader = lastChunk.trailer.find { _.name == "grpc-message" }
statusMessageHeader.map(_.value()) should be(Some("No name found"))
val richErrorHeader = lastChunk.trailer.find { _.name == RichErrorHeader.name }
richErrorHeader.map(_.value()) should be(Some("test-data"))
val richErrorHeaderValue = lastChunk.trailer.collectFirst { case RichErrorHeader(value) => value }
richErrorHeaderValue should be(Some("test-data"))
}
}
}
8 changes: 4 additions & 4 deletions runtime/src/main/scala/akka/grpc/GrpcServiceException.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@

package akka.grpc

import java.lang.{ Iterable => jIterable }
import java.lang.Iterable

import akka.grpc.internal.GrpcExceptionHelper
import akka.http.javadsl.model.{ HttpHeader => jHttpHeader }
import akka.http.scaladsl.model.HttpHeader
import io.grpc.Status

class GrpcServiceException(val status: Status, val headers: Seq[HttpHeader])
class GrpcServiceException(val status: Status, val headers: List[HttpHeader])
extends RuntimeException(status.getDescription) {

require(!status.isOk, "Use GrpcServiceException in case of failure, not as a flow control mechanism.")
Expand All @@ -23,7 +23,7 @@ class GrpcServiceException(val status: Status, val headers: Seq[HttpHeader])
/**
* Java API: Constructs a service exception which includes response metadata.
*/
def this(status: Status, headers: jIterable[jHttpHeader]) {
def this(status: Status, headers: Iterable[jHttpHeader]) {
this(status, GrpcExceptionHelper.asScala(headers))
}

Expand All @@ -36,6 +36,6 @@ class GrpcServiceException(val status: Status, val headers: Seq[HttpHeader])
/**
* Java API: The response headers.
*/
def getHeaders: jIterable[jHttpHeader] =
def getHeaders: Iterable[jHttpHeader] =
GrpcExceptionHelper.asJava(headers)
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ object GrpcEntityHelpers {
system: ActorSystem) =
e.map(m.serialize).via(Grpc.grpcFramingEncoder(codec)).map(bytes => HttpEntity.Chunk(bytes)).concat(trail)

def trailer(status: Status, headers: Seq[HttpHeader] = Nil): LastChunk =
def trailer(status: Status, headers: List[HttpHeader] = Nil): LastChunk =
LastChunk(trailer = statusHeaders(status) ++ headers)

def statusHeaders(status: Status): List[HttpHeader] =
Expand Down
24 changes: 12 additions & 12 deletions runtime/src/main/scala/akka/grpc/internal/GrpcExceptionHelper.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,36 @@

package akka.grpc.internal

import akka.japi.{ Function => jFunction }
import akka.http.javadsl.model.{ HttpHeader => jHttpHeader }
import akka.http.scaladsl.model.{ HttpHeader => sHttpHeader }
import akka.http.scaladsl.model.headers.{ RawHeader => sRawHeader }
import akka.grpc.javadsl.{ GrpcErrorResponse => jGrpcErrorResponse }
import akka.grpc.scaladsl.{ GrpcErrorResponse => sGrpcErrorResponse }
import java.lang.{ Iterable => jIterable }

import scala.collection.JavaConverters._

import akka.grpc.javadsl.{ scalaAnonymousPartialFunction, scalaPartialFunction }
import akka.actor.ActorSystem
import akka.annotation.InternalApi
import java.lang.{ Iterable => jIterable }
import akka.grpc.javadsl.{ scalaAnonymousPartialFunction, GrpcErrorResponse => jGrpcErrorResponse }
import akka.grpc.scaladsl.{ GrpcErrorResponse => sGrpcErrorResponse }
import akka.http.javadsl.model.{ HttpHeader => jHttpHeader }
import akka.http.scaladsl.model.headers.{ RawHeader => sRawHeader }
import akka.http.scaladsl.model.{ HttpHeader => sHttpHeader }
import akka.japi.{ Function => jFunction }

@InternalApi
object GrpcExceptionHelper {
def asScala(r: jGrpcErrorResponse): sGrpcErrorResponse =
sGrpcErrorResponse(r.status, r.headers.asScala.map(asScala).toSeq)
sGrpcErrorResponse(r.status, asScala(r.headers))

def asScala(h: jHttpHeader): sHttpHeader = h match {
case s: sHttpHeader => s
case _ => sRawHeader(h.value, h.name)
}

def asScala(i: jIterable[jHttpHeader]): Seq[sHttpHeader] =
i.asScala.map(asScala).toSeq
def asScala(i: jIterable[jHttpHeader]): List[sHttpHeader] =
i.asScala.map(asScala).toList

def asScala(m: jFunction[ActorSystem, jFunction[Throwable, jGrpcErrorResponse]])
: ActorSystem => PartialFunction[Throwable, sGrpcErrorResponse] =
scalaAnonymousPartialFunction(m).andThen(f => f.andThen(asScala _))

def asJava(s: Seq[sHttpHeader]): jIterable[jHttpHeader] =
def asJava(s: List[sHttpHeader]): jIterable[jHttpHeader] =
s.map(_.asInstanceOf[jHttpHeader]).asJava
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

package akka.grpc.javadsl

import java.lang.{ Iterable => jIterable }
import java.lang.Iterable
import io.grpc.Status
import akka.http.javadsl.model.HttpHeader

case class GrpcErrorResponse(status: Status, headers: jIterable[HttpHeader])
case class GrpcErrorResponse(status: Status, headers: Iterable[HttpHeader])
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

package akka.grpc.javadsl

import java.lang.{ Iterable => jIterable }
import java.lang.Iterable
import java.util.concurrent.CompletionException
import java.util.{ ArrayList => jArrayList }
import java.util.ArrayList

import io.grpc.Status
import scala.concurrent.ExecutionException
Expand All @@ -18,7 +18,7 @@ import akka.http.javadsl.model.{ HttpHeader, HttpResponse }
import akka.japi.{ Function => jFunction }

object GrpcExceptionHandler {
private val NO_HEADERS: jIterable[HttpHeader] = new jArrayList[HttpHeader]();
private val NO_HEADERS: Iterable[HttpHeader] = new ArrayList[HttpHeader]();

private val INVALID_ARGUMENT = GrpcErrorResponse(Status.INVALID_ARGUMENT, NO_HEADERS)
private val INTERNAL = GrpcErrorResponse(Status.INTERNAL, NO_HEADERS)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ package akka.grpc.scaladsl
import akka.http.scaladsl.model.HttpHeader
import io.grpc.Status

case class GrpcErrorResponse(status: Status, headers: Seq[HttpHeader] = Nil)
case class GrpcErrorResponse(status: Status, headers: List[HttpHeader] = Nil)

0 comments on commit 72f4c2b

Please sign in to comment.