sbt/util/control/ErrorHandling.scala

44 lines
1.1 KiB
Scala
Raw Normal View History

2010-02-08 05:45:19 +01:00
/* sbt -- Simple Build Tool
* Copyright 2009 Mark Harrah
*/
package sbt
2009-08-16 20:29:08 +02:00
2011-11-04 18:44:09 +01:00
import java.io.IOException
2009-08-16 20:29:08 +02:00
object ErrorHandling
{
def translate[T](msg: => String)(f: => T) =
try { f }
catch {
case e: IOException => throw new TranslatedIOException(msg + e.toString, e)
case e: Exception => throw new TranslatedException(msg + e.toString, e)
}
2009-08-16 20:29:08 +02:00
def wideConvert[T](f: => T): Either[Throwable, T] =
try { Right(f) }
catch
{
case ex @ (_: Exception | _: StackOverflowError) => Left(ex)
case err @ (_: ThreadDeath | _: VirtualMachineError) => throw err
case x => Left(x)
}
2009-08-16 20:29:08 +02:00
def convert[T](f: => T): Either[Exception, T] =
try { Right(f) }
catch { case e: Exception => Left(e) }
def reducedToString(e: Throwable): String =
if(e.getClass == classOf[RuntimeException])
{
val msg = e.getMessage
if(msg == null || msg.isEmpty) e.toString else msg
}
else
e.toString
2009-08-16 20:29:08 +02:00
}
sealed class TranslatedException private[sbt](msg: String, cause: Throwable) extends RuntimeException(msg, cause)
2009-08-16 20:29:08 +02:00
{
override def toString = msg
}
final class TranslatedIOException private[sbt](msg: String, cause: IOException) extends TranslatedException(msg, cause)