Skip to content
This repository was archived by the owner on Apr 25, 2024. It is now read-only.

Commit 01b4cb7

Browse files
interface IExceptionInfo
Signed-off-by: Stuart Halloway <[email protected]>
1 parent c1884ea commit 01b4cb7

File tree

4 files changed

+32
-5
lines changed

4 files changed

+32
-5
lines changed

src/clj/clojure/core.clj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4287,7 +4287,7 @@
42874287
(with-out-str
42884288
(apply println xs)))
42894289

4290-
(import clojure.lang.ExceptionInfo)
4290+
(import clojure.lang.ExceptionInfo clojure.lang.IExceptionInfo)
42914291
(defn ex-info
42924292
"Alpha - subject to change.
42934293
Create an instance of ExceptionInfo, a RuntimeException subclass
@@ -4300,12 +4300,12 @@
43004300

43014301
(defn ex-data
43024302
"Alpha - subject to change.
4303-
Returns exception data (a map) if ex is an ExceptionInfo.
4303+
Returns exception data (a map) if ex is an IExceptionInfo.
43044304
Otherwise returns nil."
43054305
{:added "1.4"}
43064306
[ex]
4307-
(when (instance? ExceptionInfo ex)
4308-
(.getData ^ExceptionInfo ex)))
4307+
(when (instance? IExceptionInfo ex)
4308+
(.getData ^IExceptionInfo ex)))
43094309

43104310
(defmacro assert
43114311
"Evaluates expr and throws an exception if it does not evaluate to

src/jvm/clojure/lang/ExceptionInfo.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* richer semantics for exceptions should use this in lieu of defining project-specific
1616
* exception classes.
1717
*/
18-
public class ExceptionInfo extends RuntimeException{
18+
public class ExceptionInfo extends RuntimeException implements IExceptionInfo {
1919
public final IPersistentMap data;
2020

2121
public ExceptionInfo(String s, IPersistentMap data) {
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* Copyright (c) Rich Hickey. All rights reserved.
3+
* The use and distribution terms for this software are covered by the
4+
* Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
5+
* which can be found in the file epl-v10.html at the root of this distribution.
6+
* By using this software in any fashion, you are agreeing to be bound by
7+
* the terms of this license.
8+
* You must not remove this notice, or any other, from this software.
9+
*/
10+
11+
package clojure.lang;
12+
13+
/**
14+
* Interface for exceptions that carry data (a map) as additional payload. Clojure
15+
* programs that need richer semantics for exceptions should use this in lieu of
16+
* defining project-specific exception classes.
17+
*/
18+
public interface IExceptionInfo {
19+
public IPersistentMap getData();
20+
}

test/clojure/test_clojure/errors.clj

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,10 @@
4545
(is (thrown-with-msg? IllegalArgumentException
4646
(re-pattern (format msg-regex-str *ns*))
4747
(macroexpand form)))))
48+
49+
(deftest extract-ex-data
50+
(try
51+
(throw (ex-info "example error" {:foo 1}))
52+
(catch Throwable t
53+
(is (= {:foo 1} (ex-data t)))))
54+
(is (nil? (ex-data (RuntimeException. "example non ex-data")))))

0 commit comments

Comments
 (0)