21
21
import java .lang .reflect .ParameterizedType ;
22
22
import java .lang .reflect .Type ;
23
23
import java .util .List ;
24
+ import io .clientcore .core .http .models .HttpResponseException ;
25
+ import java .nio .charset .StandardCharsets ;
24
26
25
27
/**
26
28
* Initializes a new instance of the SimpleXmlSerializableServiceImpl type.
@@ -70,7 +72,12 @@ public void sendApplicationXml(SimpleXmlSerializable simpleXmlSerializable) {
70
72
int responseCode = networkResponse .getStatusCode ();
71
73
boolean expectedResponse = responseCode == 200 ;
72
74
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
+ }
74
81
}
75
82
networkResponse .close ();
76
83
}
@@ -95,7 +102,12 @@ public void sendTextXml(SimpleXmlSerializable simpleXmlSerializable) {
95
102
int responseCode = networkResponse .getStatusCode ();
96
103
boolean expectedResponse = responseCode == 200 ;
97
104
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
+ }
99
111
}
100
112
networkResponse .close ();
101
113
}
@@ -112,7 +124,12 @@ public SimpleXmlSerializable getXml(String contentType) {
112
124
int responseCode = networkResponse .getStatusCode ();
113
125
boolean expectedResponse = responseCode == 200 ;
114
126
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
+ }
116
133
}
117
134
Object result = null ;
118
135
ParameterizedType returnType = CoreUtils .createParameterizedType (io .clientcore .annotation .processor .test .implementation .models .SimpleXmlSerializable .class );
@@ -140,7 +157,12 @@ public SimpleXmlSerializable getInvalidXml(String contentType) {
140
157
int responseCode = networkResponse .getStatusCode ();
141
158
boolean expectedResponse = responseCode == 200 ;
142
159
if (!expectedResponse ) {
143
- 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
+ }
144
166
}
145
167
Object result = null ;
146
168
ParameterizedType returnType = CoreUtils .createParameterizedType (io .clientcore .annotation .processor .test .implementation .models .SimpleXmlSerializable .class );
@@ -178,7 +200,25 @@ private static Object decodeNetworkResponse(BinaryData data, ObjectSerializer se
178
200
}
179
201
return serializer .deserializeFromBytes (data .toBytes (), token );
180
202
} catch (IOException e ) {
181
- 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 );
182
221
}
222
+ return new HttpResponseException (exceptionMessage .toString (), response , decodedValue );
183
223
}
184
224
}
0 commit comments