@@ -170,39 +170,14 @@ protected ReplicationDumpHeader toReplicationDumpHeader(HttpResponseEntity res)
170170 }
171171
172172 /**
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
176+ * @throws ArangoException if any error happened
185177 */
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 {
203179 int statusCode = res .getStatusCode ();
204- if (statusCode >= 400 ) {
205- requestSuccessful = false ;
180+ if (statusCode >= 400 ) { // always throws ArangoException
206181 DefaultEntity defaultEntity = new DefaultEntity ();
207182 if (res .getText () != null && !res .getText ().equalsIgnoreCase ("" ) && statusCode != 500 ) {
208183 JsonParser jsonParser = new JsonParser ();
@@ -263,7 +238,44 @@ protected <T extends BaseEntity> T createEntity(
263238 arangoException .setCode (statusCode );
264239 throw arangoException ;
265240 }
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+
267279 try {
268280 EntityDeserializers .setParameterized (pclazz );
269281
@@ -283,7 +295,7 @@ protected <T extends BaseEntity> T createEntity(
283295 validate (res , entity );
284296 }
285297
286- if (isDocumentEntity && requestSuccessful ) {
298+ if (isDocumentEntity ) { // && requestSuccessful NOTE: no need for this, an exception is always thrown
287299 entity .setCode (statusCode );
288300 entity .setErrorMessage (null );
289301 entity .setError (false );
@@ -299,76 +311,16 @@ protected <T extends BaseEntity> T createEntity(
299311 /**
300312 * Gets the raw JSON string with results, from the Http response
301313 * @param res the response of the database
302- * @return
314+ * @return A valid JSON string with the results
303315 * @throws ArangoException
304316 */
305317 protected String getJSONResponseText (HttpResponseEntity res ) throws ArangoException {
306318 if (res == null ) {
307319 return null ;
308320 }
309- int statusCode = res .getStatusCode ();
310- if (statusCode >= 400 ) {
311- DefaultEntity defaultEntity = new DefaultEntity ();
312- if (res .getText () != null && !res .getText ().equalsIgnoreCase ("" ) && statusCode != 500 ) {
313- JsonParser jsonParser = new JsonParser ();
314- JsonElement jsonElement = jsonParser .parse (res .getText ());
315- JsonObject jsonObject = jsonElement .getAsJsonObject ();
316- JsonElement errorMessage = jsonObject .get ("errorMessage" );
317- defaultEntity .setErrorMessage (errorMessage .getAsString ());
318- JsonElement errorNumber = jsonObject .get ("errorNum" );
319- defaultEntity .setErrorNumber (errorNumber .getAsInt ());
320- } else {
321- String statusPhrase = "" ;
322- switch (statusCode ) {
323- case 400 :
324- statusPhrase = "Bad Request" ;
325- break ;
326- case 401 :
327- statusPhrase = "Unauthorized" ;
328- break ;
329- case 403 :
330- statusPhrase = "Forbidden" ;
331- break ;
332- case 404 :
333- statusPhrase = "Not Found" ;
334- break ;
335- case 405 :
336- statusPhrase = "Method Not Allowed" ;
337- break ;
338- case 406 :
339- statusPhrase = "Not Acceptable" ;
340- break ;
341- case 407 :
342- statusPhrase = "Proxy Authentication Required" ;
343- break ;
344- case 408 :
345- statusPhrase = "Request Time-out" ;
346- break ;
347- case 409 :
348- statusPhrase = "Conflict" ;
349- break ;
350- case 500 :
351- statusPhrase = "Internal Server Error" ;
352- break ;
353- default :
354- statusPhrase = "unknown error" ;
355- break ;
356- }
357321
358- defaultEntity .setErrorMessage (statusPhrase );
359- if (statusCode == 500 ) {
360- defaultEntity .setErrorMessage (statusPhrase + ": " + res .getText ());
361- }
362- }
322+ checkServerErrors (res );
363323
364- defaultEntity .setCode (statusCode );
365- defaultEntity .setStatusCode (statusCode );
366- defaultEntity .setError (true );
367- ArangoException arangoException = new ArangoException (defaultEntity );
368- arangoException .setCode (statusCode );
369- throw arangoException ;
370- }
371-
372324 // no errors, return results as a JSON string
373325 JsonParser jsonParser = new JsonParser ();
374326 JsonElement jsonElement = jsonParser .parse (res .getText ());
0 commit comments