error trying to get errors with scala Try function

102 views Asked by At

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

1

There are 1 answers

1
Angelo Genovese On BEST ANSWER

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.