Skip to content

Commit eb5b986

Browse files
committed
improve error messages
1 parent 10c1b95 commit eb5b986

30 files changed

+170
-82
lines changed

core/src/main/java/net/jbock/coerce/CoercionProvider.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public Coercion findCoercion(
5757
return handle(optionalInfo, basicInfo, paramName, mapperClass, collectorClass);
5858
} catch (TmpException e) {
5959
throw e.asValidationException(sourceMethod);
60-
} catch (SearchHintException e) {
60+
} catch (UnknownTypeException e) {
6161
Optional<String> hint = HintProvider.instance().findHint(optionalInfo, basicInfo);
6262
throw hint.map(m -> e.asValidationException(sourceMethod, m))
6363
.orElseGet(() -> e.asValidationException(sourceMethod));
@@ -69,7 +69,7 @@ private Coercion handle(
6969
BasicInfo basicInfo,
7070
String paramName,
7171
TypeElement mapperClass,
72-
TypeElement collectorClass) throws TmpException, SearchHintException {
72+
TypeElement collectorClass) throws TmpException, UnknownTypeException {
7373
boolean auto = mapperClass == null;
7474
if (basicInfo.repeatable) {
7575
if (auto) {
@@ -89,7 +89,7 @@ private Coercion handle(
8989
// no mapper, not repeatable
9090
private Coercion handleSingleAuto(
9191
Optional<TypeMirror> optionalInfo,
92-
BasicInfo basicInfo) throws TmpException, SearchHintException {
92+
BasicInfo basicInfo) throws TmpException, UnknownTypeException {
9393
CoercionFactory factory = findCoercion(optionalInfo.orElse(basicInfo.returnType()));
9494
return factory.getCoercion(basicInfo, optionalInfo, Optional.empty());
9595
}
@@ -122,20 +122,20 @@ private Coercion handleRepeatable(
122122
// repeatable without mapper
123123
private Coercion handleRepeatableAuto(
124124
TypeElement collectorClass,
125-
BasicInfo basicInfo) throws TmpException, SearchHintException {
125+
BasicInfo basicInfo) throws TmpException, UnknownTypeException {
126126
CollectorInfo collectorInfo = collectorInfo(basicInfo.returnType(), collectorClass);
127127
CoercionFactory coercion = findCoercion(collectorInfo.inputType);
128128
return coercion.getCoercion(basicInfo, Optional.empty(), collectorInfo.collectorType());
129129
}
130130

131-
private CoercionFactory findCoercion(TypeMirror mirror) throws TmpException, SearchHintException {
131+
private CoercionFactory findCoercion(TypeMirror mirror) throws TmpException, UnknownTypeException {
132132
CoercionFactory standardCoercion = StandardCoercions.get(mirror);
133133
if (standardCoercion != null) {
134134
return standardCoercion;
135135
}
136136
boolean isEnum = isEnumType(mirror);
137137
if (!isEnum) {
138-
throw SearchHintException.create("Unknown parameter type. Define a custom mapper.");
138+
throw UnknownTypeException.create();
139139
}
140140
return EnumCoercion.create(mirror);
141141
}
@@ -166,11 +166,11 @@ private CollectorInfo collectorInfo(
166166
}
167167
TypeTool tool = TypeTool.get();
168168
if (!tool.isSameErasure(returnType, List.class)) {
169-
throw TmpException.create("Either define a custom collector, or return List");
169+
throw TmpException.create("Either define a custom collector, or return List.");
170170
}
171171
List<? extends TypeMirror> typeParameters = tool.typeargs(returnType);
172172
if (typeParameters.isEmpty()) {
173-
throw TmpException.create("Either define a custom collector, or return List");
173+
throw TmpException.create("Add a type parameter.");
174174
}
175175
return CollectorInfo.listCollector(typeParameters.get(0));
176176
}

core/src/main/java/net/jbock/coerce/CollectorInfo.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import javax.lang.model.type.TypeMirror;
44
import java.util.Optional;
55

6-
public class CollectorInfo {
6+
class CollectorInfo {
77

88
final TypeMirror inputType;
99

core/src/main/java/net/jbock/coerce/Extension.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import javax.lang.model.element.TypeElement;
44
import javax.lang.model.type.DeclaredType;
55

6-
public class Extension {
6+
class Extension {
77

88
private final TypeElement baseClass;
99
private final DeclaredType extensionClass;
@@ -13,11 +13,11 @@ public class Extension {
1313
this.extensionClass = extensionClass;
1414
}
1515

16-
public TypeElement baseClass() {
16+
TypeElement baseClass() {
1717
return baseClass;
1818
}
1919

20-
public DeclaredType extensionClass() {
20+
DeclaredType extensionClass() {
2121
return extensionClass;
2222
}
2323

core/src/main/java/net/jbock/coerce/LiftedType.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import java.util.OptionalLong;
1212
import java.util.function.Function;
1313

14-
public final class LiftedType {
14+
final class LiftedType {
1515

1616
private final TypeMirror originalType;
1717

core/src/main/java/net/jbock/coerce/SuppliedClassValidator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ static void commonChecks(TypeElement classToCheck, String name) throws TmpExcept
1818
throw TmpException.create(
1919
String.format("The %s class may not be private.", name));
2020
}
21-
if (!Util.checkDefaultConstructorExists(classToCheck)) {
21+
if (!Util.hasDefaultConstructor(classToCheck)) {
2222
throw TmpException.create(
2323
String.format("The %s class must have a default constructor", name));
2424
}

core/src/main/java/net/jbock/coerce/SearchHintException.java renamed to core/src/main/java/net/jbock/coerce/UnknownTypeException.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@
44

55
import javax.lang.model.element.ExecutableElement;
66

7-
class SearchHintException extends Exception {
7+
class UnknownTypeException extends Exception {
88

9-
static SearchHintException create(String message) {
10-
return new SearchHintException(message);
9+
private static final String DEFAULT_MESSAGE = "Unknown parameter type. Define a custom mapper.";
10+
11+
static UnknownTypeException create() {
12+
return new UnknownTypeException();
1113
}
1214

13-
private SearchHintException(String message) {
14-
super(message);
15+
private UnknownTypeException() {
16+
super(DEFAULT_MESSAGE);
1517
}
1618

1719
ValidationException asValidationException(ExecutableElement sourceMethod) {

core/src/main/java/net/jbock/coerce/hint/ArrayHint.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
import javax.lang.model.type.TypeKind;
44
import javax.lang.model.type.TypeMirror;
55

6-
public class ArrayHint extends Hint {
6+
class ArrayHint extends Hint {
77

88
@Override
9-
public String message(TypeMirror type, boolean repeatable) {
9+
String message(TypeMirror type, boolean repeatable) {
1010
if (type.getKind() == TypeKind.ARRAY) {
1111
if (!repeatable) {
1212
return "Declare this parameter repeatable.";

core/src/main/java/net/jbock/coerce/hint/CollectionHint.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
import javax.lang.model.type.TypeMirror;
66
import java.util.List;
77

8-
public class CollectionHint extends Hint {
8+
class CollectionHint extends Hint {
99

1010
@Override
11-
public String message(TypeMirror type, boolean repeatable) {
11+
String message(TypeMirror type, boolean repeatable) {
1212
if (TypeTool.get().isSameErasure(type, List.class)) {
1313
if (!repeatable) {
1414
return "Declare this parameter repeatable.";

core/src/main/java/net/jbock/coerce/hint/DateHint.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
import java.util.stream.Collectors;
99
import java.util.stream.Stream;
1010

11-
public class DateHint extends PreciseTypeHint {
11+
class DateHint extends PreciseTypeHint {
1212

1313
@Override
14-
public Set<TypeName> types() {
14+
Set<TypeName> types() {
1515
return Stream.of(
1616
java.util.Date.class,
1717
java.sql.Date.class,
@@ -20,7 +20,7 @@ public Set<TypeName> types() {
2020
}
2121

2222
@Override
23-
public String message(TypeName typeName) {
23+
String message(TypeName typeName) {
2424
return typeName + " is not supported. Use a type from java.time instead.";
2525
}
2626
}

core/src/main/java/net/jbock/coerce/hint/Hint.java

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

33
import javax.lang.model.type.TypeMirror;
44

5-
public abstract class Hint {
5+
abstract class Hint {
66

7-
public abstract String message(TypeMirror type, boolean repeatable);
7+
abstract String message(TypeMirror type, boolean repeatable);
88
}

0 commit comments

Comments
 (0)