buildo / nozzle

MIT License
0 stars 1 forks source link

Consider adding a default handler to map spray rejections to JSend #8

Open gabro opened 9 years ago

gabro commented 9 years ago

Currently in our internal projects we provide custom rejection handlers that produce a JSendFailure, then we leave the rest to the default rejection handler, which produces meaningful responses, with appropriate HTTP error codes, but that is in plaintext.

It should be trivial to add a default JSend wrapper for the default rejection handler response, using mapHttpResponse. Something along the lines of

  def toJSend(res: HttpResponse): HttpResponse = 
    res.withEntity(HttpBody(ContentType.`application/json`,
      JSendFailure(message = res.entity.asString.toJson).toJson))

  implicit val jsonRejectionHandler = RejectionHandler {
    case rejections => mapHttpResponse(toJSend)(RejectionHandler.Default(rejections))
  }

Not sure the above compiles, but you get the idea.

@utaal, thoughts?

I know @federico-pellegatta is trying to implement this in one of our projects.

utaal commented 9 years ago

Sure this isn't in the new Scala base already (for monadic controllers)? I have to check. On Thu, 29 Oct 2015 at 11:49, Gabriele Petronella notifications@github.com wrote:

Currently in our internal projects we provide custom rejection handlers that produce a JSendFailure, then we leave the rest to the default rejection handler, which produces meaningful responses, with appropriate HTTP error codes, but that is in plaintext.

It should be trivial to add a default JSend wrapper for the default rejection handler response, using mapHttpResponse. Something along the lines of

def toJSend(res: HttpResponse): HttpResponse = res.withEntity(HttpBody(ContentType.application/json, JSendFailure(message = res.entity.asString.toJson).toJson))

implicit val jsonRejectionHandler = RejectionHandler { case rejections => mapHttpResponse(toJSend)(RejectionHandler.Default(rejections)) }

Not sure the above compiles, but you get the idea.

@utaal https://github.com/utaal, thoughts?

I know @federico-pellegatta https://github.com/federico-pellegatta is trying to implement this in one of our projects.

— Reply to this email directly or view it on GitHub https://github.com/buildo/nozzle/issues/8.

utaal commented 9 years ago

Possibly incomplete, but have a look at https://github.com/buildo/labonline/blob/master/escalapio/src/main/scala/labonline/routers/EscalapioRouterModule.scala#L79

gabro commented 9 years ago

@utaal not quite. Here's what I mean: https://github.com/buildo/labonline/commit/edd47c0873683eac6ec5da9c19f1036620de7b74

That leverages the default messages and codes already defined in spray-routing, simply wrapping the in a JSendFailure

gabro commented 8 years ago

I think we have something like this in master now, right? @utaal

utaal commented 8 years ago

Maybe. But if it's not there we should make it happen.