2010-08-19 07:01:09 +02:00
|
|
|
/**
|
|
|
|
|
* 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
|
2010-08-19 07:01:09 +02:00
|
|
|
|
|
|
|
|
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
|
|
|
*
|
2010-08-19 07:01:09 +02:00
|
|
|
* @author <a href="http://jonasboner.com">Jonas Bonér</a>
|
|
|
|
|
*/
|
|
|
|
|
@serializable abstract class AkkaException(message: String) extends RuntimeException(message) {
|
2010-08-25 15:40:38 +02:00
|
|
|
import AkkaException._
|
2010-08-19 07:01:09 +02:00
|
|
|
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-19 07:01:09 +02:00
|
|
|
|
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)
|
2010-08-19 07:01:09 +02:00
|
|
|
|
|
|
|
|
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 = {
|
2010-08-19 07:01:09 +02:00
|
|
|
AkkaException.log.error(toString)
|
2010-08-25 15:40:38 +02:00
|
|
|
()
|
2010-08-19 07:01:09 +02:00
|
|
|
}
|
2010-08-25 15:40:38 +02:00
|
|
|
|
|
|
|
|
def log: Unit = _log
|
2010-08-19 07:01:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
object AkkaException extends Logging {
|
2010-08-21 16:13:16 +02:00
|
|
|
val hostname = try {
|
2010-08-19 07:01:09 +02:00
|
|
|
InetAddress.getLocalHost.getHostName
|
|
|
|
|
} catch {
|
|
|
|
|
case e: UnknownHostException => "unknown"
|
|
|
|
|
}
|
|
|
|
|
}
|