3737import com .arangodb .util .StringUtils ;
3838import com .google .gson .JsonElement ;
3939import com .google .gson .JsonObject ;
40+ import com .google .gson .JsonParseException ;
4041import com .google .gson .JsonParser ;
42+ import com .google .gson .JsonSyntaxException ;
4143
4244/**
4345 * @author tamtam180 - kirscheless at gmail.com
@@ -92,9 +94,9 @@ protected void validateDocumentHandle(String documentHandle) throws ArangoExcept
9294 * @param database
9395 * @param allowNull
9496 * @throws ArangoException
95- * @see <a
96- * href= "http://www.arangodb.com/manuals/current/NamingConventions.html#DatabaseNames">DatabaseNames
97- * documentation</a>
97+ * @see <a href=
98+ * "http://www.arangodb.com/manuals/current/NamingConventions.html#DatabaseNames">
99+ * DatabaseNames documentation</a>
98100 */
99101 protected void validateDatabaseName (String database , boolean allowNull ) throws ArangoException {
100102 boolean valid = false ;
@@ -171,13 +173,17 @@ protected ReplicationDumpHeader toReplicationDumpHeader(HttpResponseEntity res)
171173
172174 /**
173175 * Checks the Http response for database or server errors
174- * @param res the response of the database
176+ *
177+ * @param res
178+ * the response of the database
175179 * @return The Http status code
176- * @throws ArangoException if any error happened
180+ * @throws ArangoException
181+ * if any error happened
177182 */
178183 private int checkServerErrors (HttpResponseEntity res ) throws ArangoException {
179184 int statusCode = res .getStatusCode ();
180- if (statusCode >= 400 ) { // always throws ArangoException
185+
186+ if (statusCode >= 400 ) { // always throws ArangoException
181187 DefaultEntity defaultEntity = new DefaultEntity ();
182188 if (res .getText () != null && !res .getText ().equalsIgnoreCase ("" ) && statusCode != 500 ) {
183189 JsonParser jsonParser = new JsonParser ();
@@ -238,9 +244,10 @@ private int checkServerErrors(HttpResponseEntity res) throws ArangoException {
238244 arangoException .setCode (statusCode );
239245 throw arangoException ;
240246 }
241-
247+
242248 return statusCode ;
243249 }
250+
244251 /**
245252 * Creates an entity object
246253 *
@@ -264,18 +271,18 @@ protected <T extends BaseEntity> T createEntity(
264271 return null ;
265272 }
266273 boolean isDocumentEntity = false ;
267- //boolean requestSuccessful = true;
268-
274+ // boolean requestSuccessful = true;
275+
269276 // the following was added to ensure, that attributes with a key like
270277 // "error", "code", "errorNum"
271278 // and "etag" will be serialized, when no error was thrown by the
272279 // database
273280 if (clazz == DocumentEntity .class ) {
274281 isDocumentEntity = true ;
275282 }
276-
277- int statusCode = checkServerErrors (res );
278-
283+
284+ int statusCode = checkServerErrors (res );
285+
279286 try {
280287 EntityDeserializers .setParameterized (pclazz );
281288
@@ -295,7 +302,8 @@ protected <T extends BaseEntity> T createEntity(
295302 validate (res , entity );
296303 }
297304
298- if (isDocumentEntity ) { // && requestSuccessful NOTE: no need for this, an exception is always thrown
305+ if (isDocumentEntity ) { // && requestSuccessful NOTE: no need for
306+ // this, an exception is always thrown
299307 entity .setCode (statusCode );
300308 entity .setErrorMessage (null );
301309 entity .setError (false );
@@ -307,10 +315,12 @@ protected <T extends BaseEntity> T createEntity(
307315 EntityDeserializers .removeParameterized ();
308316 }
309317 }
310-
318+
311319 /**
312320 * Gets the raw JSON string with results, from the Http response
313- * @param res the response of the database
321+ *
322+ * @param res
323+ * the response of the database
314324 * @return A valid JSON string with the results
315325 * @throws ArangoException
316326 */
@@ -396,15 +406,22 @@ protected void validate(HttpResponseEntity res, BaseEntity entity) throws Arango
396406 @ SuppressWarnings ("unchecked" )
397407 protected <T > T createEntityImpl (HttpResponseEntity res , Class <T > type ) throws ArangoException {
398408 if (res .isJsonResponse ()) {
399- T entity = EntityFactory .createEntity (res .getText (), type );
400- return entity ;
409+ try {
410+ return EntityFactory .createEntity (res .getText (), type );
411+ } catch (JsonSyntaxException e ) {
412+ throw new ArangoException ("got JsonSyntaxException while creating entity" , e );
413+ } catch (JsonParseException e ) {
414+ throw new ArangoException ("got JsonParseException while creating entity" , e );
415+ }
401416 }
402417 if (res .isDumpResponse () && StreamEntity .class .isAssignableFrom (type )) {
403418 return (T ) new StreamEntity (res .getStream ());
404419 }
420+ if (res .getText () != null && res .getText ().length () > 0 ) {
421+ throw new ArangoException ("expected JSON result from server but got: " + res .getText ());
422+ }
423+
405424 return null ;
406- // throw new IllegalStateException("unknown response content-type:" +
407- // res.getContentType());
408425 }
409426
410427 protected String createEndpointUrl (String database , Object ... paths ) throws ArangoException {
0 commit comments