metosin / reitit

A fast data-driven routing library for Clojure/Script
https://cljdoc.org/d/metosin/reitit/
Eclipse Public License 1.0
1.43k stars 257 forks source link

Coerce-exceptions-middleware converts Exceptions into async failures too eagerly #424

Open mjhanninen opened 4 years ago

mjhanninen commented 4 years ago

The coerce-exceptions-middleware converts thrown Exceptions that are unrelated to coercion failures (for example, an ArithmeticException) into asynchronous failures terminating their stack unwinding process too early. This happens is because coercion-exception-handler is passed the asynchronous failure callback raise (instead of, say, #(throw %)) on the line 87 of the following snippet:

https://github.com/metosin/reitit/blob/43e1a520d60aefa6bd7c67316b92f170c1e6d447/modules/reitit-ring/src/reitit/ring/coercion.cljc#L84-L87

miikka commented 4 years ago

This seems like a bug to me. Fixing it would be a minor breaking change. As a workaround, users could include a middleware like this in their pipeline before coercion:

(defn convert-exceptions-to-async-middleware
  [handler]
  (fn
    ([request] (hander request))
    ([request respond raise]
      (try
        (handler request respond raise)
        (catch #?(:clj Exception :cljs js/Error) e
          (raise e))))))