28
28
import com .arangodb .internal .net .ArangoDBUnavailableException ;
29
29
import com .arangodb .internal .serde .InternalSerde ;
30
30
31
+ import java .nio .charset .Charset ;
32
+ import java .nio .charset .StandardCharsets ;
31
33
import java .util .concurrent .TimeoutException ;
32
34
33
35
/**
@@ -38,12 +40,14 @@ public final class ResponseUtils {
38
40
private static final int ERROR_STATUS = 300 ;
39
41
private static final int ERROR_INTERNAL = 503 ;
40
42
private static final String HEADER_ENDPOINT = "x-arango-endpoint" ;
43
+ private static final String CONTENT_TYPE = "content-type" ;
44
+ private static final String TEXT_PLAIN = "text/plain" ;
41
45
42
46
private ResponseUtils () {
43
47
super ();
44
48
}
45
49
46
- public static ArangoDBException translateError (final InternalSerde util , final InternalResponse response ) {
50
+ public static ArangoDBException translateError (InternalSerde serde , InternalResponse response ) {
47
51
final int responseCode = response .getResponseCode ();
48
52
if (responseCode < ERROR_STATUS ) {
49
53
return null ;
@@ -52,17 +56,49 @@ public static ArangoDBException translateError(final InternalSerde util, final I
52
56
return new ArangoDBRedirectException (String .format ("Response Code: %s" , responseCode ),
53
57
response .getMeta (HEADER_ENDPOINT ));
54
58
}
55
- if (response .getBody () != null ) {
56
- final ErrorEntity errorEntity = util .deserialize (response .getBody (), ErrorEntity .class );
57
- if (errorEntity .getCode () == ERROR_INTERNAL && errorEntity .getErrorNum () == ERROR_INTERNAL ) {
58
- return ArangoDBUnavailableException .from (errorEntity );
59
- }
60
- ArangoDBException e = new ArangoDBException (errorEntity );
61
- if (ArangoErrors .QUEUE_TIME_VIOLATED .equals (e .getErrorNum ())) {
62
- return ArangoDBException .of (new TimeoutException ().initCause (e ));
63
- }
64
- return e ;
59
+
60
+ byte [] body = response .getBody ();
61
+ if (body == null ) {
62
+ return new ArangoDBException (String .format ("Response Code: %s" , responseCode ), responseCode );
63
+ }
64
+
65
+ if (isTextPlain (response )) {
66
+ String payload = new String (body , getContentTypeCharset (response ));
67
+ return new ArangoDBException ("Response Code: " + responseCode + "[" + payload + "]" , responseCode );
68
+ }
69
+
70
+ ErrorEntity errorEntity ;
71
+ try {
72
+ errorEntity = serde .deserialize (body , ErrorEntity .class );
73
+ } catch (Exception e ) {
74
+ ArangoDBException adbEx = new ArangoDBException ("Response Code: " + responseCode
75
+ + "[Unparsable data] Response: " + response , responseCode );
76
+ adbEx .addSuppressed (e );
77
+ return adbEx ;
78
+ }
79
+
80
+ if (errorEntity .getCode () == ERROR_INTERNAL && errorEntity .getErrorNum () == ERROR_INTERNAL ) {
81
+ return ArangoDBUnavailableException .from (errorEntity );
65
82
}
66
- return new ArangoDBException (String .format ("Response Code: %s" , responseCode ), responseCode );
83
+ ArangoDBException e = new ArangoDBException (errorEntity );
84
+ if (ArangoErrors .QUEUE_TIME_VIOLATED .equals (e .getErrorNum ())) {
85
+ return ArangoDBException .of (new TimeoutException ().initCause (e ));
86
+ }
87
+ return e ;
88
+ }
89
+
90
+ private static boolean isTextPlain (InternalResponse response ) {
91
+ String contentType = response .getMeta (CONTENT_TYPE );
92
+ return contentType != null && contentType .startsWith (TEXT_PLAIN );
67
93
}
94
+
95
+ private static Charset getContentTypeCharset (InternalResponse response ) {
96
+ String contentType = response .getMeta (CONTENT_TYPE );
97
+ int paramIdx = contentType .indexOf ("charset=" );
98
+ if (paramIdx == -1 ) {
99
+ return StandardCharsets .UTF_8 ;
100
+ }
101
+ return Charset .forName (contentType .substring (paramIdx + 8 ));
102
+ }
103
+
68
104
}
0 commit comments