Reputation: 586
I am new to play framework. I am using Play Framework (2.4)
with scala
. I need your opinion about exception handling.
Our play client controller is calling play server controller. Now we need to introduce custom exceptions which will be thrown from Server and client will catch that exception and do necessary things.
Now when I throw any exception from server it is not reaching client as it is. The reason I found behind this is, Play catches all exceptions and convert them to 500 Internal Server Error
So is it possible to send custom exception to client without modifying it?
Upvotes: 0
Views: 1236
Reputation: 14825
Stop play from giving 500
by handling the exception by yourself in the play controller
You can write a custom action builder for this purpose
object ExceptionCatchingAction extends ActionBuilder[Request] {
override def invokeBlock[A](request: Request[A], block: (Request[A]) => Future[Result]): Future[Result] = {
block(request).recover { case th =>
//use meaningful http codes instead of OK here based on the exception type
Ok(Json.obj("msg" -> "error occurred", "info" -> "error info"))
}
}
}
Use it like this
object ExampleController extends Controller {
def doStuff = ExceptionCatchingAction { req =>
throw new Exception("foo")
}
}
Your client will no get all the exceptions. So that client can now take decisions on what to do.
Upvotes: 2