ring-swagger

ring-swagger & compojure-api
Empperi 2018-07-13T07:24:04.000192Z

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

Empperi 2018-07-13T07:24:33.000118Z

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

Empperi 2018-07-13T07:25:20.000280Z

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

Empperi 2018-07-13T07:25:52.000173Z

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

Empperi 2018-07-13T07:26:39.000039Z

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

valtteri 2018-07-13T09:06:57.000274Z

How are you throwing the exceptions?

valtteri 2018-07-13T09:09:13.000006Z

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

valtteri 2018-07-13T09:11:33.000044Z

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

Empperi 2018-07-13T11:53:36.000005Z

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