Skip to content

Commit 94f156a

Browse files
committed
Use Valid HTTP status codes in error responses
VSS client-server protocol doesn't really need valid http status codes. Any non-200 status code is considered as error, but it is still good to response with valid status codes corresponding to error. Some http-clients might also have http-status code validation. No changes on client-side required, since any non-200 status code works.
1 parent 0a185d8 commit 94f156a

File tree

5 files changed

+27
-22
lines changed

5 files changed

+27
-22
lines changed

app/src/main/java/org/vss/api/AbstractVssApi.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,23 +28,28 @@ Response toResponse(GeneratedMessageV3 protoResponse) {
2828

2929
Response toErrorResponse(Exception e) {
3030
ErrorCode errorCode;
31+
int statusCode;
3132
if (e instanceof ConflictException) {
3233
errorCode = ErrorCode.CONFLICT_EXCEPTION;
34+
statusCode = 409;
3335
} else if (e instanceof IllegalArgumentException
3436
|| e instanceof InvalidProtocolBufferException) {
3537
errorCode = ErrorCode.INVALID_REQUEST_EXCEPTION;
38+
statusCode = 400;
3639
} else if (e instanceof NoSuchKeyException) {
3740
errorCode = ErrorCode.NO_SUCH_KEY_EXCEPTION;
41+
statusCode = 404;
3842
} else {
3943
errorCode = ErrorCode.INTERNAL_SERVER_EXCEPTION;
44+
statusCode = 500;
4045
}
4146

4247
ErrorResponse errorResponse = ErrorResponse.newBuilder()
4348
.setErrorCode(errorCode)
4449
.setMessage(e.getMessage())
4550
.build();
4651

47-
return Response.status(errorCode.getNumber())
52+
return Response.status(statusCode)
4853
.entity(errorResponse.toByteArray())
4954
.build();
5055
}

app/src/test/java/org/vss/api/DeleteObjectApiTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ void execute_ValidPayload_ReturnsResponse() {
5959
@ParameterizedTest
6060
@MethodSource("provideErrorTestCases")
6161
void execute_InvalidPayload_ReturnsErrorResponse(Exception exception,
62-
ErrorCode errorCode) {
62+
ErrorCode errorCode, int statusCode) {
6363
DeleteObjectRequest expectedRequest =
6464
DeleteObjectRequest.newBuilder().setStoreId(TEST_STORE_ID).setKeyValue(
6565
KeyValue.newBuilder().setKey(TEST_KEY).setVersion(0)
@@ -74,15 +74,15 @@ void execute_InvalidPayload_ReturnsErrorResponse(Exception exception,
7474
.setMessage("")
7575
.build();
7676
assertThat(response.getEntity(), is(expectedErrorResponse.toByteArray()));
77-
assertThat(response.getStatus(), is(expectedErrorResponse.getErrorCode().getNumber()));
77+
assertThat(response.getStatus(), is(statusCode));
7878
verify(mockKVStore).delete(expectedRequest);
7979
}
8080

8181
private static Stream<Arguments> provideErrorTestCases() {
8282
return Stream.of(
83-
Arguments.of(new ConflictException(""), ErrorCode.CONFLICT_EXCEPTION),
84-
Arguments.of(new IllegalArgumentException(""), ErrorCode.INVALID_REQUEST_EXCEPTION),
85-
Arguments.of(new RuntimeException(""), ErrorCode.INTERNAL_SERVER_EXCEPTION)
83+
Arguments.of(new ConflictException(""), ErrorCode.CONFLICT_EXCEPTION, 409),
84+
Arguments.of(new IllegalArgumentException(""), ErrorCode.INVALID_REQUEST_EXCEPTION, 400),
85+
Arguments.of(new RuntimeException(""), ErrorCode.INTERNAL_SERVER_EXCEPTION, 500)
8686
);
8787
}
8888
}

app/src/test/java/org/vss/api/GetObjectApiTest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ void execute_ValidPayload_ReturnsResponse() {
5858
@ParameterizedTest
5959
@MethodSource("provideErrorTestCases")
6060
void execute_InvalidPayload_ReturnsErrorResponse(Exception exception,
61-
ErrorCode errorCode) {
61+
ErrorCode errorCode, int statusCode) {
6262
GetObjectRequest expectedRequest = GetObjectRequest.newBuilder()
6363
.setStoreId(TEST_STORE_ID)
6464
.setKey(TEST_KEY)
@@ -73,16 +73,16 @@ void execute_InvalidPayload_ReturnsErrorResponse(Exception exception,
7373
.setMessage("")
7474
.build();
7575
assertThat(response.getEntity(), is(expectedErrorResponse.toByteArray()));
76-
assertThat(response.getStatus(), is(expectedErrorResponse.getErrorCode().getNumber()));
76+
assertThat(response.getStatus(), is(statusCode));
7777
verify(mockKVStore).get(expectedRequest);
7878
}
7979

8080
private static Stream<Arguments> provideErrorTestCases() {
8181
return Stream.of(
82-
Arguments.of(new ConflictException(""), ErrorCode.CONFLICT_EXCEPTION),
83-
Arguments.of(new IllegalArgumentException(""), ErrorCode.INVALID_REQUEST_EXCEPTION),
84-
Arguments.of(new NoSuchKeyException(""), ErrorCode.NO_SUCH_KEY_EXCEPTION),
85-
Arguments.of(new RuntimeException(""), ErrorCode.INTERNAL_SERVER_EXCEPTION)
82+
Arguments.of(new ConflictException(""), ErrorCode.CONFLICT_EXCEPTION, 409),
83+
Arguments.of(new IllegalArgumentException(""), ErrorCode.INVALID_REQUEST_EXCEPTION, 400),
84+
Arguments.of(new NoSuchKeyException(""), ErrorCode.NO_SUCH_KEY_EXCEPTION, 404),
85+
Arguments.of(new RuntimeException(""), ErrorCode.INTERNAL_SERVER_EXCEPTION, 500)
8686
);
8787
}
8888
}

app/src/test/java/org/vss/api/ListKeyVersionsApiTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ void execute_ValidPayload_ReturnsResponse() {
6262
@ParameterizedTest
6363
@MethodSource("provideErrorTestCases")
6464
void execute_InvalidPayload_ReturnsErrorResponse(Exception exception,
65-
ErrorCode errorCode) {
65+
ErrorCode errorCode, int statusCode) {
6666
ListKeyVersionsRequest expectedRequest =
6767
ListKeyVersionsRequest.newBuilder()
6868
.setStoreId(TEST_STORE_ID)
@@ -78,15 +78,15 @@ void execute_InvalidPayload_ReturnsErrorResponse(Exception exception,
7878
.setMessage("")
7979
.build();
8080
assertThat(response.getEntity(), is(expectedErrorResponse.toByteArray()));
81-
assertThat(response.getStatus(), is(expectedErrorResponse.getErrorCode().getNumber()));
81+
assertThat(response.getStatus(), is(statusCode));
8282
verify(mockKVStore).listKeyVersions(expectedRequest);
8383
}
8484

8585
private static Stream<Arguments> provideErrorTestCases() {
8686
return Stream.of(
87-
Arguments.of(new ConflictException(""), ErrorCode.CONFLICT_EXCEPTION),
88-
Arguments.of(new IllegalArgumentException(""), ErrorCode.INVALID_REQUEST_EXCEPTION),
89-
Arguments.of(new RuntimeException(""), ErrorCode.INTERNAL_SERVER_EXCEPTION)
87+
Arguments.of(new ConflictException(""), ErrorCode.CONFLICT_EXCEPTION, 409),
88+
Arguments.of(new IllegalArgumentException(""), ErrorCode.INVALID_REQUEST_EXCEPTION, 400),
89+
Arguments.of(new RuntimeException(""), ErrorCode.INTERNAL_SERVER_EXCEPTION, 500)
9090
);
9191
}
9292
}

app/src/test/java/org/vss/api/PutObjectsApiTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ void execute_ValidPayload_ReturnsResponse() {
6161
@ParameterizedTest
6262
@MethodSource("provideErrorTestCases")
6363
void execute_InvalidPayload_ReturnsErrorResponse(Exception exception,
64-
ErrorCode errorCode) {
64+
ErrorCode errorCode, int statusCode) {
6565
PutObjectRequest expectedRequest =
6666
PutObjectRequest.newBuilder()
6767
.setStoreId(TEST_STORE_ID)
@@ -77,15 +77,15 @@ void execute_InvalidPayload_ReturnsErrorResponse(Exception exception,
7777
.setMessage("")
7878
.build();
7979
assertThat(response.getEntity(), is(expectedErrorResponse.toByteArray()));
80-
assertThat(response.getStatus(), is(expectedErrorResponse.getErrorCode().getNumber()));
80+
assertThat(response.getStatus(), is(statusCode));
8181
verify(mockKVStore).put(expectedRequest);
8282
}
8383

8484
private static Stream<Arguments> provideErrorTestCases() {
8585
return Stream.of(
86-
Arguments.of(new ConflictException(""), ErrorCode.CONFLICT_EXCEPTION),
87-
Arguments.of(new IllegalArgumentException(""), ErrorCode.INVALID_REQUEST_EXCEPTION),
88-
Arguments.of(new RuntimeException(""), ErrorCode.INTERNAL_SERVER_EXCEPTION)
86+
Arguments.of(new ConflictException(""), ErrorCode.CONFLICT_EXCEPTION, 409),
87+
Arguments.of(new IllegalArgumentException(""), ErrorCode.INVALID_REQUEST_EXCEPTION, 400),
88+
Arguments.of(new RuntimeException(""), ErrorCode.INTERNAL_SERVER_EXCEPTION, 500)
8989
);
9090
}
9191
}

0 commit comments

Comments
 (0)