pekko/akka-actor/src/main/scala/akka/AkkaException.scala

51 lines
1.6 KiB
Scala
Raw Normal View History

/**
2012-01-19 18:21:06 +01:00
* Copyright (C) 2009-2012 Typesafe Inc. <http://www.typesafe.com>
*/
package akka
import akka.actor.newUuid
import java.net.{ InetAddress, UnknownHostException }
2011-07-26 18:33:59 +12:00
object AkkaException {
val hostname = try InetAddress.getLocalHost.getHostAddress catch { case e: UnknownHostException "unknown" }
def toStringWithStackTrace(throwable: Throwable): String = {
if (throwable eq null) "Unknown Exception"
throwable match {
case ae: AkkaException ae.toLongString
case e "%s:%s\n%s" format (e.getClass.getName, e.getMessage, stackTraceToString(e))
}
}
def stackTraceToString(throwable: Throwable): String = {
val trace = throwable.getStackTrace
val sb = new StringBuilder
for (i 0 until trace.length)
sb.append("\tat %s\n" format trace(i))
sb.toString
}
}
/**
* Akka base Exception. Each Exception gets:
* <ul>
* <li>a uuid for tracking purposes</li>
* <li>toString that includes exception name, message and uuid</li>
* <li>toLongString which also includes the stack trace</li>
* </ul>
*/
2011-04-29 17:15:00 +02:00
class AkkaException(message: String = "", cause: Throwable = null) extends RuntimeException(message, cause) with Serializable {
val uuid = "%s_%s".format(AkkaException.hostname, newUuid)
override lazy val toString =
"%s:%s\n[%s]".format(getClass.getName, message, uuid)
lazy val toLongString =
"%s:%s\n[%s]\n%s".format(getClass.getName, message, uuid, stackTraceToString)
2011-07-26 18:33:59 +12:00
def this(msg: String) = this(msg, null);
2011-07-25 12:47:43 +03:00
def stackTraceToString = AkkaException.stackTraceToString(this)
}