Fork me on GitHub
#reitit
<
2023-12-11
>
dvingo23:12:24

does anyone use reitit with coercion on the frontend? The behavior in reitit.frontend.history calls reitit.frontend/match-by-path which https://github.com/metosin/reitit/blob/620d0c271175a4e11d91d922b26c8162660db3f9/modules/reitit-frontend/src/reitit/frontend.cljs#L63C30-L63C34 if coercion fails. If I try to wrap the router in a try catch then using {:href (rfe/href nme),,, fails because there is no history. I don't see how this is usable for a client-side app as uncaught exceptions cause the page to crash. Any ideas? I guess I can just coerce in on-navigate on my own using data in the route.

dvingo00:12:56

Solved it by copying the implementation of reitit.coercion/request-coercer and commenting out the throw call:

(defn request-coercer [coercion type model {::keys [extract-request-format parameter-coercion serialize-failed-result]
                                            :or {extract-request-format reitit.coercion/extract-request-format-default
                                                 parameter-coercion reitit.coercion/default-parameter-coercion}}]
  (if coercion
    (if-let [{:keys [keywordize? open? in style]} (parameter-coercion type)]
      (let [transform (comp (if keywordize? walk/keywordize-keys identity) in)
            model (if open? (reitit.coercion/-open-model coercion model) model)]
        (if-let [coercer (reitit.coercion/-request-coercer coercion style model)]
          (fn [request]
            (let [value (transform request)
                  format (extract-request-format request)
                  result (coercer value format)]
              (if (reitit.coercion/error? result)
                result
                ;(request-coercion-failed! result coercion value in request serialize-failed-result)
                result))))))))
would be awesome if this could be parameterized when creating the frontend router