Skip to content

Commit 6fabd7b

Browse files
committed
Provide meaningful message for MultitargetException
1 parent 3c2a541 commit 6fabd7b

File tree

4 files changed

+24
-50
lines changed

4 files changed

+24
-50
lines changed

src/main/java/net/tascalate/concurrent/AggregatingPromise.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,15 @@ void onComplete(int idx, T result, Throwable error) {
175175
cancelPromises();
176176
}
177177

178-
failure(new MultitargetException(errors));
178+
failure(new MultitargetException(
179+
String.format(
180+
c == 1 ?
181+
"Aggregated promise was completed exceptionally (1 out of %d)"
182+
:
183+
"Aggregated promises were completed exceptionally (%2$d out of %1$d)",
184+
promises.size(), c),
185+
errors
186+
));
179187
}
180188
}
181189
}

src/main/java/net/tascalate/concurrent/MultitargetException.java

Lines changed: 2 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import java.io.IOException;
1919
import java.io.PrintStream;
2020
import java.io.PrintWriter;
21-
import java.io.StringWriter;
2221
import java.util.Collections;
2322
import java.util.IdentityHashMap;
2423
import java.util.List;
@@ -34,7 +33,8 @@ public class MultitargetException extends Exception {
3433

3534
private final List<Throwable> exceptions;
3635

37-
public MultitargetException(List<Throwable> exceptions) {
36+
public MultitargetException(String message, List<Throwable> exceptions) {
37+
super(message);
3838
this.exceptions = exceptions == null ?
3939
Collections.emptyList()
4040
:
@@ -67,41 +67,6 @@ public List<Throwable> getExceptions() {
6767
Optional<Throwable> getFirstException() {
6868
return exceptions.stream().filter(Objects::nonNull).findFirst();
6969
}
70-
71-
public static MultitargetException of(final Throwable exception) {
72-
return new MultitargetException(Collections.singletonList(exception));
73-
}
74-
75-
@Override
76-
public String toString() {
77-
// Exclude getMessage / getLocalizedMessage to avoid infinite recursion
78-
return getClass().getName();
79-
}
80-
81-
@Override
82-
public String getMessage() {
83-
StringWriter w = new StringWriter();
84-
printDetails(new PrintWriter(w), newDejavueSet());
85-
return w.toString();
86-
}
87-
88-
void printDetails(PrintWriter w, Set<Throwable> visited) {
89-
visited.add(this);
90-
w.println(this);
91-
printExceptions(w, (ex, padding) -> {
92-
PrintWriter pw = new PrintWriter(new PaddedWriter(w, padding), true);
93-
if (visited.contains(ex)) {
94-
pw.println("\t[CIRCULAR REFERENCE:" + ex + "]");
95-
} else {
96-
if (ex instanceof MultitargetException) {
97-
((MultitargetException)ex).printDetails(pw, visited);
98-
} else {
99-
pw.println(ex.toString());
100-
}
101-
}
102-
});
103-
104-
}
10570

10671
public void printExceptions() {
10772
printExceptions(System.err);

src/main/java/net/tascalate/concurrent/Promises.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -423,16 +423,14 @@ private static <T> Function<Promise<T>, Promise<T>> onCloseSource(Object source)
423423

424424
} catch (RuntimeException | Error ex) {
425425
if (null != e) {
426-
e.addSuppressed(ex);
427-
} else {
428-
throw ex;
426+
ex.addSuppressed(e);
429427
}
428+
throw ex;
430429
} catch (Exception ex) {
431430
if (null != e) {
432-
e.addSuppressed(ex);
433-
} else {
434-
throw new CompletionException(ex);
431+
ex.addSuppressed(e);
435432
}
433+
throw new CompletionException(ex);
436434
}
437435
}, true);
438436
} else {
@@ -1186,7 +1184,10 @@ private static <E extends Throwable> MultitargetException wrapMultitargetExcepti
11861184
if (exception instanceof MultitargetException) {
11871185
return (MultitargetException)exception;
11881186
} else {
1189-
return MultitargetException.of(exception);
1187+
return new MultitargetException(
1188+
"Aggregated promise was completed exceptionally (1 out of 1)",
1189+
Collections.singletonList(exception)
1190+
);
11901191
}
11911192
}
11921193

src/test/java/net/tascalate/concurrent/TestMultitargetExceptionTraces.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ public static void main(String[] argv) {
1111
//Throwable e = outer().initCause(err());
1212
//e.printStackTrace(new PrintWriter(System.err, true));
1313
e.printStackTrace();
14-
System.out.println("--------------");
15-
System.out.println(e.getLocalizedMessage());
16-
System.out.println("--------------");
14+
System.err.println("--------------");
15+
System.err.println(e.getLocalizedMessage());
16+
System.err.println("--------------");
1717
e.printExceptions();
1818
}
1919

@@ -26,12 +26,12 @@ static Exception err() {
2626
}
2727

2828
static Exception err_1() {
29-
MultitargetException e = new MultitargetException(Arrays.asList(
29+
MultitargetException e = new MultitargetException("First message", Arrays.asList(
3030
null,
3131
null,
3232
b(), null,
3333
a(), null,
34-
new MultitargetException(Arrays.asList(c(), b()))
34+
new MultitargetException("Another message", Arrays.asList(c(), b()))
3535
));
3636
//e.fillInStackTrace();
3737
return e;

0 commit comments

Comments
 (0)