A long-lost feature of Visual Basic 3, lovingly recreated in Scala
The Guardian's content API is implemented in Scala. A short while ago we were working on the process that extracts information from our internal database and inserts it into the Apache Solr index that backs the API, and a few different places we wanted to ignore and log errors, but continue indexing anyway.
This brought back memories of programming in Visual Basic 3 and VBScript where On Error Resume Next was the ultimate (and often dangerous) solution to stopping error message boxes popping up.
Scala being an extensible language, we thought it was time to reintroduce this concept:
import org.slf4j.LoggerFactory
import scala.util.control.Exception._
trait Logging {
protected def log = LoggerFactory.getLogger(getClass)
protected def ignoreAndLogExceptions = catching(classOf[Exception]) withApply (
e => log.warn("ignoring exception", e))
protected def onErrorResumeNext = ignoreAndLogExceptions
}
Which meant we could write:
override def execute(context: JobExecutionContext) = onErrorResumeNext {
indexer.incremental
}
Now it's not quite On Error Resume Next because the code inside the block does stop executing when it encounters an error rather than blindly carrying on, and we at least record the error in the log, but it amused us to introduce this blast from the past into the code base. We were so excited we tweeted about it, only for our friendly architect to reply "You're fired". We're still here at the moment, though ...
Lorri Bagley Leslie Bega Maria Sharapova Lindsay Price Zoe Saldana
No comments:
Post a Comment