Skip to content

Instantly share code, notes, and snippets.

@quoll
Created August 24, 2022 16:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save quoll/acb206d2387d5c4f0ae690781eace6d6 to your computer and use it in GitHub Desktop.
Save quoll/acb206d2387d5c4f0ae690781eace6d6 to your computer and use it in GitHub Desktop.
Error->map function for ClojureScript
(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