From 4bb4c101431a6402c9603d35db244c2fd5aeb4c4 Mon Sep 17 00:00:00 2001 From: Stuart Halloway Date: Mon, 17 Sep 2012 07:45:36 -0400 Subject: [PATCH] interface IExceptionInfo --- src/clj/clojure/core.clj | 8 ++++---- src/jvm/clojure/lang/ExceptionInfo.java | 2 +- src/jvm/clojure/lang/IExceptionInfo.java | 20 ++++++++++++++++++++ test/clojure/test_clojure/errors.clj | 7 +++++++ 4 files changed, 32 insertions(+), 5 deletions(-) create mode 100644 src/jvm/clojure/lang/IExceptionInfo.java diff --git a/src/clj/clojure/core.clj b/src/clj/clojure/core.clj index 4a6e61f..50fb33e 100644 --- a/src/clj/clojure/core.clj +++ b/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 diff --git a/src/jvm/clojure/lang/ExceptionInfo.java b/src/jvm/clojure/lang/ExceptionInfo.java index 1ebc83c..7ccaa0b 100644 --- a/src/jvm/clojure/lang/ExceptionInfo.java +++ b/src/jvm/clojure/lang/ExceptionInfo.java @@ -15,7 +15,7 @@ package clojure.lang; * 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) { diff --git a/src/jvm/clojure/lang/IExceptionInfo.java b/src/jvm/clojure/lang/IExceptionInfo.java new file mode 100644 index 0000000..be7dc80 --- /dev/null +++ b/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(); +} diff --git a/test/clojure/test_clojure/errors.clj b/test/clojure/test_clojure/errors.clj index 67e51d6..98cab48 100644 --- a/test/clojure/test_clojure/errors.clj +++ b/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"))))) -- 1.7.3.5