Skip to content

Commit

Permalink
Respond with 400 Bad Request when request can not be parsed
Browse files Browse the repository at this point in the history
  • Loading branch information
igor-vovk committed Dec 1, 2024
1 parent dfb80df commit fd2e15d
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import io.grpc.*
import io.grpc.MethodDescriptor.MethodType
import io.grpc.stub.MetadataUtils
import org.http4s.dsl.Http4sDsl
import org.http4s.{MediaType, Method, Response}
import org.http4s.{MediaType, MessageFailure, Method, Response}
import org.ivovk.connect_rpc_scala.Mappings.*
import org.ivovk.connect_rpc_scala.grpc.{MethodName, MethodRegistry}
import org.ivovk.connect_rpc_scala.http.Headers.`X-Test-Case-Name`
Expand All @@ -22,12 +22,13 @@ import java.util.concurrent.atomic.AtomicReference
import scala.concurrent.duration.*
import scala.util.chaining.*

class ConnectHandler[F[_]: Async](
class ConnectHandler[F[_] : Async](
codecRegistry: MessageCodecRegistry[F],
methodRegistry: MethodRegistry,
channel: Channel,
httpDsl: Http4sDsl[F],
) {

import httpDsl.*

private val logger: Logger = LoggerFactory.getLogger(getClass)
Expand Down Expand Up @@ -137,6 +138,7 @@ class ConnectHandler[F[_]: Async](
case _ => e.getStatus
}
case e: StatusException => e.getStatus
case e: MessageFailure => io.grpc.Status.INVALID_ARGUMENT
case _ => io.grpc.Status.INTERNAL
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ case class RequestEntity[F[_]](
message: String | Stream[F, Byte],
headers: Headers,
) {

def contentType: Option[`Content-Type`] =
headers.get[`Content-Type`]

Expand All @@ -36,11 +37,4 @@ case class RequestEntity[F[_]](
def as[A <: Message](using M: MonadThrow[F], codec: MessageCodec[F], cmp: Companion[A]): F[A] =
M.rethrow(codec.decode(this).value)

def fold[A](string: String => A)(stream: Stream[F, Byte] => A): A =
message match {
case s: String =>
string(s)
case b: Stream[F, Byte] =>
stream(b)
}
}

0 comments on commit fd2e15d

Please sign in to comment.