Skip to content

Commit e1b5d39

Browse files
authored
Use HttpResponseException in annotation-processor (#45053)
1 parent bb338da commit e1b5d39

25 files changed

+1313
-309
lines changed

sdk/clientcore/annotation-processor-test/src/main/java/io/clientcore/annotation/processor/test/HostEdgeCase1ServiceImpl.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import io.clientcore.core.instrumentation.logging.ClientLogger;
1414
import io.clientcore.core.serialization.json.JsonSerializer;
1515
import io.clientcore.core.serialization.xml.XmlSerializer;
16-
import io.clientcore.core.http.models.HttpResponseException;
16+
import io.clientcore.core.utils.GeneratedCodeUtils;
1717

1818
/**
1919
* Initializes a new instance of the HostEdgeCase1ServiceImpl type.
@@ -53,8 +53,8 @@ public byte[] getByteArray(String url, int numberOfBytes) {
5353
int responseCode = networkResponse.getStatusCode();
5454
boolean expectedResponse = responseCode == 200;
5555
if (!expectedResponse) {
56-
String errorMessage = networkResponse.getValue().toString();
57-
throw new HttpResponseException(errorMessage, networkResponse, null);
56+
// Handle unexpected response
57+
GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, jsonSerializer, xmlSerializer, null, null);
5858
}
5959
BinaryData responseBody = networkResponse.getValue();
6060
return responseBody != null ? responseBody.toBytes() : null;

sdk/clientcore/annotation-processor-test/src/main/java/io/clientcore/annotation/processor/test/HostEdgeCase2ServiceImpl.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import io.clientcore.core.instrumentation.logging.ClientLogger;
1414
import io.clientcore.core.serialization.json.JsonSerializer;
1515
import io.clientcore.core.serialization.xml.XmlSerializer;
16-
import io.clientcore.core.http.models.HttpResponseException;
16+
import io.clientcore.core.utils.GeneratedCodeUtils;
1717

1818
/**
1919
* Initializes a new instance of the HostEdgeCase2ServiceImpl type.
@@ -53,8 +53,8 @@ public byte[] getByteArray(String uri, int numberOfBytes) {
5353
int responseCode = networkResponse.getStatusCode();
5454
boolean expectedResponse = responseCode == 200;
5555
if (!expectedResponse) {
56-
String errorMessage = networkResponse.getValue().toString();
57-
throw new HttpResponseException(errorMessage, networkResponse, null);
56+
// Handle unexpected response
57+
GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, jsonSerializer, xmlSerializer, null, null);
5858
}
5959
BinaryData responseBody = networkResponse.getValue();
6060
return responseBody != null ? responseBody.toBytes() : null;

sdk/clientcore/annotation-processor-test/src/main/java/io/clientcore/annotation/processor/test/HostPathEdgeCase3ServiceImpl.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import io.clientcore.core.instrumentation.logging.ClientLogger;
1515
import io.clientcore.core.serialization.json.JsonSerializer;
1616
import io.clientcore.core.serialization.xml.XmlSerializer;
17-
import io.clientcore.core.http.models.HttpResponseException;
17+
import io.clientcore.core.utils.GeneratedCodeUtils;
1818

1919
/**
2020
* Initializes a new instance of the HostPathEdgeCase3ServiceImpl type.
@@ -56,8 +56,8 @@ public Response<Void> noOperationParams(String endpoint, String apiVersion, Requ
5656
int responseCode = networkResponse.getStatusCode();
5757
boolean expectedResponse = responseCode == 204;
5858
if (!expectedResponse) {
59-
String errorMessage = networkResponse.getValue().toString();
60-
throw new HttpResponseException(errorMessage, networkResponse, null);
59+
// Handle unexpected response
60+
GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, jsonSerializer, xmlSerializer, null, null);
6161
}
6262
return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), null);
6363
}

sdk/clientcore/annotation-processor-test/src/main/java/io/clientcore/annotation/processor/test/ParameterizedHostServiceImpl.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import io.clientcore.core.instrumentation.logging.ClientLogger;
1414
import io.clientcore.core.serialization.json.JsonSerializer;
1515
import io.clientcore.core.serialization.xml.XmlSerializer;
16-
import io.clientcore.core.http.models.HttpResponseException;
16+
import io.clientcore.core.utils.GeneratedCodeUtils;
1717

1818
/**
1919
* Initializes a new instance of the ParameterizedHostServiceImpl type.
@@ -53,8 +53,8 @@ public byte[] getByteArray(String scheme, String host, int numberOfBytes) {
5353
int responseCode = networkResponse.getStatusCode();
5454
boolean expectedResponse = responseCode == 200;
5555
if (!expectedResponse) {
56-
String errorMessage = networkResponse.getValue().toString();
57-
throw new HttpResponseException(errorMessage, networkResponse, null);
56+
// Handle unexpected response
57+
GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, jsonSerializer, xmlSerializer, null, null);
5858
}
5959
BinaryData responseBody = networkResponse.getValue();
6060
return responseBody != null ? responseBody.toBytes() : null;

sdk/clientcore/annotation-processor-test/src/main/java/io/clientcore/annotation/processor/test/ParameterizedMultipleHostServiceImpl.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import io.clientcore.core.instrumentation.logging.ClientLogger;
1515
import io.clientcore.core.serialization.json.JsonSerializer;
1616
import io.clientcore.core.serialization.xml.XmlSerializer;
17-
import io.clientcore.core.http.models.HttpResponseException;
17+
import io.clientcore.core.utils.GeneratedCodeUtils;
1818
import io.clientcore.core.utils.CoreUtils;
1919
import java.lang.reflect.ParameterizedType;
2020
import io.clientcore.core.serialization.SerializationFormat;
@@ -57,9 +57,9 @@ public HttpBinJSON get(String scheme, String hostPart1, String hostPart2) {
5757
int responseCode = networkResponse.getStatusCode();
5858
boolean expectedResponse = responseCode == 200;
5959
if (!expectedResponse) {
60-
String errorMessage = networkResponse.getValue().toString();
60+
// Handle unexpected response
61+
GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, jsonSerializer, xmlSerializer, null, null);
6162
networkResponse.close();
62-
throw new HttpResponseException(errorMessage, networkResponse, null);
6363
}
6464
HttpBinJSON deserializedResult;
6565
ParameterizedType returnType = CoreUtils.createParameterizedType(HttpBinJSON.class);

sdk/clientcore/annotation-processor-test/src/main/java/io/clientcore/annotation/processor/test/SimpleXmlSerializableServiceImpl.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
import io.clientcore.core.serialization.xml.XmlSerializer;
1717
import io.clientcore.core.serialization.SerializationFormat;
1818
import io.clientcore.core.utils.CoreUtils;
19-
import io.clientcore.core.http.models.HttpResponseException;
19+
import io.clientcore.core.utils.GeneratedCodeUtils;
2020
import io.clientcore.core.http.models.HttpHeader;
2121
import java.lang.reflect.ParameterizedType;
2222

@@ -67,9 +67,9 @@ public void sendApplicationXml(SimpleXmlSerializable simpleXmlSerializable) {
6767
int responseCode = networkResponse.getStatusCode();
6868
boolean expectedResponse = responseCode == 200;
6969
if (!expectedResponse) {
70-
String errorMessage = networkResponse.getValue().toString();
70+
// Handle unexpected response
71+
GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, jsonSerializer, xmlSerializer, null, null);
7172
networkResponse.close();
72-
throw new HttpResponseException(errorMessage, networkResponse, null);
7373
}
7474
networkResponse.close();
7575
}
@@ -93,9 +93,9 @@ public void sendTextXml(SimpleXmlSerializable simpleXmlSerializable) {
9393
int responseCode = networkResponse.getStatusCode();
9494
boolean expectedResponse = responseCode == 200;
9595
if (!expectedResponse) {
96-
String errorMessage = networkResponse.getValue().toString();
96+
// Handle unexpected response
97+
GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, jsonSerializer, xmlSerializer, null, null);
9798
networkResponse.close();
98-
throw new HttpResponseException(errorMessage, networkResponse, null);
9999
}
100100
networkResponse.close();
101101
}
@@ -113,9 +113,9 @@ public SimpleXmlSerializable getXml(String contentType) {
113113
int responseCode = networkResponse.getStatusCode();
114114
boolean expectedResponse = responseCode == 200;
115115
if (!expectedResponse) {
116-
String errorMessage = networkResponse.getValue().toString();
116+
// Handle unexpected response
117+
GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, jsonSerializer, xmlSerializer, null, null);
117118
networkResponse.close();
118-
throw new HttpResponseException(errorMessage, networkResponse, null);
119119
}
120120
SimpleXmlSerializable deserializedResult;
121121
ParameterizedType returnType = CoreUtils.createParameterizedType(SimpleXmlSerializable.class);
@@ -143,9 +143,9 @@ public SimpleXmlSerializable getInvalidXml(String contentType) {
143143
int responseCode = networkResponse.getStatusCode();
144144
boolean expectedResponse = responseCode == 200;
145145
if (!expectedResponse) {
146-
String errorMessage = networkResponse.getValue().toString();
146+
// Handle unexpected response
147+
GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, jsonSerializer, xmlSerializer, null, null);
147148
networkResponse.close();
148-
throw new HttpResponseException(errorMessage, networkResponse, null);
149149
}
150150
SimpleXmlSerializable deserializedResult;
151151
ParameterizedType returnType = CoreUtils.createParameterizedType(SimpleXmlSerializable.class);

sdk/clientcore/annotation-processor-test/src/main/java/io/clientcore/annotation/processor/test/SpecialReturnBodiesServiceImpl.java

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import io.clientcore.core.instrumentation.logging.ClientLogger;
1616
import io.clientcore.core.serialization.json.JsonSerializer;
1717
import io.clientcore.core.serialization.xml.XmlSerializer;
18-
import io.clientcore.core.http.models.HttpResponseException;
18+
import io.clientcore.core.utils.GeneratedCodeUtils;
1919
import io.clientcore.core.utils.CoreUtils;
2020
import java.lang.reflect.ParameterizedType;
2121
import io.clientcore.core.serialization.SerializationFormat;
@@ -58,9 +58,9 @@ public BinaryData getBinaryData(String url) {
5858
int responseCode = networkResponse.getStatusCode();
5959
boolean expectedResponse = responseCode == 200;
6060
if (!expectedResponse) {
61-
String errorMessage = networkResponse.getValue().toString();
61+
// Handle unexpected response
62+
GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, jsonSerializer, xmlSerializer, null, null);
6263
networkResponse.close();
63-
throw new HttpResponseException(errorMessage, networkResponse, null);
6464
}
6565
return networkResponse.getValue();
6666
}
@@ -75,9 +75,9 @@ public Response<BinaryData> getBinaryDataWithResponse(String url) {
7575
int responseCode = networkResponse.getStatusCode();
7676
boolean expectedResponse = responseCode == 200;
7777
if (!expectedResponse) {
78-
String errorMessage = networkResponse.getValue().toString();
78+
// Handle unexpected response
79+
GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, jsonSerializer, xmlSerializer, null, null);
7980
networkResponse.close();
80-
throw new HttpResponseException(errorMessage, networkResponse, null);
8181
}
8282
return networkResponse;
8383
}
@@ -92,8 +92,8 @@ public byte[] getByteArray(String url) {
9292
int responseCode = networkResponse.getStatusCode();
9393
boolean expectedResponse = responseCode == 200;
9494
if (!expectedResponse) {
95-
String errorMessage = networkResponse.getValue().toString();
96-
throw new HttpResponseException(errorMessage, networkResponse, null);
95+
// Handle unexpected response
96+
GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, jsonSerializer, xmlSerializer, null, null);
9797
}
9898
BinaryData responseBody = networkResponse.getValue();
9999
return responseBody != null ? responseBody.toBytes() : null;
@@ -110,8 +110,8 @@ public Response<byte[]> getByteArrayWithResponse(String url) {
110110
int responseCode = networkResponse.getStatusCode();
111111
boolean expectedResponse = responseCode == 200;
112112
if (!expectedResponse) {
113-
String errorMessage = networkResponse.getValue().toString();
114-
throw new HttpResponseException(errorMessage, networkResponse, null);
113+
// Handle unexpected response
114+
GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, jsonSerializer, xmlSerializer, null, null);
115115
}
116116
BinaryData responseBody = networkResponse.getValue();
117117
return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), responseBody != null ? responseBody.toBytes() : null);
@@ -128,9 +128,9 @@ public InputStream getInputStream(String url) {
128128
int responseCode = networkResponse.getStatusCode();
129129
boolean expectedResponse = responseCode == 200;
130130
if (!expectedResponse) {
131-
String errorMessage = networkResponse.getValue().toString();
131+
// Handle unexpected response
132+
GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, jsonSerializer, xmlSerializer, null, null);
132133
networkResponse.close();
133-
throw new HttpResponseException(errorMessage, networkResponse, null);
134134
}
135135
return networkResponse.getValue().toStream();
136136
}
@@ -145,9 +145,9 @@ public Response<InputStream> getInputStreamWithResponse(String url) {
145145
int responseCode = networkResponse.getStatusCode();
146146
boolean expectedResponse = responseCode == 200;
147147
if (!expectedResponse) {
148-
String errorMessage = networkResponse.getValue().toString();
148+
// Handle unexpected response
149+
GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, jsonSerializer, xmlSerializer, null, null);
149150
networkResponse.close();
150-
throw new HttpResponseException(errorMessage, networkResponse, null);
151151
}
152152
return new Response<>(networkResponse.getRequest(), responseCode, networkResponse.getHeaders(), networkResponse.getValue().toStream());
153153
}
@@ -162,9 +162,9 @@ public Response<List<BinaryData>> getListOfBinaryData(String endpoint) {
162162
int responseCode = networkResponse.getStatusCode();
163163
boolean expectedResponse = responseCode == 200;
164164
if (!expectedResponse) {
165-
String errorMessage = networkResponse.getValue().toString();
165+
// Handle unexpected response
166+
GeneratedCodeUtils.handleUnexpectedResponse(responseCode, networkResponse, jsonSerializer, xmlSerializer, null, null);
166167
networkResponse.close();
167-
throw new HttpResponseException(errorMessage, networkResponse, null);
168168
}
169169
List<BinaryData> deserializedResult;
170170
ParameterizedType returnType = CoreUtils.createParameterizedType(List.class, BinaryData.class);

sdk/clientcore/annotation-processor-test/src/main/java/io/clientcore/annotation/processor/test/implementation/TestInterfaceClientImpl.java

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import io.clientcore.annotation.processor.test.implementation.models.Foo;
77
import io.clientcore.annotation.processor.test.implementation.models.FooListResult;
88
import io.clientcore.annotation.processor.test.implementation.models.HttpBinJSON;
9+
import io.clientcore.annotation.processor.test.implementation.models.ServiceError;
10+
import io.clientcore.annotation.processor.test.implementation.models.OperationError;
911
import io.clientcore.core.annotations.ServiceInterface;
1012
import io.clientcore.core.http.annotations.BodyParam;
1113
import io.clientcore.core.http.annotations.HeaderParam;
@@ -14,6 +16,7 @@
1416
import io.clientcore.core.http.annotations.PathParam;
1517
import io.clientcore.core.http.annotations.QueryParam;
1618
import io.clientcore.core.http.annotations.UnexpectedResponseExceptionDetail;
19+
import io.clientcore.core.http.annotations.UnexpectedResponseExceptionDetails;
1720
import io.clientcore.core.http.models.HttpMethod;
1821
import io.clientcore.core.http.models.RequestContext;
1922
import io.clientcore.core.http.models.Response;
@@ -65,7 +68,11 @@ Response<Void> testMethod(@HostParam("uri") String uri, @BodyParam("application/
6568
Void testMethodReturnsVoid(@HostParam("uri") String uri);
6669

6770
@HttpRequestInformation(method = HttpMethod.GET, path = "kv/{key}", expectedStatusCodes = { 200 })
68-
@UnexpectedResponseExceptionDetail(exceptionBodyClass = Error.class)
71+
@UnexpectedResponseExceptionDetails({
72+
@UnexpectedResponseExceptionDetail(statusCode = { 400 }),
73+
@UnexpectedResponseExceptionDetail(statusCode = { 403 }, exceptionBodyClass = OperationError.class),
74+
@UnexpectedResponseExceptionDetail(exceptionBodyClass = ServiceError.class)
75+
})
6976
Response<Foo> getFoo(@PathParam("key") String key, @QueryParam("label") String label,
7077
@HeaderParam("Sync-Token") String syncToken);
7178

@@ -302,26 +309,37 @@ HttpBinJSON putWithUnexpectedResponse(@HostParam("uri") String uri,
302309
@BodyParam(ContentType.APPLICATION_OCTET_STREAM) String putBody);
303310

304311
@HttpRequestInformation(method = HttpMethod.PUT, path = "put", expectedStatusCodes = { 201 })
305-
@UnexpectedResponseExceptionDetail(exceptionBodyClass = HttpBinJSON.class)
312+
@UnexpectedResponseExceptionDetail(exceptionBodyClass = ServiceError.class)
306313
HttpBinJSON putWithUnexpectedResponseAndExceptionType(@HostParam("uri") String uri,
307314
@BodyParam(ContentType.APPLICATION_OCTET_STREAM) String putBody);
308315

309316
@HttpRequestInformation(method = HttpMethod.PUT, path = "put", expectedStatusCodes = { 201 })
310-
@UnexpectedResponseExceptionDetail(statusCode = { 200 }, exceptionBodyClass = HttpBinJSON.class)
317+
@UnexpectedResponseExceptionDetail(statusCode = { 200 }, exceptionBodyClass = ServiceError.class)
311318
HttpBinJSON putWithUnexpectedResponseAndDeterminedExceptionType(@HostParam("uri") String uri,
312319
@BodyParam(ContentType.APPLICATION_OCTET_STREAM) String putBody);
313320

314321
@HttpRequestInformation(method = HttpMethod.PUT, path = "put", expectedStatusCodes = { 201 })
315-
@UnexpectedResponseExceptionDetail(statusCode = { 400 })
316-
@UnexpectedResponseExceptionDetail(exceptionBodyClass = HttpBinJSON.class)
317-
HttpBinJSON putWithUnexpectedResponseAndFallthroughExceptionType(@HostParam("uri") String uri,
322+
@UnexpectedResponseExceptionDetails({
323+
@UnexpectedResponseExceptionDetail(statusCode = { 400 }),
324+
@UnexpectedResponseExceptionDetail(statusCode = { 403 }, exceptionBodyClass = ServiceError.class)
325+
})HttpBinJSON putWithUnexpectedResponseAndFallthroughExceptionType(@HostParam("uri") String uri,
318326
@BodyParam(ContentType.APPLICATION_OCTET_STREAM) String putBody);
319327

320328
@HttpRequestInformation(method = HttpMethod.PUT, path = "put", expectedStatusCodes = { 201 })
321-
@UnexpectedResponseExceptionDetail(statusCode = { 400 }, exceptionBodyClass = HttpBinJSON.class)
329+
@UnexpectedResponseExceptionDetail(statusCode = { 400 }, exceptionBodyClass = ServiceError.class)
322330
HttpBinJSON putWithUnexpectedResponseAndNoFallthroughExceptionType(@HostParam("uri") String uri,
323331
@BodyParam(ContentType.APPLICATION_OCTET_STREAM) String putBody);
324332

333+
@HttpRequestInformation(method = HttpMethod.PUT, path = "put", expectedStatusCodes = { 201 })
334+
@UnexpectedResponseExceptionDetails({
335+
@UnexpectedResponseExceptionDetail(statusCode = { 400 }, exceptionBodyClass = ServiceError.class),
336+
@UnexpectedResponseExceptionDetail(statusCode = { 403 }, exceptionBodyClass = OperationError.class)
337+
})
338+
HttpBinJSON unexpectedResponseWithStatusCodeAndExceptionType(
339+
@HostParam("uri") String uri,
340+
@BodyParam(ContentType.APPLICATION_OCTET_STREAM) String putBody
341+
);
342+
325343
@HttpRequestInformation(method = HttpMethod.POST, path = "post", expectedStatusCodes = { 200 })
326344
HttpBinJSON post(@HostParam("uri") String uri,
327345
@BodyParam(ContentType.APPLICATION_OCTET_STREAM) String postBody);

0 commit comments

Comments
 (0)