Skip to content

Commit

Permalink
Fix issue #321: the result of a Future can be another Future, so we u…
Browse files Browse the repository at this point in the history
…nroll them until we have an actual result
  • Loading branch information
rossabaker committed Nov 24, 2013
1 parent 3fd37f9 commit f87f9a9
Showing 1 changed file with 26 additions and 19 deletions.
45 changes: 26 additions & 19 deletions core/src/main/scala/org/scalatra/FutureSupport.scala
Original file line number Diff line number Diff line change
Expand Up @@ -71,26 +71,33 @@ trait FutureSupport extends AsyncSupport {
def onStartAsync(event: AsyncEvent) {}
})

f onComplete {
case a {
withinAsyncContext(context) {
if (gotResponseAlready.compareAndSet(false, true)) {
try {
a.right.map(renderResponse(_)).left.map {
case e: HaltException
renderHaltException(e)
case e
try {
renderResponse(errorHandler(e))
} catch {
case e: Throwable =>
ScalatraBase.runCallbacks(Left(e))
renderUncaughtException(e)
ScalatraBase.runRenderCallbacks(Left(e))
}
renderFutureResult(f)

def renderFutureResult(f: Future[_]) {
f onComplete {
// Loop until we have a non-future result
case Right(f2: Future[_]) => renderFutureResult(f2)
case Right(r: AsyncResult) => renderFutureResult(r.is)
case a {
withinAsyncContext(context) {
if (gotResponseAlready.compareAndSet(false, true)) {
try {
a.right.map(renderResponse(_)).left.map {
case e: HaltException
renderHaltException(e)
case e
try {
renderResponse(errorHandler(e))
} catch {
case e: Throwable =>
ScalatraBase.runCallbacks(Left(e))
renderUncaughtException(e)
ScalatraBase.runRenderCallbacks(Left(e))
}
}
} finally {
context.complete()
}
} finally {
context.complete()
}
}
}
Expand Down

0 comments on commit f87f9a9

Please sign in to comment.