【发布时间】:2014-12-30 05:29:14
【问题描述】:
当我使用 json 时,如果 json 无效,我会发送格式错误:
error: { error: { obj.email: [{args: "", msg: "error.email"}] } //1
但是使用 json 验证器,我有一个标准的方法来验证数据(例如,如果数据库中存在电子邮件)。 如何统一所有以json形式发送错误的方法(一)?
例如带有动作的控制器:
implicit val loginReads: Reads[Login] = (
(__ \ "email").read[String](email) and
(__ \ "password").read[String]
)(Login.apply _)
def login() = Action { request =>
request.body.asJson match {
case Some(login) => login.validate[Login] fold (
err => BadRequest(Json.toJson(Map("error" -> JsError.toFlatJson(err)))),
result => if (!isPresentInDb(result.email)) {
//how to handle error and send it as json error form: "error" -> JsError.toFlatJson(err)
} else {
//success
}
)
case None => //bad request
}
}
//login class
case class Login(email: String, password: String)
【问题讨论】:
标签: scala playframework-2.0 playframework-2.3