MichaelDrogalis / dire

Erlang-style supervisor error handling for Clojure
483 stars 19 forks source link

Support for multiple exception selectors in one handler? #33

Closed oubiwann closed 8 years ago

oubiwann commented 8 years ago

I'd like to be able to do something like this:

(with-handler! #'init
  [java.lang.IllegalArgumentException, java.lang.InstantiationException]
  (fn [e & args]
    (println "[ERROR] Cannot instantiate ...")
    (println (str {:args args :errors e}))))

A possibly naive solution jumps to mind: check iexception-selector with vector?, and if so, (map #(with-handler ... % ...) exception-selector)

Thoughts?

(If you're okay with the proposition, I can branch & submit PR)

MichaelDrogalis commented 8 years ago

Seems fine, a PR would be great - thanks!

oubiwann commented 8 years ago

Well, I'd forgotten about the custom selectors (which are vectors), so the solution I've put together is different than the simple proposition in the ticket description. That being said, this is much less hacky (so I'm happy about that).

It did require a change to the private function selector-type, though -- the check for :key-values was originally vector? and ensuring there were an even number of elements. This would obviously conflict with an even number of exceptions. So I added a third requirement: every other element must be a keyword for it to be a :key-values. This then left room for a new selector type: :class-names.

To pair with that, I added a new selector-matches? method that checks that the given object is an instance of at least one of the elements in the vector of exception classes.

Caveat about midje: I've only used it once before! Let me know if you'd prefer the added tests to done differently :-) (as expected, though: added tests fail without the changes and pass with them).

Note that when I submit the PR, I'll paste some of this text there for reference ...

oubiwann commented 8 years ago

Ah, one thing I missed -- updating the README. I'll push an update shortly ...