From fa7cf3c33f17e9d7e518ef09efcb4f423af78ad0 Mon Sep 17 00:00:00 2001 From: jlangch Date: Sat, 30 Mar 2024 15:46:58 +0100 Subject: [PATCH] added support for tracing exceptions in `trace/trace-var` --- ChangeLog.md | 1 + .../modules/ModuleTracingSection.java | 2 +- .../com/github/jlangch/venice/trace.venice | 25 ++++++++++++++++--- 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/ChangeLog.md b/ChangeLog.md index 82e65bdae..8a2f2d91e 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -13,6 +13,7 @@ All notable changes to this project will be documented in this file. - function `io/file-normalize-utf` - thread type support to the function `thread` to allow spawning daemon or user threads +- support for tracing exceptions in `trace/trace-var` diff --git a/src/main/java/com/github/jlangch/venice/impl/docgen/cheatsheet/modules/ModuleTracingSection.java b/src/main/java/com/github/jlangch/venice/impl/docgen/cheatsheet/modules/ModuleTracingSection.java index 69446f6a0..62b544e67 100644 --- a/src/main/java/com/github/jlangch/venice/impl/docgen/cheatsheet/modules/ModuleTracingSection.java +++ b/src/main/java/com/github/jlangch/venice/impl/docgen/cheatsheet/modules/ModuleTracingSection.java @@ -45,7 +45,7 @@ public DocSection section() { final DocSection trace = new DocSection("Tracing", id()); all.addSection(trace); trace.addItem(diBuilder.getDocItem("trace/trace")); - trace.addItem(diBuilder.getDocItem("trace/trace-var")); + trace.addItem(diBuilder.getDocItem("trace/trace-var", true, true)); trace.addItem(diBuilder.getDocItem("trace/untrace-var")); final DocSection test = new DocSection("Test", id()); diff --git a/src/main/resources/com/github/jlangch/venice/trace.venice b/src/main/resources/com/github/jlangch/venice/trace.venice index 83f018d36..c6109ee1b 100644 --- a/src/main/resources/com/github/jlangch/venice/trace.venice +++ b/src/main/resources/com/github/jlangch/venice/trace.venice @@ -54,9 +54,13 @@ (let [id (gensym "t")] (tracer id (str (trace-indent) (pr-str (cons name args)))) (binding [*trace-depth* (inc *trace-depth*)] - (let [result (apply f args)] - (tracer id (str (trace-indent) "=> " (pr-str result))) - result)))) + (try + (let [result (apply f args)] + (tracer id (str (trace-indent) "=> " (pr-str result))) + result) + (catch :Exception e + (tracer id (str (trace-indent) "=> " (pr-str e))) + (throw e)))))) (defn @@ -150,7 +154,7 @@ (t/trace-var +) (+ 1 2)) - """ + """, """ (do (load-module :trace ['trace :as 't]) @@ -163,6 +167,19 @@ (t/trace-var foo) (t/trace-var bar) + (bar 5)) + """, + """ + (do + (load-module :trace ['trace :as 't]) + + (defn foo [x] (/ x 0)) ;; division by zero! + (defn bar [x] (foo x)) + + (t/trace-var /) + (t/trace-var foo) + (t/trace-var bar) + (bar 5)) """ ) :see-also '(