Skip to content

Commit af2ee68

Browse files
committed
use HttpResponseException
1 parent 4231e3a commit af2ee68

File tree

8 files changed

+695
-128
lines changed

8 files changed

+695
-128
lines changed
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import java.lang.reflect.ParameterizedType;
2222
import java.lang.reflect.Type;
2323
import java.util.List;
24+
import io.clientcore.core.http.models.HttpResponseException;
25+
import java.nio.charset.StandardCharsets;
2426

2527
/**
2628
* Initializes a new instance of the SimpleXmlSerializableServiceImpl type.
@@ -70,7 +72,12 @@ public void sendApplicationXml(SimpleXmlSerializable simpleXmlSerializable) {
7072
int responseCode = networkResponse.getStatusCode();
7173
boolean expectedResponse = responseCode == 200;
7274
if (!expectedResponse) {
73-
throw new RuntimeException("Unexpected response code: " + responseCode);
75+
if (networkResponse.getValue() == null || networkResponse.getValue().toBytes().length == 0) {
76+
throw instantiateUnexpectedException(responseCode, networkResponse, null, null);
77+
} else {
78+
ParameterizedType returnType = null;
79+
throw instantiateUnexpectedException(responseCode, networkResponse, networkResponse.getValue(), decodeNetworkResponse(networkResponse.getValue(), jsonSerializer, returnType));
80+
}
7481
}
7582
networkResponse.close();
7683
}
@@ -95,7 +102,12 @@ public void sendTextXml(SimpleXmlSerializable simpleXmlSerializable) {
95102
int responseCode = networkResponse.getStatusCode();
96103
boolean expectedResponse = responseCode == 200;
97104
if (!expectedResponse) {
98-
throw new RuntimeException("Unexpected response code: " + responseCode);
105+
if (networkResponse.getValue() == null || networkResponse.getValue().toBytes().length == 0) {
106+
throw instantiateUnexpectedException(responseCode, networkResponse, null, null);
107+
} else {
108+
ParameterizedType returnType = null;
109+
throw instantiateUnexpectedException(responseCode, networkResponse, networkResponse.getValue(), decodeNetworkResponse(networkResponse.getValue(), jsonSerializer, returnType));
110+
}
99111
}
100112
networkResponse.close();
101113
}
@@ -112,7 +124,12 @@ public SimpleXmlSerializable getXml(String contentType) {
112124
int responseCode = networkResponse.getStatusCode();
113125
boolean expectedResponse = responseCode == 200;
114126
if (!expectedResponse) {
115-
throw new RuntimeException("Unexpected response code: " + responseCode);
127+
if (networkResponse.getValue() == null || networkResponse.getValue().toBytes().length == 0) {
128+
throw instantiateUnexpectedException(responseCode, networkResponse, null, null);
129+
} else {
130+
ParameterizedType returnType = CoreUtils.createParameterizedType(io.clientcore.annotation.processor.test.implementation.models.SimpleXmlSerializable.class);
131+
throw instantiateUnexpectedException(responseCode, networkResponse, networkResponse.getValue(), decodeNetworkResponse(networkResponse.getValue(), jsonSerializer, returnType));
132+
}
116133
}
117134
Object result = null;
118135
ParameterizedType returnType = CoreUtils.createParameterizedType(io.clientcore.annotation.processor.test.implementation.models.SimpleXmlSerializable.class);
@@ -130,16 +147,22 @@ public SimpleXmlSerializable getXml(String contentType) {
130147

131148
@SuppressWarnings({ "unchecked", "cast" })
132149
@Override
133-
public SimpleXmlSerializable getInvalidXml() {
150+
public SimpleXmlSerializable getInvalidXml(String contentType) {
134151
String url = "http://localhost/getInvalidXml";
135152
// Create the HTTP request
136153
HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod.GET).setUri(url);
154+
httpRequest.getHeaders().add(HttpHeaderName.CONTENT_TYPE, contentType);
137155
// Send the request through the httpPipeline
138156
Response<BinaryData> networkResponse = this.httpPipeline.send(httpRequest);
139157
int responseCode = networkResponse.getStatusCode();
140158
boolean expectedResponse = responseCode == 200;
141159
if (!expectedResponse) {
142-
throw new RuntimeException("Unexpected response code: " + responseCode);
160+
if (networkResponse.getValue() == null || networkResponse.getValue().toBytes().length == 0) {
161+
throw instantiateUnexpectedException(responseCode, networkResponse, null, null);
162+
} else {
163+
ParameterizedType returnType = CoreUtils.createParameterizedType(io.clientcore.annotation.processor.test.implementation.models.SimpleXmlSerializable.class);
164+
throw instantiateUnexpectedException(responseCode, networkResponse, networkResponse.getValue(), decodeNetworkResponse(networkResponse.getValue(), jsonSerializer, returnType));
165+
}
143166
}
144167
Object result = null;
145168
ParameterizedType returnType = CoreUtils.createParameterizedType(io.clientcore.annotation.processor.test.implementation.models.SimpleXmlSerializable.class);
@@ -177,7 +200,25 @@ private static Object decodeNetworkResponse(BinaryData data, ObjectSerializer se
177200
}
178201
return serializer.deserializeFromBytes(data.toBytes(), token);
179202
} catch (IOException e) {
180-
throw LOGGER.logThrowableAsError(CoreException.from(e));
203+
LOGGER.atWarning().setThrowable(e).log(() -> "Failed to deserialize the error entity.");
204+
return null;
205+
}
206+
}
207+
208+
private static HttpResponseException instantiateUnexpectedException(int responseCode, Response<BinaryData> response, BinaryData data, Object decodedValue) {
209+
StringBuilder exceptionMessage = new StringBuilder("Status code ").append(responseCode).append(", ");
210+
String contentType = response.getHeaders().getValue(HttpHeaderName.CONTENT_TYPE);
211+
if ("application/octet-stream".equalsIgnoreCase(contentType)) {
212+
String contentLength = response.getHeaders().getValue(HttpHeaderName.CONTENT_LENGTH);
213+
exceptionMessage.append("(").append(contentLength).append("-byte body)");
214+
} else if (data == null || data.toBytes().length == 0) {
215+
exceptionMessage.append("(empty body)");
216+
} else {
217+
exceptionMessage.append('"').append(new String(data.toBytes(), StandardCharsets.UTF_8)).append('"');
218+
}
219+
if (decodedValue instanceof IOException || decodedValue instanceof IllegalStateException) {
220+
return new HttpResponseException(exceptionMessage.toString(), response, (Throwable) decodedValue);
181221
}
222+
return new HttpResponseException(exceptionMessage.toString(), response, decodedValue);
182223
}
183224
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,5 +70,5 @@ static SimpleXmlSerializableService getNewInstance(HttpPipeline pipeline) {
7070
* @return The retrieved invalid XML payload.
7171
*/
7272
@HttpRequestInformation(method = HttpMethod.GET, path = "getInvalidXml", expectedStatusCodes = { 200 })
73-
SimpleXmlSerializable getInvalidXml();
73+
SimpleXmlSerializable getInvalidXml(@HeaderParam("Content-Type") String contentType);
7474
}

0 commit comments

Comments
 (0)