You want this on every single fn you define? in this case, yes you would
need a tweaked 'defn' form...something like this perhaps:
(defmacro defn-capt [name [& args] & code]
`(defn ~name [~@args]
(try ~@code
(catch Exception e# {:exception e#
:origin ~name
:inputs (vector ~@args)}))))
(defn-capt foo [a b] (/ a b))
(foo 1 0) => {:inputs [1 0],
:exception #<ArithmeticException
java.lang.ArithmeticException: Divide by zero>,
:origin #<user$foo user$foo@4dbf98eb>}
Jim
On 04/09/12 12:31, Jim - FooBar(); wrote:
I'm so sorry... this one works!
(defmacro capture-inputs [f & args]
`(try (~f ~@args)
(catch Exception e# (do (println "oops!") {:e e# :inputs (vector
~@args)}))))
Jim
On 04/09/12 12:23, Jim - FooBar(); wrote:
oops gensym mistake!
(defmacro capture-inputs [f & args]
`(try (apply ~f ~@args)
(catch Exception e# (do (println "oops!") {:e e# :inputs (vec
~args)}))))
Jim
On 04/09/12 12:20, Jim - FooBar(); wrote:
you want the exception thrown to report the arguments or 'capture
them' (as you say) in the actual Exception object so you can use
them outside the try-catch scope? the former is very straight
forward you just use your arguments...the latter is more fuss as you
have to gen-class your own exception type and and pass the arguments
to its constructor so they can be stored internally...at least this
is what i understand from your question...
Jim
p.s: I just realised you can simply return a map
(defn foo [x y]
(try (doSomething x y)
(catch Exception e {:e e :inputs [x y]}))) ;;return the map
(defmacro capture-inputs [f & args]
`(try (apply ~f ~@args)
(catch Exception e (do (println "oops!") {:e e :inputs (vec
~args)}))))
On 04/09/12 05:03, Timothy Pratley wrote:
I'm working on a project where it would be quite convenient to have
the input values to the function reported in any exceptions that
get thrown. I'm guessing the way to do this would be to create a
replacement defn which had a try catch and threw a new error with
the inputs captured. Has anyone tried this, know of a better way,
or a library that helps in this way? --
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
Note that posts from new members are moderated - please be patient
with your first post.
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
--
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
Note that posts from new members are moderated - please be patient with your
first post.
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