Skip to content

Commit 6be652e

Browse files
authored
ErrorType#name must be CamelCase (#13)
1 parent e4da5b8 commit 6be652e

File tree

3 files changed

+45
-22
lines changed

3 files changed

+45
-22
lines changed

errors/src/main/java/com/palantir/remoting/api/errors/ErrorType.java

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.palantir.remoting.api.errors;
1818

19+
import java.util.regex.Pattern;
1920
import org.immutables.value.Value;
2021

2122
/**
@@ -26,6 +27,8 @@
2627
@ImmutablesStyle
2728
public abstract class ErrorType {
2829

30+
private static final Pattern UPPER_CAMEL_CASE = Pattern.compile("([A-Z][a-z]+)+");
31+
2932
public enum Code {
3033
UNKNOWN(500),
3134
PERMISSION_DENIED(403),
@@ -41,11 +44,11 @@ public enum Code {
4144
}
4245
}
4346

44-
public static final ErrorType UNKNOWN = create(Code.UNKNOWN);
45-
public static final ErrorType PERMISSION_DENIED = create(Code.PERMISSION_DENIED);
46-
public static final ErrorType INVALID_ARGUMENT = create(Code.INVALID_ARGUMENT);
47-
public static final ErrorType FAILED_PRECONDITION = create(Code.FAILED_PRECONDITION);
48-
public static final ErrorType INTERNAL = create(Code.INTERNAL);
47+
public static final ErrorType UNKNOWN = createInternal(Code.UNKNOWN, "Unknown");
48+
public static final ErrorType PERMISSION_DENIED = createInternal(Code.PERMISSION_DENIED, "PermissionDenied");
49+
public static final ErrorType INVALID_ARGUMENT = createInternal(Code.INVALID_ARGUMENT, "InvalidArgument");
50+
public static final ErrorType FAILED_PRECONDITION = createInternal(Code.FAILED_PRECONDITION, "FailedPrecondition");
51+
public static final ErrorType INTERNAL = createInternal(Code.INTERNAL, "Internal");
4952

5053
/** The {@link Code} of this error. */
5154
public abstract Code code();
@@ -59,6 +62,13 @@ public enum Code {
5962
/** The HTTP error code used to convey this error to HTTP clients. */
6063
public abstract int httpErrorCode();
6164

65+
@Value.Check
66+
final void check() {
67+
if (!UPPER_CAMEL_CASE.matcher(name()).matches()) {
68+
throw new IllegalArgumentException("ErrorType names must be UpperCamelCase: " + name());
69+
}
70+
}
71+
6272
/**
6373
* Creates a new error type with the given name and HTTP error code, and error type{@link Code#CUSTOM}.
6474
* Allowed error codes are {@code 400 BAD REQUEST} and {@code 500 INTERNAL SERVER ERROR}.
@@ -82,17 +92,13 @@ public static ErrorType of(Code code, String name) {
8292
if (code == Code.CUSTOM) {
8393
throw new IllegalArgumentException("Use the custom() method to construct ErrorTypes with code CUSTOM");
8494
}
85-
return ImmutableErrorType.builder()
86-
.code(code)
87-
.name(name)
88-
.httpErrorCode(code.httpErrorCode)
89-
.build();
95+
return createInternal(code, name);
9096
}
9197

92-
private static ErrorType create(Code code) {
98+
private static ErrorType createInternal(Code code, String name) {
9399
return ImmutableErrorType.builder()
94100
.code(code)
95-
.name(code.name())
101+
.name(name)
96102
.httpErrorCode(code.httpErrorCode)
97103
.build();
98104
}

errors/src/test/java/com/palantir/remoting/api/errors/ErrorTypeTest.java

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,23 @@
2323

2424
public final class ErrorTypeTest {
2525

26+
@Test
27+
public void testNameMustBeCamelCase() throws Exception {
28+
assertThatThrownBy(() -> ErrorType.of(ErrorType.Code.FAILED_PRECONDITION, "foo"))
29+
.isInstanceOf(IllegalArgumentException.class)
30+
.hasMessageStartingWith("ErrorType names must be UpperCamelCase: foo");
31+
32+
assertThatThrownBy(() -> ErrorType.custom("foo", 400))
33+
.isInstanceOf(IllegalArgumentException.class)
34+
.hasMessageStartingWith("ErrorType names must be UpperCamelCase: foo");
35+
assertThatThrownBy(() -> ErrorType.custom("fooBar", 400))
36+
.isInstanceOf(IllegalArgumentException.class)
37+
.hasMessageStartingWith("ErrorType names must be UpperCamelCase: fooBar");
38+
assertThatThrownBy(() -> ErrorType.custom("", 400))
39+
.isInstanceOf(IllegalArgumentException.class)
40+
.hasMessageStartingWith("ErrorType names must be UpperCamelCase: ");
41+
}
42+
2643
@Test
2744
public void testDefaultErrorTypeHttpErrorCodes() throws Exception {
2845
assertThat(ErrorType.UNKNOWN.httpErrorCode()).isEqualTo(500);
@@ -34,29 +51,29 @@ public void testDefaultErrorTypeHttpErrorCodes() throws Exception {
3451

3552
@Test
3653
public void testCustomErrors() throws Exception {
37-
ErrorType custom400 = ErrorType.custom("myDesc", 400);
54+
ErrorType custom400 = ErrorType.custom("MyDesc", 400);
3855
assertThat(custom400.code()).isEqualTo(ErrorType.Code.CUSTOM);
3956
assertThat(custom400.httpErrorCode()).isEqualTo(400);
40-
assertThat(custom400.name()).isEqualTo("myDesc");
57+
assertThat(custom400.name()).isEqualTo("MyDesc");
4158

42-
ErrorType custom500 = ErrorType.custom("myDesc", 500);
59+
ErrorType custom500 = ErrorType.custom("MyDesc", 500);
4360
assertThat(custom500.code()).isEqualTo(ErrorType.Code.CUSTOM);
4461
assertThat(custom500.httpErrorCode()).isEqualTo(500);
45-
assertThat(custom500.name()).isEqualTo("myDesc");
62+
assertThat(custom500.name()).isEqualTo("MyDesc");
4663

47-
assertThatThrownBy(() -> ErrorType.custom("myDesc", 403))
64+
assertThatThrownBy(() -> ErrorType.custom("MyDesc", 403))
4865
.isInstanceOf(IllegalArgumentException.class)
4966
.hasMessage("CUSTOM ErrorTypes must have HTTP error code 400 or 500");
5067
}
5168

5269
@Test
5370
public void testCanCreateNewErrorTypes() throws Exception {
54-
ErrorType error = ErrorType.of(ErrorType.Code.FAILED_PRECONDITION, "myDesc");
71+
ErrorType error = ErrorType.of(ErrorType.Code.FAILED_PRECONDITION, "MyDesc");
5572
assertThat(error.code()).isEqualTo(ErrorType.Code.FAILED_PRECONDITION);
5673
assertThat(error.httpErrorCode()).isEqualTo(400);
57-
assertThat(error.name()).isEqualTo("myDesc");
74+
assertThat(error.name()).isEqualTo("MyDesc");
5875

59-
assertThatThrownBy(() -> ErrorType.of(ErrorType.Code.CUSTOM, "myDesc"))
76+
assertThatThrownBy(() -> ErrorType.of(ErrorType.Code.CUSTOM, "MyDesc"))
6077
.isInstanceOf(IllegalArgumentException.class)
6178
.hasMessage("Use the custom() method to construct ErrorTypes with code CUSTOM");
6279
}

errors/src/test/java/com/palantir/remoting/api/errors/ServiceExceptionTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626

2727
public final class ServiceExceptionTest {
2828

29-
private static final ErrorType ERROR = ErrorType.custom("myDesc", 400);
30-
private static final String EXPECTED_ERROR_MSG = "ServiceException: CUSTOM (myDesc)";
29+
private static final ErrorType ERROR = ErrorType.custom("MyDesc", 400);
30+
private static final String EXPECTED_ERROR_MSG = "ServiceException: CUSTOM (MyDesc)";
3131

3232
@Test
3333
public void testExceptionMessagesContainsSafeArgsOnly() {

0 commit comments

Comments
 (0)