Skip to content

Commit c0f17c8

Browse files
committed
core: Allow errors to specify themselves as warnings
1 parent 39cddf6 commit c0f17c8

File tree

4 files changed

+44
-15
lines changed

4 files changed

+44
-15
lines changed
Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
package io.github.notstirred.dasm.exception;
22

33
public class DasmException extends Exception {
4+
public final EKind kind;
5+
46
public DasmException(String message) {
57
super(message);
8+
this.kind = EKind.ERROR;
69
}
710

8-
protected DasmException() { }
11+
public DasmException(String message, EKind kind) {
12+
super(message);
13+
this.kind = kind;
14+
}
915
}

src/main/java/io/github/notstirred/dasm/exception/DasmTransformException.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,8 @@ public abstract class DasmTransformException extends DasmException {
44
public DasmTransformException(String message) {
55
super(message);
66
}
7+
8+
public DasmTransformException(String message, EKind kind) {
9+
super(message, kind);
10+
}
711
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package io.github.notstirred.dasm.exception;
2+
3+
public enum EKind {
4+
INFO,
5+
WARNING,
6+
ERROR;
7+
8+
public boolean isAtLeast(EKind kind) {
9+
return this.ordinal() >= kind.ordinal();
10+
}
11+
}

src/main/java/io/github/notstirred/dasm/exception/wrapped/DasmExceptionData.java

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package io.github.notstirred.dasm.exception.wrapped;
22

33
import io.github.notstirred.dasm.exception.DasmException;
4+
import io.github.notstirred.dasm.exception.EKind;
45
import io.github.notstirred.dasm.util.IndentingStringBuilder;
56

67
import java.util.ArrayList;
78
import java.util.Iterator;
89
import java.util.List;
9-
import java.util.function.Consumer;
10+
import java.util.stream.Stream;
1011

1112
public 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

Comments
 (0)