Skip to content

Instantly share code, notes, and snippets.

Last active April 7, 2020 11:37
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save codonnell/5a095c9ba0207c6b1c7a4581f3a458ab to your computer and use it in GitHub Desktop.
Error handling with fulcro and pathom
;; Example error-action
(error-action [{:keys [state result] :as env}]
(let [message (get-in env [:result :body `create-gift ::p/error ::anom/message])]
(swap! state
#(-> %
(assoc-in [::gift/id id :ui/submitting] false)
(assoc-in [:component/id :flash-message] #:ui{:active true
:message message
:type "negative"})))))
;; Assoc errors to ::p/error key using ::p/env in parser definition
::p/env {::p/reader [p/map-reader pc/parallel-reader
pc/open-ident-reader p/env-placeholder-reader]
::p/process-error (fn [env e]
(log/error (p/error-str e))
(log/error (with-out-str (.printStackTrace e)))
{::p/error (ex-data e)})
::p/placeholder-prefixes #{">"}}
;; Detect errors in parser results
(defn wrap-api [handler uri]
(fn [request]
(if (= uri (:uri request))
(let [result (parser/parser {:ring/request request}
(:transit-params request))
errors (sequence (keep (comp ::p/error val)) result)]
(log/debug "result" result)
(assoc-in {:status (if (seq errors) 500 200) :body result}
[:headers "Content-Type"] "application/transit+json"))
(handler request))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment