These are chat archives for hseeberger/akka-http-json

10th
Jan 2018
Jens Grassel
@jan0sch
Jan 10 2018 07:06
Hi, if I wanted to handle decoding failures myself would I need to do custom exception or custom rejection handling?
Loránd Szakács
@lorandszakacs
Jan 10 2018 07:32
@jan0sch, I think both. Roughly speaking, ExceptionHandlers deal with exceptions thrown in complete(...) blocks, while RejectionsHandlers anything from missing headers, deserialization errors, etc.
/**
  * Mix-in this trait to accumulate all errors during unmarshalling.
  */
trait ErrorAccumulatingUnmarshaller { this: BaseCirceSupport =>

  override implicit final def unmarshaller[A: Decoder]: FromEntityUnmarshaller[A] = {
    def decode(json: Json) =
      implicitly[Decoder[A]]
        .accumulating(json.hcursor)
        .fold(failures => throw ErrorAccumulatingCirceSupport.DecodingFailures(failures), identity)
    jsonUnmarshaller.map(decode)
  }
}
I'll test it out with exceptions first.
Loránd Szakács
@lorandszakacs
Jan 10 2018 07:49
hmm, you might be right. It's been a while since I looked at that stuff.
Jens Grassel
@jan0sch
Jan 10 2018 07:51
Thanks nonetheless, I'll report back here if that is the way to do it.
Jens Grassel
@jan0sch
Jan 10 2018 08:27
As it seems a custom RejectionHandler is the way to go. A MalformedRequestContentRejection will be produced and can be worked on in the handler. =)
Heiko Seeberger
@hseeberger
Jan 10 2018 13:09
Thanks @jan0sch !