13
13
import io .clientcore .core .instrumentation .logging .ClientLogger ;
14
14
import io .clientcore .core .serialization .json .JsonSerializer ;
15
15
import io .clientcore .core .serialization .xml .XmlSerializer ;
16
- import io .clientcore .core .utils .CoreUtils ;
17
16
import java .lang .reflect .ParameterizedType ;
17
+ import io .clientcore .core .utils .CoreUtils ;
18
18
import io .clientcore .core .serialization .SerializationFormat ;
19
+ import java .io .IOException ;
20
+ import io .clientcore .core .http .models .HttpResponseException ;
21
+ import java .nio .charset .StandardCharsets ;
19
22
20
23
/**
21
24
* Initializes a new instance of the SimpleXmlSerializableServiceImpl type.
@@ -65,7 +68,12 @@ public void sendApplicationXml(SimpleXmlSerializable simpleXmlSerializable) {
65
68
int responseCode = networkResponse .getStatusCode ();
66
69
boolean expectedResponse = responseCode == 200 ;
67
70
if (!expectedResponse ) {
68
- throw new RuntimeException ("Unexpected response code: " + responseCode );
71
+ if (networkResponse .getValue () == null || networkResponse .getValue ().toBytes ().length == 0 ) {
72
+ throw instantiateUnexpectedException (responseCode , networkResponse , null , null );
73
+ } else {
74
+ ParameterizedType returnType = null ;
75
+ throw instantiateUnexpectedException (responseCode , networkResponse , networkResponse .getValue (), CoreUtils .decodeNetworkResponse (networkResponse .getValue (), jsonSerializer , returnType ));
76
+ }
69
77
}
70
78
networkResponse .close ();
71
79
}
@@ -90,7 +98,12 @@ public void sendTextXml(SimpleXmlSerializable simpleXmlSerializable) {
90
98
int responseCode = networkResponse .getStatusCode ();
91
99
boolean expectedResponse = responseCode == 200 ;
92
100
if (!expectedResponse ) {
93
- throw new RuntimeException ("Unexpected response code: " + responseCode );
101
+ if (networkResponse .getValue () == null || networkResponse .getValue ().toBytes ().length == 0 ) {
102
+ throw instantiateUnexpectedException (responseCode , networkResponse , null , null );
103
+ } else {
104
+ ParameterizedType returnType = null ;
105
+ throw instantiateUnexpectedException (responseCode , networkResponse , networkResponse .getValue (), CoreUtils .decodeNetworkResponse (networkResponse .getValue (), jsonSerializer , returnType ));
106
+ }
94
107
}
95
108
networkResponse .close ();
96
109
}
@@ -107,7 +120,12 @@ public SimpleXmlSerializable getXml(String contentType) {
107
120
int responseCode = networkResponse .getStatusCode ();
108
121
boolean expectedResponse = responseCode == 200 ;
109
122
if (!expectedResponse ) {
110
- throw new RuntimeException ("Unexpected response code: " + responseCode );
123
+ if (networkResponse .getValue () == null || networkResponse .getValue ().toBytes ().length == 0 ) {
124
+ throw instantiateUnexpectedException (responseCode , networkResponse , null , null );
125
+ } else {
126
+ ParameterizedType returnType = CoreUtils .createParameterizedType (io .clientcore .annotation .processor .test .implementation .models .SimpleXmlSerializable .class );
127
+ throw instantiateUnexpectedException (responseCode , networkResponse , networkResponse .getValue (), CoreUtils .decodeNetworkResponse (networkResponse .getValue (), jsonSerializer , returnType ));
128
+ }
111
129
}
112
130
Object result = null ;
113
131
ParameterizedType returnType = CoreUtils .createParameterizedType (io .clientcore .annotation .processor .test .implementation .models .SimpleXmlSerializable .class );
@@ -135,7 +153,12 @@ public SimpleXmlSerializable getInvalidXml(String contentType) {
135
153
int responseCode = networkResponse .getStatusCode ();
136
154
boolean expectedResponse = responseCode == 200 ;
137
155
if (!expectedResponse ) {
138
- throw new RuntimeException ("Unexpected response code: " + responseCode );
156
+ if (networkResponse .getValue () == null || networkResponse .getValue ().toBytes ().length == 0 ) {
157
+ throw instantiateUnexpectedException (responseCode , networkResponse , null , null );
158
+ } else {
159
+ ParameterizedType returnType = CoreUtils .createParameterizedType (io .clientcore .annotation .processor .test .implementation .models .SimpleXmlSerializable .class );
160
+ throw instantiateUnexpectedException (responseCode , networkResponse , networkResponse .getValue (), CoreUtils .decodeNetworkResponse (networkResponse .getValue (), jsonSerializer , returnType ));
161
+ }
139
162
}
140
163
Object result = null ;
141
164
ParameterizedType returnType = CoreUtils .createParameterizedType (io .clientcore .annotation .processor .test .implementation .models .SimpleXmlSerializable .class );
@@ -150,4 +173,21 @@ public SimpleXmlSerializable getInvalidXml(String contentType) {
150
173
networkResponse .close ();
151
174
return (io .clientcore .annotation .processor .test .implementation .models .SimpleXmlSerializable ) result ;
152
175
}
176
+
177
+ private static HttpResponseException instantiateUnexpectedException (int responseCode , Response <BinaryData > response , BinaryData data , Object decodedValue ) {
178
+ StringBuilder exceptionMessage = new StringBuilder ("Status code " ).append (responseCode ).append (", " );
179
+ String contentType = response .getHeaders ().getValue (HttpHeaderName .CONTENT_TYPE );
180
+ if ("application/octet-stream" .equalsIgnoreCase (contentType )) {
181
+ String contentLength = response .getHeaders ().getValue (HttpHeaderName .CONTENT_LENGTH );
182
+ exceptionMessage .append ("(" ).append (contentLength ).append ("-byte body)" );
183
+ } else if (data == null || data .toBytes ().length == 0 ) {
184
+ exceptionMessage .append ("(empty body)" );
185
+ } else {
186
+ exceptionMessage .append ('"' ).append (new String (data .toBytes (), StandardCharsets .UTF_8 )).append ('"' );
187
+ }
188
+ if (decodedValue instanceof IOException || decodedValue instanceof IllegalStateException ) {
189
+ return new HttpResponseException (exceptionMessage .toString (), response , (Throwable ) decodedValue );
190
+ }
191
+ return new HttpResponseException (exceptionMessage .toString (), response , decodedValue );
192
+ }
153
193
}
0 commit comments