JulianBirch / cljs-ajax

simple asynchronous Ajax client for ClojureScript and Clojure
670 stars 136 forks source link

ajax-request handler catches all exceptions #250

Closed feral-dot-io closed 3 years ago

feral-dot-io commented 3 years ago

Any exceptions thrown inside the handler function of ajax-request get caught by a catch-all and don't bubble up.

This has resulted in bugs going unnoticed or at least, has made them harder to track down. If code in my handler fires an exception it goes unnoticed. I do a lot of work in the handler because I'm in the middle of callback hell.

Here is an example that demonstrates the problem. Running under Java / Emacs I don't see an error report just "Result ..." in the REPL.

(ajax-request
 {:method :get
  :uri "https://ifconfig.co/ip"
  :format (ajax/text-request-format)
  :response-format (ajax/detect-response-format)
  :handler (fn [[ok? result]]
             (println "Result:" ok? result)
             (throw (Exception. "I tripped")))})

I presume ajax-request has a catch all to ensure that :handler is always called. This makes a lot of sense. It doesn't make as much sense to cover the user fn though as there's no further mechanism to propagate errors.

JulianBirch commented 3 years ago

The problem is it's worse than you think: the handler gets called in an environment with no useful stacktrace. So if I don't handle it, basically no-one does. So an exception would either get lost or break your entire application (depending on exactly what platform you're running on).

Sorry for the super-slow response times.