Scala Customizing Refined Type Exceptions in Http4s

Viewed 77

I am using refined for my requests in order to ensure that the request body confronts to certain specs, such as

 case class NewGenreRequest(
      name: GenreNameParam,
      parent: Option[GenreParentParam]
  ) 

where

type GenreNameString = String Refined MinSize[3]

When the request body sends something with 2 characters, this results in the error Predicate taking size(me) = 2 failed: Predicate (2 < 3) did not fail.: DownField(name) in the response body. However I think this isn't the best error to return to the client.

I would like to find a way to customize this response. The logical thing to try was to write a middleware

 def handleErr(resp: Response[F]): Response[F] =
      resp match {
        case Status.BadRequest(resp) =>
          logger.info("HERE IS MY ERR")
          resp.???? 
        case _ =>
          resp
      }

However I need to extract the info regarding which field has failed the predicates. The response body is an fs2 Stream and I am not quite sure about how to go about effectively modifying it using the existing info in the body. I tried maping it but to no result.

Is there an easy way of modifying these error messages?

P.S.: Cross posting from http4s discord as I wasn't able to get an answer

0 Answers
Related