Skip to content

Commit 676a9ae

Browse files
authored
Respond with 400 Bad Request when request can not be parsed (#37)
1 parent dfb80df commit 676a9ae

File tree

2 files changed

+5
-9
lines changed

2 files changed

+5
-9
lines changed

core/src/main/scala/org/ivovk/connect_rpc_scala/ConnectHandler.scala

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import io.grpc.*
88
import io.grpc.MethodDescriptor.MethodType
99
import io.grpc.stub.MetadataUtils
1010
import org.http4s.dsl.Http4sDsl
11-
import org.http4s.{MediaType, Method, Response}
11+
import org.http4s.{MediaType, MessageFailure, Method, Response}
1212
import org.ivovk.connect_rpc_scala.Mappings.*
1313
import org.ivovk.connect_rpc_scala.grpc.{MethodName, MethodRegistry}
1414
import org.ivovk.connect_rpc_scala.http.Headers.`X-Test-Case-Name`
@@ -22,12 +22,13 @@ import java.util.concurrent.atomic.AtomicReference
2222
import scala.concurrent.duration.*
2323
import scala.util.chaining.*
2424

25-
class ConnectHandler[F[_]: Async](
25+
class ConnectHandler[F[_] : Async](
2626
codecRegistry: MessageCodecRegistry[F],
2727
methodRegistry: MethodRegistry,
2828
channel: Channel,
2929
httpDsl: Http4sDsl[F],
3030
) {
31+
3132
import httpDsl.*
3233

3334
private val logger: Logger = LoggerFactory.getLogger(getClass)
@@ -137,6 +138,7 @@ class ConnectHandler[F[_]: Async](
137138
case _ => e.getStatus
138139
}
139140
case e: StatusException => e.getStatus
141+
case e: MessageFailure => io.grpc.Status.INVALID_ARGUMENT
140142
case _ => io.grpc.Status.INTERNAL
141143
}
142144

core/src/main/scala/org/ivovk/connect_rpc_scala/http/RequestEntity.scala

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ case class RequestEntity[F[_]](
2121
message: String | Stream[F, Byte],
2222
headers: Headers,
2323
) {
24+
2425
def contentType: Option[`Content-Type`] =
2526
headers.get[`Content-Type`]
2627

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

39-
def fold[A](string: String => A)(stream: Stream[F, Byte] => A): A =
40-
message match {
41-
case s: String =>
42-
string(s)
43-
case b: Stream[F, Byte] =>
44-
stream(b)
45-
}
4640
}

0 commit comments

Comments
 (0)