Skip to content

Respond with 400 Bad Request when request can not be parsed #37

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

Merged
merged 1 commit into from
Dec 1, 2024
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
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)
}
}