Skip to content

Commit f49b422

Browse files
committed
Included exception handling when getting raw JSON
1 parent 285d2cd commit f49b422

File tree

4 files changed

+85
-6
lines changed

4 files changed

+85
-6
lines changed

src/main/java/com/arangodb/ArangoDriver.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@
7575
import com.arangodb.http.BatchHttpManager;
7676
import com.arangodb.http.BatchPart;
7777
import com.arangodb.http.HttpManager;
78-
import com.arangodb.http.HttpResponseEntity;
7978
import com.arangodb.http.InvocationHandlerImpl;
8079
import com.arangodb.impl.ImplFactory;
8180
import com.arangodb.impl.InternalBatchDriverImpl;
@@ -2478,7 +2477,7 @@ public <T> CursorResult<T> executeAqlQuery(
24782477
* @return
24792478
* @throws ArangoException
24802479
*/
2481-
public HttpResponseEntity executeAqlQueryJSON(
2480+
public String executeAqlQueryJSON(
24822481
String query,
24832482
Map<String, Object> bindVars,
24842483
AqlQueryOptions aqlQueryOptions

src/main/java/com/arangodb/BaseArangoDriver.java

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,87 @@ protected <T extends BaseEntity> T createEntity(
295295
EntityDeserializers.removeParameterized();
296296
}
297297
}
298+
299+
/**
300+
* Gets the raw JSON string with results, from the Http response
301+
* @param res the response of the database
302+
* @return
303+
* @throws ArangoException
304+
*/
305+
protected String getJSONResponseText(HttpResponseEntity res) throws ArangoException {
306+
if (res == null) {
307+
return null;
308+
}
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+
}
357+
358+
defaultEntity.setErrorMessage(statusPhrase);
359+
if (statusCode == 500) {
360+
defaultEntity.setErrorMessage(statusPhrase + ": " + res.getText());
361+
}
362+
}
363+
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+
372+
// no errors, return results as a JSON string
373+
JsonParser jsonParser = new JsonParser();
374+
JsonElement jsonElement = jsonParser.parse(res.getText());
375+
JsonObject jsonObject = jsonElement.getAsJsonObject();
376+
JsonElement result = jsonObject.get("result");
377+
return result.toString();
378+
}
298379

299380
protected <T> T createEntity(String str, Class<T> clazz, Class<?>... pclazz) throws ArangoException {
300381
try {

src/main/java/com/arangodb/InternalCursorDriver.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import com.arangodb.entity.QueriesResultEntity;
99
import com.arangodb.entity.QueryTrackingPropertiesEntity;
1010
import com.arangodb.entity.ShortestPathEntity;
11-
import com.arangodb.http.HttpResponseEntity;
1211
import com.arangodb.impl.BaseDriverInterface;
1312
import com.arangodb.util.AqlQueryOptions;
1413
import com.arangodb.util.ShortestPathOptions;
@@ -42,7 +41,7 @@ <T> CursorResult<T> executeAqlQuery(
4241
Class<T> clazz) throws ArangoException;
4342

4443
// return the raw JSON response from server
45-
HttpResponseEntity executeAqlQueryJSON(
44+
String executeAqlQueryJSON(
4645
String database,
4746
String query,
4847
Map<String, Object> bindVars,

src/main/java/com/arangodb/impl/InternalCursorDriverImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,13 @@ public CursorEntity<?> validateQuery(String database, String query) throws Arang
5555
}
5656

5757
@Override
58-
public HttpResponseEntity executeAqlQueryJSON(
58+
public String executeAqlQueryJSON(
5959
String database,
6060
String query,
6161
Map<String, Object> bindVars,
6262
AqlQueryOptions aqlQueryOptions) throws ArangoException {
6363

64-
return getCursor(database, query, bindVars, aqlQueryOptions);
64+
return getJSONResponseText(getCursor(database, query, bindVars, aqlQueryOptions));
6565
}
6666

6767
@SuppressWarnings("unchecked")

0 commit comments

Comments
 (0)