Skip to content

Commit 69f0979

Browse files
committed
catch enum illegal state exception
1 parent 3d32302 commit 69f0979

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

http-api/src/main/java/io/avaje/http/api/PathTypeConversion.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,17 @@ public static int asInt(String value) {
6464
checkNull(value);
6565
try {
6666
return Integer.parseInt(value);
67-
} catch (NumberFormatException e) {
67+
} catch (final NumberFormatException e) {
68+
throw new InvalidPathArgumentException(e);
69+
}
70+
}
71+
72+
/** Convert to enum. */
73+
public static <T> Enum asEnum(Class<T> clazz, String value) {
74+
checkNull(value);
75+
try {
76+
return Enum.valueOf((Class<Enum>) clazz, value.toUpperCase());
77+
} catch (final IllegalArgumentException e) {
6878
throw new InvalidPathArgumentException(e);
6979
}
7080
}

http-generator-core/src/main/java/io/avaje/http/generator/core/TypeMap.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ static class EnumHandler extends ObjectHandler {
310310

311311
@Override
312312
public String toMethod() {
313-
return type.shortType() + ".valueOf(";
313+
return "(" + type.shortType() + ") asEnum(" + type.shortType() + ".class,";
314314
}
315315

316316
@Override
@@ -334,7 +334,7 @@ static class CollectionHandler implements TypeHandler {
334334
(set ? "set" : "list")
335335
+ "("
336336
+ (isEnum
337-
? handler.toMethod().replace(".", "::").replace("(", "")
337+
? "qp -> " + handler.toMethod() + " qp)"
338338
: "PathTypeConversion::" + shortName)
339339
+ ", ";
340340

0 commit comments

Comments
 (0)