Skip to content

Commit abc7955

Browse files
author
Mark
committed
improved ArangoDBException with responseCode, errorNum, errorMessage
1 parent 368764c commit abc7955

File tree

4 files changed

+37
-9
lines changed

4 files changed

+37
-9
lines changed

ChangeLog

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ v4.1.8 (2017-02-xx)
55
* changed java.sql.Timestamp serialization from VPack.date to VPack.string (ISO 8601)
66
* added byte[] de-/serialization from/to VPack.string (Base64)
77
* added ArangoCollection.drop(isSystem)
8+
* improved ArangoDBException with responseCode, errorNum, errorMessage
89

910
v4.1.7 (2017-01-26)
1011
---------------------------

src/main/java/com/arangodb/ArangoDBException.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,22 @@
2020

2121
package com.arangodb;
2222

23+
import com.arangodb.entity.ErrorEntity;
24+
2325
/**
2426
* @author Mark - mark at arangodb.com
2527
*
2628
*/
2729
public class ArangoDBException extends RuntimeException {
2830

2931
private static final long serialVersionUID = 6165638002614173801L;
32+
private ErrorEntity entity = null;
33+
34+
public ArangoDBException(final ErrorEntity errorEntity) {
35+
super(String.format("Response: %s, Error: %s - %s", errorEntity.getCode(), errorEntity.getErrorNum(),
36+
errorEntity.getErrorMessage()));
37+
this.entity = errorEntity;
38+
}
3039

3140
public ArangoDBException(final String message) {
3241
super(message);
@@ -36,4 +45,16 @@ public ArangoDBException(final Throwable cause) {
3645
super(cause);
3746
}
3847

48+
public String getErrorMessage() {
49+
return entity != null ? entity.getErrorMessage() : null;
50+
}
51+
52+
public int getResponseCode() {
53+
return entity != null ? entity.getCode() : null;
54+
}
55+
56+
public int getErrorNum() {
57+
return entity != null ? entity.getErrorNum() : null;
58+
}
59+
3960
}

src/main/java/com/arangodb/internal/velocystream/Communication.java

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,8 @@ protected void checkError(final Response response) throws ArangoDBException {
101101
try {
102102
if (response.getResponseCode() >= ERROR_STATUS) {
103103
if (response.getBody() != null) {
104-
throw new ArangoDBException(createErrorMessage(response));
104+
final ErrorEntity errorEntity = vpack.deserialize(response.getBody(), ErrorEntity.class);
105+
throw new ArangoDBException(errorEntity);
105106
} else {
106107
throw new ArangoDBException(String.format("Response Code: %s", response.getResponseCode()));
107108
}
@@ -111,14 +112,6 @@ protected void checkError(final Response response) throws ArangoDBException {
111112
}
112113
}
113114

114-
private String createErrorMessage(final Response response) throws VPackParserException {
115-
String errorMessage;
116-
final ErrorEntity errorEntity = vpack.deserialize(response.getBody(), ErrorEntity.class);
117-
errorMessage = String.format("Response: %s, Error: %s - %s", errorEntity.getCode(), errorEntity.getErrorNum(),
118-
errorEntity.getErrorMessage());
119-
return errorMessage;
120-
}
121-
122115
protected Response createResponse(final Message messsage) throws VPackParserException {
123116
final Response response = vpack.deserialize(messsage.getHead(), Response.class);
124117
if (messsage.getBody() != null) {

src/test/java/com/arangodb/ArangoDBTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,4 +393,17 @@ public void setLogLevel() {
393393
arangoDB.setLogLevel(entity);
394394
}
395395
}
396+
397+
@Test
398+
public void arangoDBException() {
399+
final ArangoDB arangoDB = new ArangoDB.Builder().build();
400+
try {
401+
arangoDB.db("no").getInfo();
402+
fail();
403+
} catch (final ArangoDBException e) {
404+
assertThat(e.getResponseCode(), is(404));
405+
assertThat(e.getErrorNum(), is(1228));
406+
assertThat(e.getErrorMessage(), is("database not found"));
407+
}
408+
}
396409
}

0 commit comments

Comments
 (0)