Last active
April 7, 2020 11:37
-
-
Save codonnell/5a095c9ba0207c6b1c7a4581f3a458ab to your computer and use it in GitHub Desktop.
Error handling with fulcro and pathom
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; 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"}))))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; 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 #{">"}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; 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