Skip to content

Commit dfafd57

Browse files
authored
Trande api review0225 (Azure#44092)
* API Review Updates. * Fixing linting report. * More API review changes. * Fixing bug. * Adding javadoc. * Fixing last error.
1 parent 1ecd729 commit dfafd57

File tree

5 files changed

+139
-187
lines changed

5 files changed

+139
-187
lines changed

sdk/spring/azure-spring-data-cosmos/src/main/java/com/azure/spring/data/cosmos/Constants.java

Lines changed: 0 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -91,107 +91,6 @@ public final class Constants {
9191
*/
9292
public static final String ISO_8601_COMPATIBLE_DATE_PATTERN = "yyyy-MM-dd'T'HH:mm:ss:SSSXXX";
9393

94-
/**
95-
* Cosmos Exception Status Codes.
96-
*/
97-
public static final class CosmosExceptionStatusCodes {
98-
/**
99-
* Default constructor.
100-
*/
101-
private CosmosExceptionStatusCodes() {
102-
}
103-
/**
104-
* Bad Request Status Code.
105-
*/
106-
public static final int BADREQUEST = 400;
107-
/**
108-
* Conflict Status Code.
109-
*/
110-
public static final int CONFLICT = 409;
111-
/**
112-
* Forbidden Status Code.
113-
*/
114-
public static final int FORBIDDEN = 403;
115-
/**
116-
* Gone Status Code.
117-
*/
118-
public static final int GONE = 410;
119-
/**
120-
* Internal Server Error Status Code.
121-
*/
122-
public static final int INTERNAL_SERVER_ERROR = 500;
123-
/**
124-
* Method Not Allowed Status Code.
125-
*/
126-
public static final int METHOD_NOT_ALLOWED = 405;
127-
/**
128-
* Not Found Status Code.
129-
*/
130-
public static final int NOTFOUND = 404;
131-
/**
132-
* Request Timeout Status Code.
133-
*/
134-
public static final int REQUEST_TIMEOUT = 408;
135-
/**
136-
* Precondition Failed Status Code.
137-
*/
138-
public static final int PRECONDITION_FAILED = 412;
139-
/**
140-
* Request Entity Too Large Status Code.
141-
*/
142-
public static final int REQUEST_ENTITY_TOO_LARGE = 413;
143-
/**
144-
* Too Many Requests Status Code.
145-
*/
146-
public static final int TOO_MANY_REQUESTS = 429;
147-
/**
148-
* Retry With Status Code.
149-
*/
150-
public static final int RETRY_WITH = 449;
151-
/**
152-
* Service Unavailable Status Code.
153-
*/
154-
public static final int SERVICE_UNAVAILABLE = 503;
155-
/**
156-
* Unauthorized Status Code.
157-
*/
158-
public static final int UNAUTHORIZED = 401;
159-
}
160-
161-
/**
162-
* Cosmos Exception Sub Status Codes.
163-
*/
164-
public static final class CosmosExceptionSubStatusCodes {
165-
/**
166-
* Default constructor.
167-
*/
168-
private CosmosExceptionSubStatusCodes() {
169-
}
170-
// For 410 GONE
171-
/**
172-
* Name Cache Is Stale Sub Status Code.
173-
*/
174-
public static final int NAME_CACHE_IS_STALE = 1000;
175-
/**
176-
* Partition Key Range Gone Sub Status Code.
177-
*/
178-
public static final int PARTITION_KEY_RANGE_GONE = 1002;
179-
/**
180-
* Completing Split or Merge Sub Status Code.
181-
*/
182-
public static final int COMPLETING_SPLIT_OR_MERGE = 1007;
183-
/**
184-
* Completing Partition Migration Sub Status Code.
185-
*/
186-
public static final int COMPLETING_PARTITION_MIGRATION = 1008;
187-
188-
// For 408 REQUEST_TIMEOUT
189-
/**
190-
* Client Operation Timeout Sub Status Code.
191-
*/
192-
public static final int CLIENT_OPERATION_TIMEOUT = 20008;
193-
}
194-
19594
private Constants() {
19695
}
19796
}

sdk/spring/azure-spring-data-cosmos/src/main/java/com/azure/spring/data/cosmos/exception/CosmosExceptionUtils.java

Lines changed: 47 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
package com.azure.spring.data.cosmos.exception;
44

55
import com.azure.cosmos.CosmosException;
6-
import com.azure.spring.data.cosmos.Constants;
76
import com.azure.spring.data.cosmos.common.CosmosUtils;
87
import com.azure.spring.data.cosmos.core.ResponseDiagnosticsProcessor;
8+
import org.springframework.http.HttpStatus;
99
import org.springframework.util.ObjectUtils;
1010
import reactor.core.Exceptions;
1111
import reactor.core.publisher.Mono;
@@ -43,66 +43,52 @@ public static <T> Mono<T> exceptionHandler(String message, Throwable throwable,
4343
CosmosException cosmosException = (CosmosException) unwrappedThrowable;
4444
CosmosUtils.fillAndProcessCosmosExceptionDiagnostics(responseDiagnosticsProcessor, cosmosException);
4545

46-
switch (cosmosException.getStatusCode()) {
47-
case Constants.CosmosExceptionStatusCodes.BADREQUEST:
48-
cosmosAccessException = new CosmosBadRequestException(message, cosmosException);
49-
break;
50-
case Constants.CosmosExceptionStatusCodes.CONFLICT:
51-
cosmosAccessException = new CosmosConflictException(message, cosmosException);
52-
break;
53-
case Constants.CosmosExceptionStatusCodes.FORBIDDEN:
54-
cosmosAccessException = new CosmosForbiddenException(message, cosmosException);
55-
break;
56-
case Constants.CosmosExceptionStatusCodes.GONE:
57-
if (cosmosException.getSubStatusCode() == Constants.CosmosExceptionSubStatusCodes.NAME_CACHE_IS_STALE) {
58-
cosmosAccessException = new CosmosInvalidPartitionException(message, cosmosException);
59-
} else if (cosmosException.getSubStatusCode() == Constants.CosmosExceptionSubStatusCodes.COMPLETING_PARTITION_MIGRATION) {
60-
cosmosAccessException = new CosmosPartitionIsMigratingException(message, cosmosException);
61-
} else if (cosmosException.getSubStatusCode() == Constants.CosmosExceptionSubStatusCodes.PARTITION_KEY_RANGE_GONE) {
62-
cosmosAccessException = new CosmosPartitionKeyRangeGoneException(message, cosmosException);
63-
} else if (cosmosException.getSubStatusCode() == Constants.CosmosExceptionSubStatusCodes.COMPLETING_SPLIT_OR_MERGE) {
64-
cosmosAccessException = new CosmosPartitionKeyRangeIsSplittingException(message, cosmosException);
65-
} else {
66-
cosmosAccessException = new CosmosGoneException(message, cosmosException);
67-
}
68-
break;
69-
case Constants.CosmosExceptionStatusCodes.INTERNAL_SERVER_ERROR:
70-
cosmosAccessException = new CosmosInternalServerErrorException(message, cosmosException);
71-
break;
72-
case Constants.CosmosExceptionStatusCodes.METHOD_NOT_ALLOWED:
73-
cosmosAccessException = new CosmosMethodNotAllowedException(message, cosmosException);
74-
break;
75-
case Constants.CosmosExceptionStatusCodes.NOTFOUND:
76-
cosmosAccessException = new CosmosNotFoundException(message, cosmosException);
77-
break;
78-
case Constants.CosmosExceptionStatusCodes.REQUEST_TIMEOUT:
79-
if (((CosmosException) unwrappedThrowable).getSubStatusCode() == Constants.CosmosExceptionSubStatusCodes.CLIENT_OPERATION_TIMEOUT) {
80-
cosmosAccessException = new CosmosOperationCancelledException(message, cosmosException);
81-
} else {
82-
cosmosAccessException = new CosmosRequestTimeoutException(message, cosmosException);
83-
}
84-
break;
85-
case Constants.CosmosExceptionStatusCodes.PRECONDITION_FAILED:
86-
cosmosAccessException = new CosmosPreconditionFailedException(message, cosmosException);
87-
break;
88-
case Constants.CosmosExceptionStatusCodes.REQUEST_ENTITY_TOO_LARGE:
89-
cosmosAccessException = new CosmosRequestEntityTooLargeException(message, cosmosException);
90-
break;
91-
case Constants.CosmosExceptionStatusCodes.TOO_MANY_REQUESTS:
92-
cosmosAccessException = new CosmosRequestRateTooLargeException(message, cosmosException);
93-
break;
94-
case Constants.CosmosExceptionStatusCodes.RETRY_WITH:
95-
cosmosAccessException = new CosmosRetryWithException(message, cosmosException);
96-
break;
97-
case Constants.CosmosExceptionStatusCodes.SERVICE_UNAVAILABLE:
98-
cosmosAccessException = new CosmosServiceUnavailableException(message, cosmosException);
99-
break;
100-
case Constants.CosmosExceptionStatusCodes.UNAUTHORIZED:
101-
cosmosAccessException = new CosmosUnauthorizedException(message, cosmosException);
102-
break;
103-
default:
104-
cosmosAccessException = new CosmosAccessException(message, cosmosException);
105-
break;
46+
int statusCode = cosmosException.getStatusCode();
47+
int subStatusCode = ((CosmosException) unwrappedThrowable).getSubStatusCode();
48+
if (statusCode == HttpStatus.BAD_REQUEST.value()) {
49+
cosmosAccessException = new CosmosBadRequestException(message, cosmosException);
50+
} else if (statusCode == HttpStatus.CONFLICT.value()) {
51+
cosmosAccessException = new CosmosConflictException(message, cosmosException);
52+
} else if (statusCode == HttpStatus.FORBIDDEN.value()) {
53+
cosmosAccessException = new CosmosForbiddenException(message, cosmosException);
54+
} else if (statusCode == HttpStatus.GONE.value()) {
55+
if (subStatusCode == HttpConstants.CosmosExceptionSubStatusCodes.NAME_CACHE_IS_STALE) {
56+
cosmosAccessException = new CosmosInvalidPartitionException(message, cosmosException);
57+
} else if (subStatusCode == HttpConstants.CosmosExceptionSubStatusCodes.COMPLETING_PARTITION_MIGRATION) {
58+
cosmosAccessException = new CosmosPartitionIsMigratingException(message, cosmosException);
59+
} else if (subStatusCode == HttpConstants.CosmosExceptionSubStatusCodes.PARTITION_KEY_RANGE_GONE) {
60+
cosmosAccessException = new CosmosPartitionKeyRangeGoneException(message, cosmosException);
61+
} else if (subStatusCode == HttpConstants.CosmosExceptionSubStatusCodes.COMPLETING_SPLIT_OR_MERGE) {
62+
cosmosAccessException = new CosmosPartitionKeyRangeIsSplittingException(message, cosmosException);
63+
} else {
64+
cosmosAccessException = new CosmosGoneException(message, cosmosException);
65+
}
66+
} else if (statusCode == HttpStatus.INTERNAL_SERVER_ERROR.value()) {
67+
cosmosAccessException = new CosmosInternalServerErrorException(message, cosmosException);
68+
} else if (statusCode == HttpStatus.METHOD_NOT_ALLOWED.value()) {
69+
cosmosAccessException = new CosmosMethodNotAllowedException(message, cosmosException);
70+
} else if (statusCode == HttpStatus.NOT_FOUND.value()) {
71+
cosmosAccessException = new CosmosNotFoundException(message, cosmosException);
72+
} else if (statusCode == HttpStatus.REQUEST_TIMEOUT.value()) {
73+
if (subStatusCode == HttpConstants.CosmosExceptionSubStatusCodes.CLIENT_OPERATION_TIMEOUT) {
74+
cosmosAccessException = new CosmosOperationCancelledException(message, cosmosException);
75+
} else {
76+
cosmosAccessException = new CosmosRequestTimeoutException(message, cosmosException);
77+
}
78+
} else if (statusCode == HttpStatus.PRECONDITION_FAILED.value()) {
79+
cosmosAccessException = new CosmosPreconditionFailedException(message, cosmosException);
80+
} else if (statusCode == HttpStatus.PAYLOAD_TOO_LARGE.value()) {
81+
cosmosAccessException = new CosmosRequestEntityTooLargeException(message, cosmosException);
82+
} else if (statusCode == HttpStatus.TOO_MANY_REQUESTS.value()) {
83+
cosmosAccessException = new CosmosRequestRateTooLargeException(message, cosmosException);
84+
} else if (statusCode == HttpConstants.CosmosExceptionStatusCodes.RETRY_WITH) {
85+
cosmosAccessException = new CosmosRetryWithException(message, cosmosException);
86+
} else if (statusCode == HttpStatus.SERVICE_UNAVAILABLE.value()) {
87+
cosmosAccessException = new CosmosServiceUnavailableException(message, cosmosException);
88+
} else if (statusCode == HttpStatus.UNAUTHORIZED.value()) {
89+
cosmosAccessException = new CosmosUnauthorizedException(message, cosmosException);
90+
} else {
91+
cosmosAccessException = new CosmosAccessException(message, cosmosException);
10692
}
10793
} else {
10894
cosmosAccessException = new CosmosAccessException(message, unwrappedThrowable);
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
package com.azure.spring.data.cosmos.exception;
4+
5+
6+
/**
7+
* Http Constants.
8+
*/
9+
final class HttpConstants {
10+
11+
/**
12+
* Cosmos Exception Status Codes.
13+
*/
14+
static final class CosmosExceptionStatusCodes {
15+
/**
16+
* Default constructor.
17+
*/
18+
private CosmosExceptionStatusCodes() {
19+
}
20+
/**
21+
* Retry With Status Code.
22+
*/
23+
static final int RETRY_WITH = 449;
24+
}
25+
26+
/**
27+
* Cosmos Exception Sub Status Codes.
28+
*/
29+
static final class CosmosExceptionSubStatusCodes {
30+
/**
31+
* Default constructor.
32+
*/
33+
private CosmosExceptionSubStatusCodes() {
34+
}
35+
// For 410 GONE
36+
/**
37+
* Name Cache Is Stale Sub Status Code.
38+
*/
39+
static final int NAME_CACHE_IS_STALE = 1000;
40+
/**
41+
* Partition Key Range Gone Sub Status Code.
42+
*/
43+
static final int PARTITION_KEY_RANGE_GONE = 1002;
44+
/**
45+
* Completing Split or Merge Sub Status Code.
46+
*/
47+
static final int COMPLETING_SPLIT_OR_MERGE = 1007;
48+
/**
49+
* Completing Partition Migration Sub Status Code.
50+
*/
51+
static final int COMPLETING_PARTITION_MIGRATION = 1008;
52+
53+
// For 408 REQUEST_TIMEOUT
54+
/**
55+
* Client Operation Timeout Sub Status Code.
56+
*/
57+
static final int CLIENT_OPERATION_TIMEOUT = 20008;
58+
}
59+
60+
/**
61+
* Default constructor.
62+
*/
63+
private HttpConstants() {
64+
}
65+
66+
}

sdk/spring/azure-spring-data-cosmos/src/main/java/module-info.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
requires spring.data.commons;
1616
requires spring.expression;
1717
requires spring.tx;
18+
requires spring.web;
1819

1920
exports com.azure.spring.data.cosmos;
2021
exports com.azure.spring.data.cosmos.common;

0 commit comments

Comments
 (0)