I have a catch block that I end up repeating quite a bit, and found this SO question that confirmed that I can use a partial function as a catch block (What are the use cases for Scala 2.9's try…catch generalization? 1).
Currently, my code looks like this:
var task = newTask("update product", "update for customer " + customerId.toString)
try {
val vcdRouter = actorSystem.actorFor("user/supervisor/router-10.10.10.10:443")
val vdcId = new UUID("92ddba5e-8101-4580-b9a5-e3ee6ea5718f")
val vdcGet = sendExpect[AdminVdcType](vcdRouter, GetVdc(vdcId))
val vdcPut = VdcPutConfig(vdcGet, c)
val vdcPutTask = sendExpect[TaskType](vcdRouter, UpdateVdc(vdcId, vdcPut))
task = task.copy(Progress = 100, status = SuccessType)
} catch {
case failure: NoResponseBodyException =>
logger.debug("*** In putCustomerProduct, got a Left(VcdGatewayException)")
task = task.copy(Progress = 100, status = Error, Error = Option(exceptionToError(failure, BadGateway)))
case failure: VcdGatewayException ⇒
logger.debug("*** In putCustomerProduct, got a Left(VcdGatewayException)")
task = task.copy(Progress = 100, status = Error, Error = Option(exceptionToError(failure, GatewayTimeout)))
case failure: Exception ⇒
logger.debug("*** In putCustomerProduct, got a Left(Exception)")
task = task.copy(Progress = 100, status = Error, Error = Option(exceptionToError(failure)))
}
Since I have this task var that is changing inside the catch block, is there a nice way to make it accessible from within a partial function that holds the catch block? The task is a var since it sets some initialization data like a created timestamp upon entry into the system. I could work around this, but am interested in an answer to the original question anyway.