Skip to content

Commit f228ac9

Browse files
authored
update!: changed the type of the reason field from Record to Object (#6)
1 parent d2b3c7f commit f228ac9

File tree

3 files changed

+69
-36
lines changed

3 files changed

+69
-36
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@ A library for handling exceptions with reasons.
55
In Java programming, it is cumbersome to implement a separate exception class for each exception case.
66
However, trying to handle multiple exception cases with a single exception class makes it difficult to distinguish between them.
77

8-
The exception class `Exc` provided by this library solves this problem by accepting a `Record` object that represents the reason for the exception.
8+
The exception class `Exc` provided by this library solves this problem by accepting an object that represents the reason for the exception.
9+
Typically, the type of this reason object is `Record`.
910
Since a `Record` object can have any fields, it can store information about the situation at the time the exception occurred.
10-
The type of the `Record` object can be determined and cast using a switch statement, making it easy to write handling logic for each exception case.
11+
The type of the reason can be determined and cast using a switch statement, making it easy to write handling logic for each exception case.
1112

1213
Optionally, when an `Exc` object is instantiated, pre-registered exception handlers can receive notifications either synchronously or asynchronously.
13-
However, to enable this feature, you must specify the system property `-Dgithub.sttk.errs.notify=true` at program startup.
14+
However, to enable this feature, the system property `-Dgithub.sttk.errs.notify=true` must be specified at program startup.
1415

1516
## Install
1617

src/main/java/com/github/sttk/errs/Exc.java

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@
1818
/**
1919
* Is the exception class with a reason.
2020
* <p>
21-
* This class has a record field which indicates a reason for this exception. The class name of the reason record
22-
* represents the type of reason, and the fields of the reason record hold the situation where the exception occurred.
21+
* This class has a field which indicates a reason for this exception. Typically the type of this field is
22+
* {@link Record}. In this case, the class name of this record represents the reason, and the fields of the record hold
23+
* the situation where the exception occurred.
2324
* <p>
2425
* Optionally, this exception class can notify its instance creation to pre-registered exception handlers. This
2526
* notification feature can be enabled by specifying the system property {@code -Dgithub.sttk.errs.notify=true} when the
@@ -44,18 +45,18 @@ public final class Exc extends Exception {
4445
private static final long serialVersionUID = 260427082865587554L;
4546

4647
/** The reason for this exception. */
47-
private transient Record reason;
48+
private transient Object reason;
4849

4950
/** The stack trace for the location of occurrence. */
5051
private StackTraceElement trace;
5152

5253
/**
53-
* Is the constructor which takes a {@link Record} object indicating the reason for this exception.
54+
* Is the constructor which takes an object indicating the reason for this exception.
5455
*
5556
* @param reason
5657
* A reason for this exception.
5758
*/
58-
public Exc(final Record reason) {
59+
public Exc(final Object reason) {
5960
if (reason == null) {
6061
throw new IllegalArgumentException("reason is null");
6162
}
@@ -67,16 +68,16 @@ public Exc(final Record reason) {
6768
}
6869

6970
/**
70-
* Is the constructor which takes a {@link Record} object indicating the reason and {@link Throwable} object
71-
* indicating the cause for this exception.
71+
* Is the constructor which takes an object indicating the reason and {@link Throwable} object indicating the cause
72+
* for this exception.
7273
*
7374
* @param reason
7475
* A reason for this exception.
7576
* @param cause
7677
* A cause for this exception.
7778
*/
7879
@SuppressWarnings("this-escape")
79-
public Exc(final Record reason, final Throwable cause) {
80+
public Exc(final Object reason, final Throwable cause) {
8081
super(cause);
8182

8283
if (reason == null) {
@@ -94,7 +95,7 @@ public Exc(final Record reason, final Throwable cause) {
9495
*
9596
* @return The reason for this exception.
9697
*/
97-
public Record getReason() {
98+
public Object getReason() {
9899
return this.reason;
99100
}
100101

@@ -105,13 +106,7 @@ public Record getReason() {
105106
*/
106107
@Override
107108
public String getMessage() {
108-
var rsn = this.reason.toString();
109-
var rname = this.reason.getClass().getSimpleName();
110-
rsn = rsn.substring(rname.length() + 1, rsn.length() - 1);
111-
112-
var buf = new StringBuilder(this.reason.getClass().getName());
113-
buf.append(" { ").append(rsn).append(" }");
114-
return buf.toString();
109+
return reason.toString();
115110
}
116111

117112
/**
@@ -123,7 +118,7 @@ public String getMessage() {
123118
@Override
124119
public String toString() {
125120
var buf = new StringBuilder(getClass().getName());
126-
buf.append(" { reason = ").append(getMessage());
121+
buf.append(" { reason = ").append(reason.getClass().getName()).append(" ").append(reason.toString());
127122
buf.append(", file = ").append(this.trace.getFileName());
128123
buf.append(", line = ").append(this.trace.getLineNumber());
129124
if (getCause() != null) {
@@ -166,8 +161,8 @@ public RuntimeException toRuntimeException() {
166161
/**
167162
* Writes a serial data of this exception to a stream.
168163
* <p>
169-
* Since a {@link Record} object is not necessarily serializable, this method will throw a
170-
* {@link NotSerializableException} if the {@code reason} field does not inherit {@link Serializable}.
164+
* Since a reason object is not necessarily serializable, this method will throw a {@link NotSerializableException}
165+
* if the {@code reason} field does not inherit {@link Serializable}.
171166
*
172167
* @param out
173168
* An {@link ObjectOutputStream} to which data is written.
@@ -185,8 +180,7 @@ private void writeObject(ObjectOutputStream out) throws IOException {
185180

186181
/**
187182
* Reconstitutes the {@code Exc} instance from a stream and initialize the reason and cause properties when
188-
* deserializing. If the reason by deserialization is null or invalid, this method throws
189-
* {@link InvalidObjectException}.
183+
* deserializing. If the reason by deserialization is null, this method throws {@link InvalidObjectException}.
190184
*
191185
* @param in
192186
* An {@link ObjectInputStream} from which data is read.
@@ -198,7 +192,7 @@ private void writeObject(ObjectOutputStream out) throws IOException {
198192
*/
199193
private void readObject(ObjectInputStream in) throws ClassNotFoundException, IOException {
200194
in.defaultReadObject();
201-
this.reason = Record.class.cast(in.readObject());
195+
this.reason = in.readObject();
202196

203197
if (this.reason == null) {
204198
throw new InvalidObjectException("reason is null or invalid.");

src/test/java/com/github/sttk/errs/ExcTest.java

Lines changed: 49 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import static org.assertj.core.api.Assertions.assertThat;
44
import static org.junit.jupiter.api.Assertions.fail;
5+
import org.junit.jupiter.api.Disabled;
56
import org.junit.jupiter.api.Nested;
67
import org.junit.jupiter.api.Test;
78

@@ -31,7 +32,7 @@ record SerializableReason(String name, int index, int min, int max) implements S
3132
@Nested
3233
class TestConstructor {
3334
@Test
34-
void with_reason() {
35+
void with_Record_reason() {
3536
var exc = new Exc(new IndexOutOfRange("data", 4, 0, 3));
3637
var reason = IndexOutOfRange.class.cast(exc.getReason());
3738
assertThat(reason.name()).isEqualTo("data");
@@ -43,6 +44,28 @@ void with_reason() {
4344
// exc.printStackTrace();
4445
}
4546

47+
@Test
48+
void with_enum_reason() {
49+
enum Reasons {
50+
FailToDoSomething,
51+
}
52+
53+
var exc = new Exc(Reasons.FailToDoSomething);
54+
var reason = Reasons.class.cast(exc.getReason());
55+
assertThat(reason.name()).isEqualTo("FailToDoSomething");
56+
assertThat(exc.getCause()).isNull();
57+
58+
// exc.printStackTrace();
59+
}
60+
61+
@Test
62+
void with_String_reason() {
63+
var exc = new Exc("FailToDoSomething");
64+
var reason = String.class.cast(exc.getReason());
65+
assertThat(reason).isEqualTo("FailToDoSomething");
66+
assertThat(exc.getCause()).isNull();
67+
}
68+
4669
@Test
4770
void with_reason_but_reason_is_null() {
4871
try {
@@ -104,7 +127,7 @@ void identify_reason_with_instanceOf() {
104127
}
105128

106129
@Test
107-
void identify_reason_with_switch_expression() {
130+
void identify_Record_reason_with_switch_expression() {
108131
var exc = new Exc(new IndexOutOfRange("data", 4, 0, 3));
109132
switch (exc.getReason()) {
110133
case IndexOutOfRange reason -> {
@@ -116,6 +139,24 @@ void identify_reason_with_switch_expression() {
116139
default -> fail();
117140
}
118141
}
142+
143+
@Test
144+
void identify_Enum_reason_with_switch_expression() {
145+
enum Reasons {
146+
FailToDoSomething, InvalidValue,
147+
}
148+
149+
var exc = new Exc(Reasons.FailToDoSomething);
150+
151+
var s = switch (exc.getReason()) {
152+
case Reasons enm -> switch (enm) {
153+
case FailToDoSomething -> "fail to do something";
154+
case InvalidValue -> "invalid value";
155+
};
156+
default -> "unknown";
157+
};
158+
assertThat(s).isEqualTo("fail to do something");
159+
}
119160
}
120161

121162
@Nested
@@ -151,7 +192,7 @@ void getFile() {
151192
@Test
152193
void getLine() {
153194
var exc = new Exc(new IndexOutOfRange("data", 4, 0, 3));
154-
assertThat(exc.getLine()).isEqualTo(153);
195+
assertThat(exc.getLine()).isEqualTo(194);
155196
}
156197
}
157198

@@ -160,16 +201,14 @@ class TestGetMessage {
160201
@Test
161202
void with_cause() {
162203
var exc = new Exc(new IndexOutOfRange("data", 4, 0, 3));
163-
assertThat(exc.getMessage())
164-
.isEqualTo("com.github.sttk.errs.ExcTest$IndexOutOfRange { name=data, index=4, min=0, max=3 }");
204+
assertThat(exc.getMessage()).isEqualTo("IndexOutOfRange[name=data, index=4, min=0, max=3]");
165205
}
166206

167207
@Test
168208
void with_no_cause() {
169209
var cause = new IndexOutOfBoundsException(4);
170210
var exc = new Exc(new IndexOutOfRange("data", 4, 0, 3), cause);
171-
assertThat(exc.getMessage())
172-
.isEqualTo("com.github.sttk.errs.ExcTest$IndexOutOfRange { name=data, index=4, min=0, max=3 }");
211+
assertThat(exc.getMessage()).isEqualTo("IndexOutOfRange[name=data, index=4, min=0, max=3]");
173212
}
174213
}
175214

@@ -179,15 +218,15 @@ class TestToString {
179218
void with_reason() {
180219
var exc = new Exc(new IndexOutOfRange("data", 4, 0, 3));
181220
assertThat(exc.toString()).isEqualTo(
182-
"com.github.sttk.errs.Exc { reason = com.github.sttk.errs.ExcTest$IndexOutOfRange { name=data, index=4, min=0, max=3 }, file = ExcTest.java, line = 180 }");
221+
"com.github.sttk.errs.Exc { reason = com.github.sttk.errs.ExcTest$IndexOutOfRange IndexOutOfRange[name=data, index=4, min=0, max=3], file = ExcTest.java, line = 219 }");
183222
}
184223

185224
@Test
186225
void with_reason_and_cause() {
187226
var cause = new IndexOutOfBoundsException(4);
188227
var exc = new Exc(new IndexOutOfRange("data", 4, 0, 3), cause);
189228
assertThat(exc.toString()).isEqualTo(
190-
"com.github.sttk.errs.Exc { reason = com.github.sttk.errs.ExcTest$IndexOutOfRange { name=data, index=4, min=0, max=3 }, file = ExcTest.java, line = 188, cause = java.lang.IndexOutOfBoundsException: Index out of range: 4 }");
229+
"com.github.sttk.errs.Exc { reason = com.github.sttk.errs.ExcTest$IndexOutOfRange IndexOutOfRange[name=data, index=4, min=0, max=3], file = ExcTest.java, line = 227, cause = java.lang.IndexOutOfBoundsException: Index out of range: 4 }");
191230
}
192231
}
193232

@@ -197,8 +236,7 @@ class TestToRuntimeException {
197236
void getMessage() {
198237
var exc = new Exc(new IndexOutOfRange("data", 4, 0, 3));
199238
var rtExc = exc.toRuntimeException();
200-
assertThat(rtExc.getMessage())
201-
.isEqualTo("com.github.sttk.errs.ExcTest$IndexOutOfRange { name=data, index=4, min=0, max=3 }");
239+
assertThat(rtExc.getMessage()).isEqualTo("IndexOutOfRange[name=data, index=4, min=0, max=3]");
202240
}
203241

204242
@Test

0 commit comments

Comments
 (0)