asyncPost exception handling in Scala

37 Views Asked by At

I'm new to scala so I might not be clear with the issue I'm facing, I'll do my best to clarify.

In an existing codebase, there is a servlet, with a post method (org.scalatra.ScalatraBase), similar to:

val myPost: Route = post("/endpoint/", operation(myoptions)) {

If the logic in this function throws a RuntimeException, the response is serialized using the exception's toString method

I want to convert my code to asyncPost (org.scalatra.AsyncSupport), similar to:

val myPost: Route = asyncPost("/endpoint/", operation(myoptions)) {

now if the logic in this function throws the same exception, I get:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML Basic 1.1//EN" "http://www.w3.org/TR/xhtml-basic/xhtml-basic11.dtd"> 
<html lang="en">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <meta name="keywords" content="error"/>
    <title>Unexpected Failure</title>
    <style type="text/css">
...

The code looks like:

    val response = for {
      myMagic <- doMagic(parsedBody) // returns Try[Magic] <- exception thrown here is serialized correctly
      newMagic = moreMagic(myMagic) // returns Future[MoreMagic] <- exception thrown here is converted to that html response
    } yield newMagic

    response.fold(
      fa => fa,
      fb => for {
        ha <- fb
      } yield someMoreMagic(ha)
    )
}
0

There are 0 best solutions below