Skip to content

Instantly share code, notes, and snippets.

@jto
Created May 24, 2013 14:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jto/5644008 to your computer and use it in GitHub Desktop.
Save jto/5644008 to your computer and use it in GitHub Desktop.
def getHandlerFor(request: RequestHeader): Either[Result, (Handler, Application)] = {
import scala.util.control.Exception
def sendHandler: Either[Throwable, (Handler, Application)] = {
try {
applicationProvider.get.right.map { application =>
val maybeAction = application.global.onRouteRequest(request)
(maybeAction.getOrElse(Action(BodyParsers.parse.empty)(_ => application.global.onHandlerNotFound(request))), application)
}
} catch {
case e: ThreadDeath => throw e
case e: VirtualMachineError => throw e
case e: Throwable => Left(e)
}
}
def logExceptionAndGetResult(e: Throwable) = {
Logger.error(
"""
|
|! %sInternal server error, for (%s) [%s] ->
|""".stripMargin.format(e match {
case p: PlayException => "@" + p.id + " - "
case _ => ""
}, request.method, request.uri),
e)
DefaultGlobal.onError(request, e)
}
Exception
.allCatch[Option[Result]]
.either(applicationProvider.handleWebCommand(request))
.left.map(logExceptionAndGetResult)
.right.flatMap(maybeResult => maybeResult.toLeft(())).right.flatMap { _ =>
sendHandler.left.map(logExceptionAndGetResult)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment