Custom Errors/Exceptions in Babashka #1554
Unanswered
NoahTheDuke
asked this question in
Q&A
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I've finished adapting @IGJoshua’s library Farolero to babashka (PR), but I thought I'd ask for a bit of advice before we merge.
In Clojure, Farolero relies on a custom java class extending Error (Signal.java) to hold additional data and skip the runtime cost of building a stack trace.
In Clojurescript, it relies on a record (
(defrecord Signal ...)
) as javascript allows throwing any object.Neither of these solutions exist in Babashka: we can't use a custom java class nor can we throw anything as it has to be an instance of Error or an instance of a subclass of Error. The solution Joshua came up with and I implemented is to use the same record from Clojurescript, and then attach it to an
ExceptionInfo
as the data and use that as the cause for an instance ofjava.lang.Error
. This allows us to catch all Errors and only rethrow if(some-> ex ex-cause ex-data)
doesn't implement the correct protocol.The big issue is one of speed: we're creating an
ExceptionInfo
(with associated stack trace) and then we're creating anError
(with associated stack trace). That's a lot of extra work to be immediately thrown away when the exception is caught.Are there any ways around the extra work of creating these stack traces in Babashka or creating custom Error classes?
Beta Was this translation helpful? Give feedback.
All reactions