I am not able to get exceptions to my error-handlers, my api
config map:
{:coercion :spec
:formats (-> muuntaja/default-options
(assoc-in [:formats "application/json" :encoder-opts]
{:encode-key-fn #(name (kebab/->camelCase %))})
(assoc-in [:formats "application/json" :decoder-opts]
{:decode-key-fn #(keyword (kebab/->kebab-case %))}))
:exceptions {:handlers {:error/space-upgrade-not-possible known-exception-handler}}}
no matter what I put in :handlers
the handler functions are not called but they end up into going to default handlers
I wonder if this is a bug caused by spec integration or am I missing something obvious here?
For example, if I replace the :type
based dispatch with exception type and put Exception
it still doesn't do anything
ping @ikitommi I know you are most likely on vacation but I'm still bugging you 🙂
How are you throwing the exceptions?
For reference, I have my api setup like this
(defn exception-handler [resp-fn type]
(fn [^Exception e data request]
(resp-fn {:message (.getMessage e)
:type type})))
(def exception-handlers
{:username-conflict (exception-handler conflict :username-conflict)
:email-conflict (exception-handler conflict :email-conflict)})
(defn create-app [{:keys [db]}]
(api
{:exceptions
{:handlers exception-handlers}}
(OPTIONS "/api/*" []
:middleware [mw/cors]
(ok {}))
(GET "/api/" [] (resource-response "index.html" {:root "public"}))
(GET "/api/health" [] (ok {:status "OK"}))
(POST "/api/actions/register" req
:middleware [mw/cors]
(let [_ (core/add-user db (:body-params req))]
(created "/fixme" {:status "OK"})))
(POST "/api/actions/login" req
:middleware [(mw/basic-auth db) mw/cors mw/auth]
(ok (:identity req)))))
And throwing exceptions like this works fine:
(throw (ex-info "Username is already in use!"
{:type :username-conflict}))
..but as you can see, there’s no spec coercion happening in my example.
yup, same thing here with the exception of having spec coercion