11package io .github .notstirred .dasm .exception .wrapped ;
22
33import io .github .notstirred .dasm .exception .DasmException ;
4+ import io .github .notstirred .dasm .exception .EKind ;
45import io .github .notstirred .dasm .util .IndentingStringBuilder ;
56
67import java .util .ArrayList ;
78import java .util .Iterator ;
89import java .util .List ;
9- import java .util .function . Consumer ;
10+ import java .util .stream . Stream ;
1011
1112public abstract class DasmExceptionData {
1213 private final List <DasmExceptionData > nested = new ArrayList <>();
@@ -24,7 +25,12 @@ public <T extends DasmExceptionData> T addNested(T e) {
2425 }
2526
2627 public boolean hasWrapped () {
27- return !this .exceptions .isEmpty () || this .nested .stream ().anyMatch (DasmExceptionData ::hasWrapped );
28+ return this .hasWrapped (EKind .ERROR );
29+ }
30+
31+ public boolean hasWrapped (EKind minimumKind ) {
32+ return this .exceptions .stream ().anyMatch (e -> e .kind .isAtLeast (minimumKind ))
33+ || this .nested .stream ().anyMatch (nested -> nested .hasWrapped (minimumKind ));
2834 }
2935
3036 /**
@@ -49,29 +55,31 @@ private boolean removeEmpty() {
4955
5056 public void throwIfHasWrapped () throws DasmException {
5157 if (this .removeEmpty ()) {
52- DasmException dasmException = new DasmException (exceptionMessage (new IndentingStringBuilder (4 )));
53- this .forAllCauses (dasmException ::addSuppressed );
54- throw dasmException ;
58+ if (this .hasWrapped (EKind .ERROR )) {
59+ DasmException dasmException = new DasmException (exceptionMessage (new IndentingStringBuilder (4 ), EKind .ERROR ));
60+ this .getCauses ().forEach (dasmException ::addSuppressed );
61+ throw dasmException ;
62+ } else {
63+ System .err .println (exceptionMessage (new IndentingStringBuilder (4 ), EKind .INFO ));
64+ }
5565 }
5666 }
5767
58- private void forAllCauses (Consumer <DasmException > consumer ) {
59- this .nested .forEach (nested -> nested .forAllCauses (consumer ));
60- this .exceptions .forEach (consumer );
68+ private Stream <DasmException > getCauses () {
69+ return Stream .concat (this .nested .stream ().flatMap (DasmExceptionData ::getCauses ), this .exceptions .stream ());
6170 }
6271
63- private String exceptionMessage (IndentingStringBuilder builder ) {
72+ private String exceptionMessage (IndentingStringBuilder builder , EKind minimumKind ) {
6473 builder .appendLine (this .message ()).indent ();
6574
6675 this .nested .forEach (nested -> {
67- if (nested .hasWrapped ()) {
68- nested .exceptionMessage (builder );
76+ if (nested .hasWrapped (minimumKind )) {
77+ nested .exceptionMessage (builder , minimumKind );
6978 }
7079 });
7180
72- this .exceptions .forEach (exception ->
73- builder .appendLine (exception .getMessage ())
74- );
81+ this .exceptions .stream ().filter (e -> e .kind .isAtLeast (minimumKind ))
82+ .forEach (exception -> builder .appendLine (exception .getMessage ()));
7583
7684 builder .unindent ();
7785 return builder .toString ();
0 commit comments