Skip to content

Commit af58d18

Browse files
author
Gary Gregory
committed
Add and use IOExceptionList(String, List).
1 parent 1f2c9a1 commit af58d18

6 files changed

Lines changed: 54 additions & 21 deletions

File tree

src/changes/changes.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,9 @@ The <action> type attribute can be add,update,fix,remove.
154154
<action issue="IO-709" dev="ggregory" type="add" due-to="Boris Unckel, Gary Gregory">
155155
Add null safe variants of isDirectory and isRegularFile.
156156
</action>
157+
<action dev="ggregory" type="add" due-to="Gary Gregory">
158+
Add and use IOExceptionList(String, List).
159+
</action>
157160
<!-- UPDATES -->
158161
<action dev="ggregory" type="update" due-to="Dependabot">
159162
Update junit-jupiter from 5.6.2 to 5.7.0 #153.

src/main/java/org/apache/commons/io/FileUtils.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ public static void cleanDirectory(final File directory) throws IOException {
326326
}
327327

328328
if (!causeList.isEmpty()) {
329-
throw new IOExceptionList(causeList);
329+
throw new IOExceptionList(directory.toString(), causeList);
330330
}
331331
}
332332

@@ -352,7 +352,7 @@ private static void cleanDirectoryOnExit(final File directory) throws IOExceptio
352352
}
353353

354354
if (!causeList.isEmpty()) {
355-
throw new IOExceptionList(causeList);
355+
throw new IOExceptionList(directory.toString(), causeList);
356356
}
357357
}
358358

