exceptionHandler rejection generating "Illegal response Rejected(List(...))"

76 views
Skip to first unread message

Sam Halliday

unread,
May 6, 2015, 6:25:44 AM5/6/15
to spray...@googlegroups.com
Dear all,

I have a route with a custom (global) implicit exceptionHandler that handles deserialisation errors in order to provide my spray-json POSTing users with enhanced feedback about what may be wrong with their payloads:

 implicit def exceptionHandler(
    implicit
    settings: RoutingSettings,
    log: LoggingContext
  ): ExceptionHandler = ExceptionHandler {
    ... a bunch of other things ....
    case e: DeserializationException => ctx => {
      log.warning(s"Received bad payload: ${ctx.request}")
      ctx.reject(MalformedRequestContentRejection(e.getMessage()))
    }
 }

and this seems to work fine in my specs (using ScalaMock to mock out the backend)

    "give a sensible error when given a bad payload" in {
      val store = stub[BackendThing]

      val message = "expected more from life"
      (store.addPayload _).when(*).returns(
        Future.failed(new DeserializationException(message))
      )

      val payload = """{"hello": "world"}""".parseJson.asJsObject

      Post(s"/event/$name", payload) ~> route ~> check {
        rejection shouldBe MalformedRequestContentRejection(message)
      }
    }

(this is passing).

BUT when I use the same route in my server, the log comes out like this

WARN  - Received bad payload: HttpRequest(POST,HIDDEN,List(Remote-Address: HIDDEN, Remote-Ip-Port: HIDDEN, User-Agent: curl/7.38.0, Host: HIDDEN, user: sam-testing, Content-Type: application/json, Content-Length: 2),HttpEntity(application/json,{}),HTTP/1.1)

[error] 06 May 15 11:17:48.181 HttpServerConnection akka://MDES/user/IO-UHTTP/listener-0/0 [ MDES-akka.actor.default-dispatcher-2] WARN  - Illegal response Rejected(List(MalformedRequestContentRejection(deserialising MyEntry: missing pricingGroup, found ,None))) to POST request to 'HIDDEN'

[info] 06 May 15 11:17:48.183 ResponseReceiverRef akka://MDES/temp/$a [ MDES-akka.actor.default-dispatcher-4] DEBUG - unhandled message from Actor[akka://MDES/user/monitor/uhttp/10.4.4.152:51131#-2079242463]: Rejected(List(MalformedRequestContentRejection(deserialising MyEntry: missing pricingGroup, found ,None)))


so it looks like my log message is printed (via the implicit ActorSystem's LoggingContext), but then the HttpServerConnection doesn't like the Rejected message.

I'm using spray 1.3.3 on scala 2.11 and I have the HTTP IO actor provided by wandoulab's WebSockets (because this same actor system is used for WebSockets comms).

Any ideas?

I don't have a custom RejectionHandler.

Best regards,
Sam

Mathias Doenitz

unread,
May 7, 2015, 5:50:00 AM5/7/15
to spray...@googlegroups.com
Sam,

the problem is that the global exception handler is applied *after* the global rejection handler.
This means that, if you reject from the global exception handler this rejection is not handled by any rejection handler anymore.

There are several possible solutions to this,
one would be to not *reject* but *complete* from your exception handler.

Cheers,
Mathias

---
mat...@spray.io
http://spray.io
> --
> You received this message because you are subscribed to the Google Groups "spray.io User List" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to spray-user+...@googlegroups.com.
> Visit this group at http://groups.google.com/group/spray-user.
> To view this discussion on the web visit https://groups.google.com/d/msgid/spray-user/e1af5273-e536-4056-97e7-be32b7362ca0%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages