0

I'm trying to catch some errors with try and i'm getting some error i dont know why, this is the code:

class Application extends Controller {

  val ds: DataSource = CsvDataSource
  val purchaseDS = PurchaseInfo.fromDataSource(ds)_


  def index = Action { implicit request =>
    Ok(views.html.index())
  }

  def redirectToIndex = Action {
    Redirect(routes.Application.index)
  }

  case class csvUploadData(clientUrl: String)
  val csvUploadForm = Form(
    mapping(
      "clientUrl" -> nonEmptyText)(csvUploadData.apply)(csvUploadData.unapply))

  def uploadCSV = Action.async(parse.multipartFormData) { implicit request =>
    csvUploadForm.bindFromRequest.fold(
      formWithErrors => {
        Future {
          Redirect(routes.Application.index).flashing(
            "error" -> formWithErrors.error("clientUrl").get.message)
        }
      },
      userData => {
        request.body.file("csvFile").fold(Future {
          Redirect(routes.Application.index).flashing(
            "error" -> "Missing CSV file").withSession(request.session)
        }) { formFile =>
          import java.io.File
          val filename = formFile.filename
          Future {
            val file = formFile.ref.file
            val purchaseInfos = purchaseDS(file)

            val t = Try {
              val driver: WebDriver = new FirefoxDriver
              val actions: ActionsHMRC = new ActionsHMRC(driver, userData.clientUrl)

              val results = actions.insertData(purchaseInfos)
              results.filter(_._2.isFailure)
            }
            t match {
              case Success(failures) =>
                val failedMsg = if (failures.nonEmpty) failures.mkString("The following rows failed: [",",","]") else ""
                Redirect(routes.Application.index).flashing(
                "success" -> s"The file '$filename' automation successfuly.\n$failedMsg")
              case Failure(e) =>
                println(e)
                Redirect(routes.Application.index).flashing(
                "error" -> s"The file '$filename' automation failed.")
            }
          }
        }
      })
  }
}

ActionsHMRC method:

def insertData(purchaseInfos: Seq[PurchaseInfo]) = {
    login()
    purchaseInfos.map { case purchaseInfo =>
      (purchaseInfo, Try(doActions(purchaseInfo)))
    }
    println("done insertData function")
  }

those lines are colored red (so there is something wrong with them) and I don't understand why...

filter(_._2

nonEmpty

mkString

if you know maybe what is going on it will help allot, thanksss

nick shmick
  • 905
  • 1
  • 9
  • 25
  • Can you provide a working code sample? There are a bunch of extra braces in your code. You're more likely to get help if I can copy/paste your code and get the exact error you are without having to clean it up first. – Brian Jun 18 '15 at 18:08
  • I'm not able to find any definition for ActionsHMRC, can you post the type signature of the ActionsHMRC.insertData() function? or perhaps the actual errors you are getting (your "colored red" comment makes me think you're using an IDE, usually hovering over the red will give you an error message) – Angelo Genovese Jun 18 '15 at 18:26
  • @AngeloGenovese when I hover it say's "cannot resolve symbol filter" and the same thing for mkString and nonEmpty. added the insertData method to the code – nick shmick Jun 18 '15 at 18:34

1 Answers1

3

insertData, as written returns Unit. Unit does not have a filter method.

I suggest adding a return type to the method (and any public method), it would help the compiler give you error messages closer to where the error actually is.

Angelo Genovese
  • 3,398
  • 17
  • 23