Skip to content

Commit 5eaab08

Browse files
authored
Better error status handling and header name constants (Azure#45192)
Better error status handling and header name constants
1 parent afa63c8 commit 5eaab08

7 files changed

Lines changed: 263 additions & 80 deletions

File tree

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +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;
1617

1718
/**
1819
* Initializes a new instance of the HostEdgeCase1ServiceImpl type.
@@ -53,7 +54,9 @@ public byte[] getByteArray(String url, int numberOfBytes) {
5354
int responseCode = networkResponse.getStatusCode();
5455
boolean expectedResponse = responseCode == 200;
5556
if (!expectedResponse) {
56-
throw new RuntimeException("Unexpected response code: " + responseCode);
57+
String errorMessage = networkResponse.getValue().toString();
58+
networkResponse.close();
59+
throw new HttpResponseException(errorMessage, networkResponse, null);
5760
}
5861
BinaryData responseBody = networkResponse.getValue();
5962
byte[] responseBodyBytes = responseBody != null ? responseBody.toBytes() : null;

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +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;
1617

1718
/**
1819
* Initializes a new instance of the HostEdgeCase2ServiceImpl type.
@@ -53,7 +54,9 @@ public byte[] getByteArray(String uri, int numberOfBytes) {
5354
int responseCode = networkResponse.getStatusCode();
5455
boolean expectedResponse = responseCode == 200;
5556
if (!expectedResponse) {
56-
throw new RuntimeException("Unexpected response code: " + responseCode);
57+
String errorMessage = networkResponse.getValue().toString();
58+
networkResponse.close();
59+
throw new HttpResponseException(errorMessage, networkResponse, null);
5760
}
5861
BinaryData responseBody = networkResponse.getValue();
5962
byte[] responseBodyBytes = responseBody != null ? responseBody.toBytes() : null;

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +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;
1617

1718
/**
1819
* Initializes a new instance of the ParameterizedHostServiceImpl type.
@@ -53,7 +54,9 @@ public byte[] getByteArray(String scheme, String host, int numberOfBytes) {
5354
int responseCode = networkResponse.getStatusCode();
5455
boolean expectedResponse = responseCode == 200;
5556
if (!expectedResponse) {
56-
throw new RuntimeException("Unexpected response code: " + responseCode);
57+
String errorMessage = networkResponse.getValue().toString();
58+
networkResponse.close();
59+
throw new HttpResponseException(errorMessage, networkResponse, null);
5760
}
5861
BinaryData responseBody = networkResponse.getValue();
5962
byte[] responseBodyBytes = responseBody != null ? responseBody.toBytes() : null;

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +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;
1718
import io.clientcore.core.utils.CoreUtils;
1819
import io.clientcore.core.utils.Base64Uri;
1920
import java.lang.reflect.ParameterizedType;
@@ -58,7 +59,9 @@ public HttpBinJSON get(String scheme, String hostPart1, String hostPart2) {
5859
int responseCode = networkResponse.getStatusCode();
5960
boolean expectedResponse = responseCode == 200;
6061
if (!expectedResponse) {
61-
throw new RuntimeException("Unexpected response code: " + responseCode);
62+
String errorMessage = networkResponse.getValue().toString();
63+
networkResponse.close();
64+
throw new HttpResponseException(errorMessage, networkResponse, null);
6265
}
6366
Object result = null;
6467
ParameterizedType returnType = CoreUtils.createParameterizedType(io.clientcore.annotation.processor.test.implementation.models.HttpBinJSON.class);

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

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +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;
1920
import io.clientcore.core.http.models.HttpHeader;
2021
import io.clientcore.core.utils.Base64Uri;
2122
import java.lang.reflect.ParameterizedType;
@@ -68,7 +69,9 @@ public void sendApplicationXml(SimpleXmlSerializable simpleXmlSerializable) {
6869
int responseCode = networkResponse.getStatusCode();
6970
boolean expectedResponse = responseCode == 200;
7071
if (!expectedResponse) {
71-
throw new RuntimeException("Unexpected response code: " + responseCode);
72+
String errorMessage = networkResponse.getValue().toString();
73+
networkResponse.close();
74+
throw new HttpResponseException(errorMessage, networkResponse, null);
7275
}
7376
networkResponse.close();
7477
}
@@ -93,7 +96,9 @@ public void sendTextXml(SimpleXmlSerializable simpleXmlSerializable) {
9396
int responseCode = networkResponse.getStatusCode();
9497
boolean expectedResponse = responseCode == 200;
9598
if (!expectedResponse) {
96-
throw new RuntimeException("Unexpected response code: " + responseCode);
99+
String errorMessage = networkResponse.getValue().toString();
100+
networkResponse.close();
101+
throw new HttpResponseException(errorMessage, networkResponse, null);
97102
}
98103
networkResponse.close();
99104
}
@@ -112,7 +117,9 @@ public SimpleXmlSerializable getXml(String contentType) {
112117
int responseCode = networkResponse.getStatusCode();
113118
boolean expectedResponse = responseCode == 200;
114119
if (!expectedResponse) {
115-
throw new RuntimeException("Unexpected response code: " + responseCode);
120+
String errorMessage = networkResponse.getValue().toString();
121+
networkResponse.close();
122+
throw new HttpResponseException(errorMessage, networkResponse, null);
116123
}
117124
Object result = null;
118125
ParameterizedType returnType = CoreUtils.createParameterizedType(io.clientcore.annotation.processor.test.implementation.models.SimpleXmlSerializable.class);
@@ -142,7 +149,9 @@ public SimpleXmlSerializable getInvalidXml(String contentType) {
142149
int responseCode = networkResponse.getStatusCode();
143150
boolean expectedResponse = responseCode == 200;
144151
if (!expectedResponse) {
145-
throw new RuntimeException("Unexpected response code: " + responseCode);
152+
String errorMessage = networkResponse.getValue().toString();
153+
networkResponse.close();
154+
throw new HttpResponseException(errorMessage, networkResponse, null);
146155
}
147156
Object result = null;
148157
ParameterizedType returnType = CoreUtils.createParameterizedType(io.clientcore.annotation.processor.test.implementation.models.SimpleXmlSerializable.class);

0 commit comments

Comments
 (0)