Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Forms 如何在scala playframework中捕获执行异常?_Forms_Scala_Playframework - Fatal编程技术网

Forms 如何在scala playframework中捕获执行异常?

Forms 如何在scala playframework中捕获执行异常?,forms,scala,playframework,Forms,Scala,Playframework,我得到执行异常 [APIRERROR:您必须提供电子邮件地址才能创建票证。] 可以捕获此错误并仅发布 “您必须提供电子邮件地址才能创建票证。” 此错误应显示在查看页面中 给出错误的代码: val question = uservoice.post("/api/v1/tickets.json", ticket).getJSONObject("ticket") 控制器: def contactSave = withOptionUser { user => implicit request =

我得到执行异常

[APIRERROR:您必须提供电子邮件地址才能创建票证。]

可以捕获此错误并仅发布

“您必须提供电子邮件地址才能创建票证。”

此错误应显示在查看页面中

给出错误的代码:

val question = uservoice.post("/api/v1/tickets.json", ticket).getJSONObject("ticket")
控制器:

def contactSave = withOptionUser { user => implicit request =>
    contactForm.bindFromRequest.fold(
      formWithErrors => BadRequest(html.anon.contact(user, formWithErrors)),
      c => {
          val uservoice = new com.uservoice.Client(SUBDOMAIN, API_KEY, API_SECRET)

          val ticketMsg = Map("state" -> "open","subject" -> c._2, "message" -> c._3).toMap[String,Object].asJava

          val ticket = Map("email" -> c._1, "ticket" -> ticketMsg).toMap[String,Object].asJava

          Logger.debug(ticket.toString)

          val question = uservoice.post("/api/v1/tickets.json", ticket).getJSONObject("ticket")

          Logger.debug(question.toString)

          Ok(views.html.anon.contactThanks(user))
      }
    )
  }
Html:

@main("Contact Us",user,"contact",stylesheet, scripts) {
    @helper.form(routes.UservoiceController.contactSave) {
<section class="contact">
    <div class="contactBox contentBox">
        <div class="leftColumn">
            <h1>Contact Us</h1>
            <span>You can fill out this form for any general inquiries, comments, etc.</span>
            <span>You can also find us on Facebook and Twitter!</span>

            <div class="social">
                @form.globalError.map { error =>
                <span class="error" data-xpl="loginError">
                    @error.message
                </span>
@main(“联系我们”,用户,“联系”,样式表,脚本){
@helper.form(routes.UservoiceController.contactSave){
联系我们
您可以填写此表格以了解任何一般性询问、意见等。
你也可以在Facebook和Twitter上找到我们!
@form.globalError.map{error=>
@错误消息

您可以使用try/catch块创建表单

var form = contactForm.bindFromRequest()
form.fold(
  formWithErrors => ...
  c => {
    ...
    try {
      val question = uservoice.post("/api/v1/tickets.json", ticket).getJSONObject("ticket")
      Logger.debug(question.toString)
      Ok(views.html.anon.contactThanks(user))
    } catch {
      case e: Exception =>
        Logger.error("error ...", e)
        val formWithError = form.withError("email", "You must provide an email address in order to create a ticket.")
        BadRequest(html.anon.contact(user, formWithError))
    }
  }
)

在发布到该API之前,您不应该检查有效的电子邮件地址吗?盲目捕获所有异常可能会掩盖其他错误。Hi Limb,代码使用第三方电子邮件发送,我无法找到他们如何验证电子邮件。因此,我想在尝试找出所使用的验证时捕获错误。Hi@Yann Simon I'm getting value withError不是(String,String,String)的成员,正如我在视图文件@(user:Option[user],form:form[(String,String,String)])中看到的那样(是的,抱歉,您必须首先提取表单实例:val form=contactForm=contactForm.bindFromRequest form.fold(formWithErrors=>,c=>…请重试{…}catch{…val formWithError=form.withError(…)…})