Created
August 24, 2022 16:01
-
-
Save quoll/acb206d2387d5c4f0ae690781eace6d6 to your computer and use it in GitHub Desktop.
Error->map function for ClojureScript
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
(ns error.core) | |
(defn name-of | |
"Identifies the name of an error object, the configured name of a type, or just the object type name" | |
[o] | |
(or (.-name o) | |
(let [t (type o) | |
type-name (.-name t)] | |
(if (seq type-name) | |
type-name | |
(pr-str t))))) | |
(defn Error->map | |
"Convert a thrown object into a map representation" | |
[e] | |
(merge {:type (symbol (name-of e)) | |
:message (or (.-message e) (str e))} | |
(when-let [data (ex-data e)] | |
{:data data}) | |
(when-let [cause (.-cause e)] | |
{:cause (Error->map cause)}) | |
(when-let [file (.-fileName e)] | |
{:filename file}) | |
(when-let [line (.-lineNumber e)] | |
{:line-number line}) | |
(when-let [column (.-columnNumber e)] | |
{:column-number column}) | |
(when-let [stack (.-stack e)] | |
(when (seq stack) {:stack stack})))) | |
(def Throwable->map Error->map) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment