On Sat, Mar 7, 2009 at 10:31 AM, Stuart Sierra
<the.stuart.sie...@gmail.com> wrote:
>
> On Mar 6, 5:14 pm, Mark Volkmann <r.mark.volkm...@gmail.com> wrote:
>> What's the recommended way to use this library in code that is run
>> outside a REPL to simplify stack traces so they focus on Clojure code
>> instead of Java code?
>
> Hi Mark,
>
> I wrote that library so I could use it in test-is, so I'm not sure if
> it will help you there.  One thing it explicitly doesn't try to do is
> omit non-Clojure-related stack frames from the stack trace.  It just
> rewrites Clojure-related frames so that they look more like Clojure
> code.
>
>> (try
>>   (main)
>>   (catch Exception e (print-stack-trace e 5)))
>
> In your example, you probably want "print-cause-trace" instead of
> "print-stack-trace".  You probably also need a number much higher than
> 5 to get useful information.
>
> Someone else put a more elaborate stack trace library on github, you
> could search the list for it.

Thanks! Just using print-cause-trace is a major improvement. For example:

(use 'clojure.contrib.stacktrace)
(defn main [] (/ 5 0))
(main)

produces:

java.lang.ArithmeticException: Divide by zero (stacktrace.clj:0)
        at clojure.lang.Compiler.eval(Compiler.java:4533)
        at clojure.lang.Compiler.load(Compiler.java:4846)
        at clojure.lang.Compiler.loadFile(Compiler.java:4813)
        at clojure.main$load_script__5681.invoke(main.clj:206)
        at clojure.main$script_opt__5712.invoke(main.clj:258)
        at clojure.main$main__5736$fn__5738.invoke(main.clj:333)
        at clojure.main$main__5736.doInvoke(main.clj:328)
        at clojure.lang.RestFn.invoke(RestFn.java:441)
        at clojure.lang.Var.invoke(Var.java:354)
        at clojure.lang.AFn.applyToHelper(AFn.java:179)
        at clojure.lang.Var.applyTo(Var.java:463)
        at clojure.main.main(main.java:39)
Caused by: java.lang.ArithmeticException: Divide by zero
        at clojure.lang.Numbers.divide(Numbers.java:138)
        at user$main__32.invoke(stacktrace.clj:4)
        at user$eval__35.invoke(stacktrace.clj:7)
        at clojure.lang.Compiler.eval(Compiler.java:4522)
        ... 11 more

Yuck!  If I instead call main like this:

(try
  (main)
  (catch Exception e (print-cause-trace e 5)))

then the output is reduced to this, making the offending line easier to locate:

java.lang.ArithmeticException: Divide by zero
 at clojure.lang.Numbers.divide (Numbers.java:138)
    user/main (stacktrace.clj:4)
    user/eval (stacktrace.clj:7)
    clojure.lang.Compiler.eval (Compiler.java:4522)
    clojure.lang.Compiler.load (Compiler.java:4846)

-- 
R. Mark Volkmann
Object Computing, Inc.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To post to this group, send email to clojure@googlegroups.com
To unsubscribe from this group, send email to 
clojure+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to