scalaplayframeworkws-client

Handling JSON parse errors of third party services in play applications


I'm wondering what's an acceptable approach to parsing JSON from third-party services considering deserialization errors.

For example, this service method:

  def signInWithEmailAndPassword(email: String, password: String): Future[ApiResponse[SignInResponse]] =
    request("/signin").post(Json.obj("email" -> email, "password" -> password))
      .map(_.json.as[ApiResponse[SignInResponse]])

Will throw a server exception if json.as fails which play will catch in the default error handler.

Is that an OK structuring of the client? Seems like a JSON parse error is not really recoverable anyway, so uses the generic error handler is appropriate?


Solution

  • Assuming ApiResponse is going to hold any client errors (wrong password, etc) and the Future is going to hold server errors (couldn't establish connection, 500 from remote service, etc), then yes it's appropriate for the exception in the Future to bubble up to the error handler and return a 500 to your caller (also assuming there are no resources you need to clean up before returning).