|
21 | 21 | package com.arangodb.async.internal.velocystream; |
22 | 22 |
|
23 | 23 | import com.arangodb.ArangoDBException; |
24 | | -import com.arangodb.entity.ErrorEntity; |
| 24 | +import com.arangodb.internal.net.ArangoDBRedirectException; |
| 25 | +import com.arangodb.internal.net.HostDescription; |
| 26 | +import com.arangodb.internal.net.HostHandle; |
25 | 27 | import com.arangodb.internal.net.HostHandler; |
| 28 | +import com.arangodb.internal.util.HostUtils; |
26 | 29 | import com.arangodb.internal.velocystream.VstCommunication; |
27 | 30 | import com.arangodb.internal.velocystream.internal.AuthenticationRequest; |
28 | 31 | import com.arangodb.internal.velocystream.internal.Message; |
@@ -58,23 +61,37 @@ protected CompletableFuture<Response> execute(final Request request, final VstCo |
58 | 61 | final Message message = createMessage(request); |
59 | 62 | send(message, connection).whenComplete((m, ex) -> { |
60 | 63 | if (m != null) { |
| 64 | + final Response response; |
61 | 65 | try { |
62 | | - final Response response = createResponse(m); |
63 | | - if (response.getResponseCode() >= 300) { |
64 | | - if (response.getBody() != null) { |
65 | | - final ErrorEntity errorEntity = util.deserialize(response.getBody(), ErrorEntity.class); |
66 | | - rfuture.completeExceptionally(new ArangoDBException(errorEntity)); |
67 | | - } else { |
68 | | - rfuture.completeExceptionally(new ArangoDBException( |
69 | | - String.format("Response Code: %s", response.getResponseCode()), response.getResponseCode())); |
70 | | - } |
71 | | - } else { |
72 | | - rfuture.complete(response); |
73 | | - } |
| 66 | + response = createResponse(m); |
74 | 67 | } catch (final VPackParserException e) { |
75 | 68 | LOGGER.error(e.getMessage(), e); |
76 | 69 | rfuture.completeExceptionally(e); |
| 70 | + return; |
77 | 71 | } |
| 72 | + |
| 73 | + try { |
| 74 | + checkError(response); |
| 75 | + } catch (final ArangoDBRedirectException e) { |
| 76 | + final String location = e.getLocation(); |
| 77 | + final HostDescription redirectHost = HostUtils.createFromLocation(location); |
| 78 | + hostHandler.closeCurrentOnError(); |
| 79 | + hostHandler.fail(); |
| 80 | + execute(request, new HostHandle().setHost(redirectHost)) |
| 81 | + .whenComplete((v, err) -> { |
| 82 | + if (v != null) { |
| 83 | + rfuture.complete(v); |
| 84 | + } else if (err != null) { |
| 85 | + rfuture.completeExceptionally(err); |
| 86 | + } else { |
| 87 | + rfuture.cancel(true); |
| 88 | + } |
| 89 | + }); |
| 90 | + return; |
| 91 | + } catch (ArangoDBException e) { |
| 92 | + rfuture.completeExceptionally(e); |
| 93 | + } |
| 94 | + rfuture.complete(response); |
78 | 95 | } else if (ex != null) { |
79 | 96 | LOGGER.error(ex.getMessage(), ex); |
80 | 97 | rfuture.completeExceptionally(ex); |
|
0 commit comments