Permalink
Browse files

interface IExceptionInfo

Signed-off-by: Stuart Halloway <stu@thinkrelevance.com>
  • Loading branch information...
1 parent c1884ea commit 01b4cb7156f0b378e70020d0abe293bffe35b031 @stuarthalloway stuarthalloway committed Sep 17, 2012
View
8 src/clj/clojure/core.clj
@@ -4287,7 +4287,7 @@
(with-out-str
(apply println xs)))
-(import clojure.lang.ExceptionInfo)
+(import clojure.lang.ExceptionInfo clojure.lang.IExceptionInfo)
(defn ex-info
"Alpha - subject to change.
Create an instance of ExceptionInfo, a RuntimeException subclass
@@ -4300,12 +4300,12 @@
(defn ex-data
"Alpha - subject to change.
- Returns exception data (a map) if ex is an ExceptionInfo.
+ Returns exception data (a map) if ex is an IExceptionInfo.
Otherwise returns nil."
{:added "1.4"}
[ex]
- (when (instance? ExceptionInfo ex)
- (.getData ^ExceptionInfo ex)))
+ (when (instance? IExceptionInfo ex)
+ (.getData ^IExceptionInfo ex)))
(defmacro assert
"Evaluates expr and throws an exception if it does not evaluate to
View
2 src/jvm/clojure/lang/ExceptionInfo.java
@@ -15,7 +15,7 @@
* richer semantics for exceptions should use this in lieu of defining project-specific
* exception classes.
*/
-public class ExceptionInfo extends RuntimeException{
+public class ExceptionInfo extends RuntimeException implements IExceptionInfo {
public final IPersistentMap data;
public ExceptionInfo(String s, IPersistentMap data) {
View
20 src/jvm/clojure/lang/IExceptionInfo.java
@@ -0,0 +1,20 @@
+/**
+ * Copyright (c) Rich Hickey. All rights reserved.
+ * The use and distribution terms for this software are covered by the
+ * Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
+ * which can be found in the file epl-v10.html at the root of this distribution.
+ * By using this software in any fashion, you are agreeing to be bound by
+ * the terms of this license.
+ * You must not remove this notice, or any other, from this software.
+ */
+
+package clojure.lang;
+
+/**
+ * Interface for exceptions that carry data (a map) as additional payload. Clojure
+ * programs that need richer semantics for exceptions should use this in lieu of
+ * defining project-specific exception classes.
+ */
+public interface IExceptionInfo {
+ public IPersistentMap getData();
+}
View
7 test/clojure/test_clojure/errors.clj
@@ -45,3 +45,10 @@
(is (thrown-with-msg? IllegalArgumentException
(re-pattern (format msg-regex-str *ns*))
(macroexpand form)))))
+
+(deftest extract-ex-data
+ (try
+ (throw (ex-info "example error" {:foo 1}))
+ (catch Throwable t
+ (is (= {:foo 1} (ex-data t)))))
+ (is (nil? (ex-data (RuntimeException. "example non ex-data")))))

0 comments on commit 01b4cb7

Please sign in to comment.