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

53 lines
1.3 KiB
Scala
Raw Normal View History

/**
* Copyright (C) 2009-2010 Scalable Solutions AB <http://scalablesolutions.se>
*/
package se.scalablesolutions.akka
2010-09-17 16:04:25 +02:00
import se.scalablesolutions.akka.util.Logging
import se.scalablesolutions.akka.actor.newUuid
import java.io.{StringWriter, PrintWriter}
import java.net.{InetAddress, UnknownHostException}
/**
* Akka base Exception. Each Exception gets:
* <ul>
* <li>a UUID for tracking purposes</li>
* <li>a message including exception name, uuid, original message and the stacktrace</li>
* <li>a method 'log' that will log the exception once and only once</li>
* </ul>
2010-08-21 16:13:16 +02:00
*
* @author <a href="http://jonasboner.com">Jonas Bon&#233;r</a>
*/
@serializable abstract class AkkaException(message: String) extends RuntimeException(message) {
2010-08-25 15:40:38 +02:00
import AkkaException._
val exceptionName = getClass.getName
2010-08-21 16:13:16 +02:00
2010-09-17 16:04:25 +02:00
val uuid = "%s_%s".format(hostname, newUuid)
2010-08-25 15:40:38 +02:00
override val toString = "%s\n\t[%s]\n\t%s\n\t%s".format(exceptionName, uuid, message, stackTrace)
val stackTrace = {
val sw = new StringWriter
val pw = new PrintWriter(sw)
printStackTrace(pw)
sw.toString
}
2010-08-25 15:40:38 +02:00
private lazy val _log = {
AkkaException.log.error(toString)
2010-08-25 15:40:38 +02:00
()
}
2010-08-25 15:40:38 +02:00
def log: Unit = _log
}
object AkkaException extends Logging {
2010-08-21 16:13:16 +02:00
val hostname = try {
InetAddress.getLocalHost.getHostName
} catch {
case e: UnknownHostException => "unknown"
}
}