Fork me on GitHub
#ring-swagger
<
2018-07-13
>
Empperi07:07:04

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}}}

Empperi07:07:33

no matter what I put in :handlers the handler functions are not called but they end up into going to default handlers

Empperi07:07:20

I wonder if this is a bug caused by spec integration or am I missing something obvious here?

Empperi07:07:52

For example, if I replace the :type based dispatch with exception type and put Exception it still doesn't do anything

Empperi07:07:39

ping @ikitommi I know you are most likely on vacation but I'm still bugging you 🙂

valtteri09:07:57

How are you throwing the exceptions?

valtteri09:07:13

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}))

valtteri09:07:33

..but as you can see, there’s no spec coercion happening in my example.

Empperi11:07:36

yup, same thing here with the exception of having spec coercion