@@ -170,39 +170,14 @@ protected ReplicationDumpHeader toReplicationDumpHeader(HttpResponseEntity res)
170
170
}
171
171
172
172
/**
173
- * Creates an entity object
174
- *
175
- * @param res
176
- * the response of the database
177
- * @param clazz
178
- * the class of the entity object
179
- * @param pclazz
180
- * the class of the object wrapped in the entity object
181
- * @param validate
182
- * true for validation
183
- * @return the result entity object of class T (T extends BaseEntity)
184
- * @throws ArangoException
173
+ * Checks the Http response for database or server errors
174
+ * @param res the response of the database
175
+ * @return The Http status code
176
+ * @throws ArangoException if any error happened
185
177
*/
186
- protected <T extends BaseEntity > T createEntity (
187
- HttpResponseEntity res ,
188
- Class <T > clazz ,
189
- Class <?>[] pclazz ,
190
- boolean validate ) throws ArangoException {
191
- if (res == null ) {
192
- return null ;
193
- }
194
- boolean isDocumentEntity = false ;
195
- boolean requestSuccessful = true ;
196
- // the following was added to ensure, that attributes with a key like
197
- // "error", "code", "errorNum"
198
- // and "etag" will be serialized, when no error was thrown by the
199
- // database
200
- if (clazz == DocumentEntity .class ) {
201
- isDocumentEntity = true ;
202
- }
178
+ private int checkServerErrors (HttpResponseEntity res ) throws ArangoException {
203
179
int statusCode = res .getStatusCode ();
204
- if (statusCode >= 400 ) {
205
- requestSuccessful = false ;
180
+ if (statusCode >= 400 ) { // always throws ArangoException
206
181
DefaultEntity defaultEntity = new DefaultEntity ();
207
182
if (res .getText () != null && !res .getText ().equalsIgnoreCase ("" ) && statusCode != 500 ) {
208
183
JsonParser jsonParser = new JsonParser ();
@@ -263,7 +238,44 @@ protected <T extends BaseEntity> T createEntity(
263
238
arangoException .setCode (statusCode );
264
239
throw arangoException ;
265
240
}
266
-
241
+
242
+ return statusCode ;
243
+ }
244
+ /**
245
+ * Creates an entity object
246
+ *
247
+ * @param res
248
+ * the response of the database
249
+ * @param clazz
250
+ * the class of the entity object
251
+ * @param pclazz
252
+ * the class of the object wrapped in the entity object
253
+ * @param validate
254
+ * true for validation
255
+ * @return the result entity object of class T (T extends BaseEntity)
256
+ * @throws ArangoException
257
+ */
258
+ protected <T extends BaseEntity > T createEntity (
259
+ HttpResponseEntity res ,
260
+ Class <T > clazz ,
261
+ Class <?>[] pclazz ,
262
+ boolean validate ) throws ArangoException {
263
+ if (res == null ) {
264
+ return null ;
265
+ }
266
+ boolean isDocumentEntity = false ;
267
+ //boolean requestSuccessful = true;
268
+
269
+ // the following was added to ensure, that attributes with a key like
270
+ // "error", "code", "errorNum"
271
+ // and "etag" will be serialized, when no error was thrown by the
272
+ // database
273
+ if (clazz == DocumentEntity .class ) {
274
+ isDocumentEntity = true ;
275
+ }
276
+
277
+ int statusCode =checkServerErrors (res );
278
+
267
279
try {
268
280
EntityDeserializers .setParameterized (pclazz );
269
281
@@ -283,7 +295,7 @@ protected <T extends BaseEntity> T createEntity(
283
295
validate (res , entity );
284
296
}
285
297
286
- if (isDocumentEntity && requestSuccessful ) {
298
+ if (isDocumentEntity ) { // && requestSuccessful NOTE: no need for this, an exception is always thrown
287
299
entity .setCode (statusCode );
288
300
entity .setErrorMessage (null );
289
301
entity .setError (false );
@@ -295,6 +307,27 @@ protected <T extends BaseEntity> T createEntity(
295
307
EntityDeserializers .removeParameterized ();
296
308
}
297
309
}
310
+
311
+ /**
312
+ * Gets the raw JSON string with results, from the Http response
313
+ * @param res the response of the database
314
+ * @return A valid JSON string with the results
315
+ * @throws ArangoException
316
+ */
317
+ protected String getJSONResponseText (HttpResponseEntity res ) throws ArangoException {
318
+ if (res == null ) {
319
+ return null ;
320
+ }
321
+
322
+ checkServerErrors (res );
323
+
324
+ // no errors, return results as a JSON string
325
+ JsonParser jsonParser = new JsonParser ();
326
+ JsonElement jsonElement = jsonParser .parse (res .getText ());
327
+ JsonObject jsonObject = jsonElement .getAsJsonObject ();
328
+ JsonElement result = jsonObject .get ("result" );
329
+ return result .toString ();
330
+ }
298
331
299
332
protected <T > T createEntity (String str , Class <T > clazz , Class <?>... pclazz ) throws ArangoException {
300
333
try {
0 commit comments