Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

CLJS-429: Data Conveying Exception: ex-data and ex-info

Introduces ex-data, ex-info, ex-message and ex-cause as well as an
ExceptionInfo type; cljs.core.ExceptionInfo.prototype is made to be a
custom Error type by resetting ExceptionInfo's prototype and the
constructor property on the new prototype.

Basic tests are included.
  • Loading branch information...
commit 7ccb67e040d32b2c0112d2c73dd2e28d5e13c50a 1 parent 3842d3f
@michalmarczyk michalmarczyk authored David Nolen committed
Showing with 50 additions and 0 deletions.
  1. +42 −0 src/cljs/cljs/core.cljs
  2. +8 −0 test/cljs/cljs/core_test.cljs
View
42 src/cljs/cljs/core.cljs
@@ -7306,3 +7306,45 @@ Maps become Objects. Arbitrary keys are encoded to by key->js."
IHash
(-hash [this]
(goog.string/hashCode (pr-str this))))
+
+;;; ExceptionInfo
+
+(deftype ExceptionInfo [message data cause])
+
+;;; ExceptionInfo is a special case, do not emulate this
+(set! cljs.core.ExceptionInfo/prototype (js/Error.))
+(set! (.-constructor cljs.core.ExceptionInfo/prototype) ExceptionInfo)
+
+(defn ex-info
+ "Alpha - subject to change.
+ Create an instance of ExceptionInfo, an Error type that carries a
+ map of additional data."
+ ([msg map]
+ (ExceptionInfo. msg map nil))
+ ([msg map cause]
+ (ExceptionInfo. msg map cause)))
+
+(defn ex-data
+ "Alpha - subject to change.
+ Returns exception data (a map) if ex is an ExceptionInfo.
+ Otherwise returns nil."
+ [ex]
+ (when (instance? ExceptionInfo ex)
+ (.-data ex)))
+
+(defn ex-message
+ "Alpha - subject to change.
+ Returns the message attached to the given Error / ExceptionInfo object.
+ For non-Errors returns nil."
+ [ex]
+ (when (instance? js/Error ex)
+ (.-message ex)))
+
+(defn ex-cause
+ "Alpha - subject to change.
+ Returns exception cause (an Error / ExceptionInfo) if ex is an
+ ExceptionInfo.
+ Otherwise returns nil."
+ [ex]
+ (when (instance? ExceptionInfo ex)
+ (.-cause ex)))
View
8 test/cljs/cljs/core_test.cljs
@@ -1770,5 +1770,13 @@
(kvr-test (array-map :k0 :v0 :k1 :v1) [:k0 :v0 :k1 :v1])
(kvr-test [:v0 :v1] [0 :v0 1 :v1]))
+ ;; data conveying exception
+ (assert (= {:foo 1}
+ (try (throw (ex-info "asdf" {:foo 1}))
+ (catch ExceptionInfo e
+ (ex-data e)))))
+ (assert (instance? js/Error (ex-info "asdf" {:foo 1})))
+ (assert (not (instance? cljs.core.ExceptionInfo (js/Error.))))
+
:ok
)
Please sign in to comment.
Something went wrong with that request. Please try again.