Returning user friendly error messages when the JSON value is incorrect?

59 views Asked by At

Currently my API endpoint works fine in the happy path if the JSON value corresponds to my case class correctly.

How can I return a user friendly message when it doesn't?

case class UpdateUserRequest (
   registrationStatus: String,
   level: Int
)

For example, say the value level has to be between 10 and 20. And the String value for registraition status has to be either "a", "b" or "c".

case updateUser @ PUT -> Root / "users" as user => {
    for {
        updateReq <- updateUser.req.as[UpdateUserRequest]
       ....
       apiResponse <- Ok(200, true, "")
    } yield apiResponse
}

My response object is like:

case class ApiResponse(
  statusCode: Int,
  success: boolean,
  errorMessage: String
)
0

There are 0 answers