Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed #69: A better way to show stack trace of error message .

  • Loading branch information...
commit 7b1dbfe9bd75b737a2862454923425fb68cd12a3 1 parent 5ea789e
@zhongl zhongl authored
View
21 src/main/scala/com/github/zhongl/housemd/command/Last.scala
@@ -0,0 +1,21 @@
+package com.github.zhongl.housemd.command
+
+import com.github.zhongl.yascli.{PrintOut, Command}
+
+/**
+ * @author <a href="mailto:zhong.lunfu@gmail.com">zhongl<a>
+ */
+class Last(out: PrintOut) extends Command("last","show exception stack trace of last error.", out) {
+
+ private var stackTraces:Array[StackTraceElement] = _
+
+ def keep(throwable: Throwable) {
+ stackTraces = throwable.getStackTrace
+ }
+
+
+ def run() {
+ if (stackTraces != null) stackTraces foreach { s => println("\t" + s) }
+ else println("There is no error.")
+ }
+}
View
10 src/main/scala/com/github/zhongl/housemd/duck/Telephone.scala
@@ -24,6 +24,7 @@ import jline.TerminalFactory
import jline.console.history.FileHistory
import management.ManagementFactory
import java.io.File
+import com.github.zhongl.housemd.command.Last
/**
* Telephone is used by Duck to communicate with HouseMD.
@@ -32,6 +33,7 @@ import java.io.File
*/
class Telephone(inst: Instrumentation, port: Int, classes: Array[Class[Command]]) extends Runnable {
+
def run() {
val socket = new Socket("localhost", port)
val reader = new ConsoleReader(socket.getInputStream, socket.getOutputStream)
@@ -40,13 +42,13 @@ class Telephone(inst: Instrumentation, port: Int, classes: Array[Class[Command]]
try {
new Shell(name = "housemd", description = "a runtime diagnosis tool of jvm.", reader = reader) {
- override protected def commands = Quit :: helpCommand :: classes.map {toCommand(_, PrintOut(reader))}.toList
+ private val lastCommand = new Last(out)
+
+ override protected def commands = Quit :: helpCommand :: lastCommand :: classes.map {toCommand(_, PrintOut(reader))}.toList
override def error(a: Any) {
super.error(a)
- if (a.isInstanceOf[Throwable]) {
- a.asInstanceOf[Throwable].getStackTrace foreach { s => println("\t" + s) }
- }
+ if (a.isInstanceOf[Throwable]) lastCommand.keep(a.asInstanceOf[Throwable])
}
} main (Array.empty[String])
View
35 src/test/scala/com/github/zhongl/housemd/command/LastSpec.scala
@@ -0,0 +1,35 @@
+package com.github.zhongl.housemd.command
+
+import org.scalatest.FunSpec
+import org.scalatest.matchers.ShouldMatchers
+import java.io.ByteArrayOutputStream
+import com.github.zhongl.yascli.PrintOut
+import org.mockito.Mockito._
+import instrument.Instrumentation
+
+/**
+ * @author <a href="mailto:zhong.lunfu@gmail.com">zhongl<a>
+ */
+class LastSpec extends FunSpec with ShouldMatchers {
+ describe("Last") {
+ it("should show exception stack traces of last error") {
+ val bout = new ByteArrayOutputStream()
+ val last = new Last(PrintOut(bout))
+ val throwable = new Exception()
+ last.keep(throwable)
+ last.parse(Array.empty[String])
+ last.run()
+ bout.toString should be(throwable.getStackTrace.map("\t" + _) mkString("", "\n", "\n"))
+ }
+
+ it("should show no exception stacks trace with out last error"){
+ val bout = new ByteArrayOutputStream()
+ val last = new Last(PrintOut(bout))
+ last.parse(Array.empty[String])
+ last.run()
+ bout.toString should be ("There is no error.\n")
+ }
+
+ }
+
+}
Please sign in to comment.
Something went wrong with that request. Please try again.