src/main/java/org/apache/commons/io/IOExceptionList.java

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,19 +41,19 @@ public class IOExceptionList extends IOException {
4141
* @param causeList a list of cause exceptions.
4242
*/
4343
public IOExceptionList(final List<? extends Throwable> causeList) {
44-
super(String.format("%,d exceptions: %s", causeList == null ? 0 : causeList.size(), causeList),
45-
causeList == null ? null : causeList.get(0));
46-
this.causeList = causeList == null ? Collections.emptyList() : causeList;
44+
this(String.format("%,d exceptions: %s", causeList == null ? 0 : causeList.size(), causeList), causeList);
4745
}
4846

4947
/**
50-
* Gets the cause list.
48+
* Creates a new exception caused by a list of exceptions.
5149
*
52-
* @param <T> type of exception to return.
53-
* @return The list of causes.
50+
* @param message The detail message, see {@link #getMessage()}.
51+
* @param causeList a list of cause exceptions.
52+
* @since 2.9.0
5453
*/
55-
public <T extends Throwable> List<T> getCauseList() {
56-
return (List<T>) causeList;
54+
public IOExceptionList(final String message, final List<? extends Throwable> causeList) {
55+
super(message, causeList == null ? null : causeList.get(0));
56+
this.causeList = causeList == null ? Collections.emptyList() : causeList;
5757
}
5858

5959
/**
@@ -79,6 +79,16 @@ public <T extends Throwable> T getCause(final int index, final Class<T> clazz) {
7979
return (T) causeList.get(index);
8080
}
8181

82+
/**
83+
* Gets the cause list.
84+
*
85+
* @param <T> type of exception to return.
86+
* @return The list of causes.
87+
*/
88+
public <T extends Throwable> List<T> getCauseList() {
89+
return (List<T>) causeList;
90+
}
91+
8292
/**
8393
* Works around Throwable and Generics, may fail at runtime depending on the argument value.
8494
*

src/main/java/org/apache/commons/io/file/PathUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -940,7 +940,7 @@ public static Path setReadOnly(final Path path, final boolean readOnly, final Li
940940
}
941941
}
942942
if (!causeList.isEmpty()) {
943-
throw new IOExceptionList(causeList);
943+
throw new IOExceptionList(path.toString(), causeList);
944944
}
945945
throw new IOException(
946946
String.format("No DosFileAttributeView or PosixFileAttributeView for '%s' (linkOptions=%s)", path,

src/main/java/org/apache/commons/io/output/FilterCollectionWriter.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public Writer append(final char c) throws IOException {
8888
i++;
8989
}
9090
if (!causeList.isEmpty()) {
91-
throw new IOExceptionList(causeList);
91+
throw new IOExceptionList("append", causeList);
9292
}
9393
return this;
9494
}
@@ -108,7 +108,7 @@ public Writer append(final CharSequence csq) throws IOException {
108108
i++;
109109
}
110110
if (!causeList.isEmpty()) {
111-
throw new IOExceptionList(causeList);
111+
throw new IOExceptionList("append", causeList);
112112
}
113113
return this;
114114
}
@@ -129,7 +129,7 @@ public Writer append(final CharSequence csq, final int start, final int end) thr
129129
i++;
130130
}
131131
if (!causeList.isEmpty()) {
132-
throw new IOExceptionList(causeList);
132+
throw new IOExceptionList("append", causeList);
133133
}
134134
return this;
135135
}
@@ -149,7 +149,7 @@ public void close() throws IOException {
149149
i++;
150150
}
151151
if (!causeList.isEmpty()) {
152-
throw new IOExceptionList(causeList);
152+
throw new IOExceptionList("close", causeList);
153153
}
154154

155155
}
@@ -174,7 +174,7 @@ public void flush() throws IOException {
174174
i++;
175175
}
176176
if (!causeList.isEmpty()) {
177-
throw new IOExceptionList(causeList);
177+
throw new IOExceptionList("flush", causeList);
178178
}
179179

180180
}
@@ -203,7 +203,7 @@ public void write(final char[] cbuf, final int off, final int len) throws IOExce
203203
i++;
204204
}
205205
if (!causeList.isEmpty()) {
206-
throw new IOExceptionList(causeList);
206+
throw new IOExceptionList("write", causeList);
207207
}
208208
}
209209

@@ -222,7 +222,7 @@ public void write(final char[] cbuf) throws IOException {
222222
i++;
223223
}
224224
if (!causeList.isEmpty()) {
225-
throw new IOExceptionList(causeList);
225+
throw new IOExceptionList("write", causeList);
226226
}
227227
}
228228

@@ -246,7 +246,7 @@ public void write(final int c) throws IOException {
246246
i++;
247247
}
248248
if (!causeList.isEmpty()) {
249-
throw new IOExceptionList(causeList);
249+
throw new IOExceptionList("write", causeList);
250250
}
251251
}
252252

@@ -265,7 +265,7 @@ public void write(final String str) throws IOException {
265265
i++;
266266
}
267267
if (!causeList.isEmpty()) {
268-
throw new IOExceptionList(causeList);
268+
throw new IOExceptionList("write", causeList);
269269
}
270270

271271
}
@@ -294,7 +294,7 @@ public void write(final String str, final int off, final int len) throws IOExcep
294294
i++;
295295
}
296296
if (!causeList.isEmpty()) {
297-
throw new IOExceptionList(causeList);
297+
throw new IOExceptionList("write", causeList);
298298
}
299299

300300
}

src/test/java/org/apache/commons/io/IOExceptionListTestCase.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@
2929

3030
import org.junit.jupiter.api.Test;
3131

32+
/**
33+
* Tests {@link IOExceptionList}.
34+
*/
3235
public class IOExceptionListTestCase {
3336

3437
@Test
@@ -46,6 +49,23 @@ public void testCause() {
4649
assertEquals(list, causeList);
4750
}
4851

52+
@Test
53+
public void testMessageCause() {
54+
final EOFException cause = new EOFException();
55+
final List<EOFException> list = Collections.singletonList(cause);
56+
final IOExceptionList sqlExceptionList = new IOExceptionList("Hello", list);
57+
assertEquals("Hello", sqlExceptionList.getMessage());
58+
//
59+
assertEquals(cause, sqlExceptionList.getCause());
60+
assertEquals(cause, sqlExceptionList.getCause(0));
61+
assertEquals(list, sqlExceptionList.getCauseList());
62+
assertEquals(list, sqlExceptionList.getCauseList(EOFException.class));
63+
assertEquals(cause, sqlExceptionList.getCause(0, EOFException.class));
64+
// No CCE:
65+
final List<EOFException> causeList = sqlExceptionList.getCauseList();
66+
assertEquals(list, causeList);
67+
}
68+
4969
@Test
5070
public void testNullCause() {
5171
final IOExceptionList sqlExceptionList = new IOExceptionList(null);

0 commit comments

Comments
 (0)