How to catch Execution exception in scala playframework? - forms

I'm getting Execution exception
[APIError: You must provide an email address in order to create a ticket.]
it is possible to catch this error and post only
"You must provide an email address in order to create a ticket."
this error should showup in the view page.
the code that give error :
val question = uservoice.post("/api/v1/tickets.json", ticket).getJSONObject("ticket")
Controller:
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>

you can use a try / catch block and create a form
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))
}
}
)

Related

Play Framework 2.4.1 One form with two buttons. Which button was pressed? And how do I get the input data?

I'm afraid I'm lost. Using Play Framework 2.4.1 with Scala. In my form I have two buttons:
#form(routes.Orders.handle(order.id)) {
#helper.textarea(field = userForm("name"), 'disabled -> "disabled")
#helper.checkbox(field = userForm("next"), 'class -> "largerCheckbox")
<button type="submit" name="action" value="one">Edit order</button>
<button type="submit" name="action" value="two">Remove order</button>
}
The controller:
def handle(id: Long) = Action { implicit request =>
val userData = userForm.bindFromRequest.get
request.body.asFormUrlEncoded.get("action").headOption match {
case Some("one") => Ok("Clicked edit ")
case Some("two") => Ok("Clicked remove")
case _ => BadRequest("This action is not allowed")
}
}
userForm.bindFromRequest.get throws in exception:
[NoSuchElementException: None.get]
How can I now differentiate in the controller which button has been pressed and also read in the user data?
UPDATE:
I have found a solution. Solution is perhaps saying too much, let's call it a terrible workaround:
val result = Try {request.body.asFormUrlEncoded.get("next").headOption}
val next = result match {
case Success(value) => true
case Failure(exception) => false
}
The name property can't be the same. Rename them to:
<button type="submit" name="action1" value="one">Edit order</button>
<button type="submit" name="action2" value="two">Remove order</button>

Is it harmful to throw in the subscribe onError Scala.RX an Observable<Closable>?

I am using an rx.lang.scala in a for-comprehension, which i want to fail fast. I also want the resource parameters to be closed if an exception occurs. Will the doOnTerminate execute properly if you throw the Exception / Throwable, as in the example provided below?
private def createAgreement(parameters: Params, output: ByteArrayOutputStream): Try[Unit] = Try {
output
.usedIn(AgreementCreator(parameters).createAgreement) //Observable.using(resource)(observableFactory, t => { dispose(t); IOUtils.closeQuietly(t) }, disposeEagerly)
.doOnTerminate(parameters.close()) //close resource
.toBlocking
.subscribe(_ => {},
e => throw e,
() => debug("completed"))
}

Play Framework 2.5.9 Scala handling multipart/form-data file field allways Some

I'm following documentation from here https://www.playframework.com/documentation/2.5.x/ScalaFileUpload to handle file upload in form.
My code is here:
def add(method: String) = Action(parse.multipartFormData) { implicit request =>
if (method == "GET") {
Ok(views.html.add(uploadForm))
} else {
uploadForm.bindFromRequest().fold(
hasErrors => BadRequest(views.html.add(hasErrors)),
form => {
Logger.debug(request.body.file("myfilefield").toString)
Ok(views.html.add(uploadForm))
}
)
}
}
Whenever I submit this form, even if I didn't select file in file field I'm getting something like this in console:
[debug] application -
Some(FilePart(myfilefield,,Some(application/octet-stream),TemporaryFile(/var/folders/n3/8nrp7hw94bqbsjbcz00n5hk40000gn/T/playtemp4371116121043398878/multipartBody1548294152084205009asTemporaryFile)))
I can't understand why is this so. I think there are should be None in request.body.file("myfilefield") if I didn't select file before submitting form.
How can I find if file was submitted?
Ok. Now I just copied code from documentation and got the same result :(
Here is my controller action:
def upload = Action(parse.multipartFormData) { request =>
request.body.file("picture").map { picture =>
import java.io.File
val filename = picture.filename
val contentType = picture.contentType
picture.ref.moveTo(new File(s"/tmp/picture/$filename"))
Ok("File uploaded")
}.getOrElse {
Redirect(routes.HomeController.uploadtest()).flashing(
"error" -> "Missing file")
}
}
and here is my view:
#()
#helper.form(action = routes.HomeController.upload, 'enctype -> "multipart/form-data") {
<input type="file" name="picture">
<p>
<input type="submit">
</p>
}
Whenever I submit this form I got "File uploaded" message.
I've found that this is a bug: https://github.com/playframework/playframework/issues/6203

Unable to run a POST action in play framework 2.2

I have a similar problem to this person and I am unsure why.
my post action is called from a form submission and the controller code
def processFreeDoc = UserAwareAction {
implicit request => {
userEmailForm.bindFromRequest.fold(
formWithErrors => {
Logger.error("Error processing the form %s "format formWithErrors.errors)
Redirect(routes.Application.index)
},
data => {
val sessionProduct = getSessionUnProcessedProduct(data.sessionId, data.documentName)
if(sessionProduct != null){
Logger.info("Found")
sessionProduct.updateProcessing(data.emailAddress, data.sessionId)
Redirect(routes.Application.index)
}
else
Logger.info("Nothing found")
Redirect(routes.Application.checkoutFree(data.sessionId))
}
)
}
}
is skipped entirely. There are no errors in the IDE(IDEA) console and the breakpoint at the entry of the method is not reached so none of the log messages are seen.
EDIT :
The relevant route in the routes file - POST /processFreeDoc controllers.Application.processFreeDoc

"Verifying" method for a form doesn't make an error show up

I have a standard, straightforward code for authenticating an user:
# controller
val addForm = Form(
tuple("email" -> nonEmptyText, "password" -> nonEmptyText)
.verifying("Invalid email or password", result => User.authenticate(result._1, result._2).isRight)
)
def add = Action { Ok(views.html.session.add(addForm)) }
def create = Action {
addForm.bindFromRequest.fold(
failure => BadRequest(views.html.session.add(failure)),
success => Redirect(routes.Home.index)
.flashing("key1" -> "You have logged in.")
)
}
I expected it to show me an error if NOT User.authenticate(result._1, result._2).isRight but there is no error showing up. Yes, authentication works well but there are no errors when it fails.
#helper.form(action = routes.Session.create()) {
#helper.inputText(form("email"))
#helper.inputText(form("password"))
<!-- ........... -->
}
Why?
Do you have a check in your view along the lines of
#if(addForm.hasErrors) { ... }
For example, a basic layout might be:
#if(addForm.hasErrors) {
<div class="alert-message error">
<p>Please correct the following:</p>
<ul>
#addForm.errors.map { error =>
<li>#error.key #error.message</li>
}
</ul>
</div>
}
You dont have key in form to display this error, thats why error is not showing up
Try this in failure case for nonEmptytTxt validation, email and your custom validation
BadRequest(views.html.sesion.add(failure.withError("password","Invalid email or password")))