Skip to content

Commit 1fc5b87

Browse files
author
Stephen Cobbe
committed
Added support for 403 access error and 422 path root error.
1 parent e5bc37e commit 1fc5b87

File tree

6 files changed

+94
-6
lines changed

6 files changed

+94
-6
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.dropbox.core;
2+
3+
import com.dropbox.core.v2.auth.AccessError;
4+
5+
/**
6+
* Gets thrown when invalid access occurs.
7+
*/
8+
public class AccessErrorException extends DbxException {
9+
private static final long serialVersionUID = 0;
10+
11+
private final AccessError accessError;
12+
13+
public AccessError getAccessError() {
14+
return accessError;
15+
}
16+
17+
public AccessErrorException(String requestId, String message, AccessError accessError) {
18+
super(requestId, message);
19+
this.accessError = accessError;
20+
}
21+
}

src/main/java/com/dropbox/core/DbxRequestUtil.java

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.dropbox.core;
22

3+
import com.fasterxml.jackson.core.JsonProcessingException;
34
import java.io.IOException;
45
import java.io.UnsupportedEncodingException;
56
import java.net.URI;
@@ -17,6 +18,9 @@
1718
import com.dropbox.core.json.JsonReader;
1819
import com.dropbox.core.util.IOUtil;
1920
import com.dropbox.core.util.StringUtil;
21+
import com.dropbox.core.v2.auth.AccessError;
22+
import com.dropbox.core.v2.common.PathRootError;
23+
import com.dropbox.core.v2.common.PathRootError.Serializer;
2024
import com.dropbox.core.v2.callbacks.DbxGlobalCallbackFactory;
2125
import com.dropbox.core.v2.callbacks.DbxNetworkErrorCallback;
2226

@@ -278,19 +282,50 @@ public static String parseErrorBody(String requestId, int statusCode, byte[] bod
278282

279283
public static DbxException unexpectedStatus(HttpRequestor.Response response)
280284
throws NetworkIOException, BadResponseException {
281-
DbxException networkError;
282285

283286
String requestId = getRequestId(response);
284-
byte[] body = loadErrorBody(response);
285-
String message = parseErrorBody(requestId, response.getStatusCode(), body);
287+
String message = null;
288+
DbxException networkError;
286289

287290
switch (response.getStatusCode()) {
288291
case 400:
292+
message = DbxRequestUtil.messageFromResponse(response, requestId);
289293
networkError = new BadRequestException(requestId, message);
290294
break;
291295
case 401:
296+
message = DbxRequestUtil.messageFromResponse(response, requestId);
292297
networkError = new InvalidAccessTokenException(requestId, message);
293298
break;
299+
case 403:
300+
try {
301+
ApiErrorResponse<AccessError> accessErrorResponse = new ApiErrorResponse.Serializer<AccessError>(AccessError.Serializer.INSTANCE)
302+
.deserialize(response.getBody());
303+
if (accessErrorResponse.getUserMessage() != null) {
304+
message = accessErrorResponse.getUserMessage().toString();
305+
}
306+
AccessError accessError = accessErrorResponse.getError();
307+
networkError = new AccessErrorException(requestId, message, accessError);
308+
} catch (JsonProcessingException ex) {
309+
throw new BadResponseException(requestId, "Bad JSON: " + ex.getMessage(), ex);
310+
} catch (IOException ex) {
311+
throw new NetworkIOException(ex);
312+
}
313+
break;
314+
case 422:
315+
try {
316+
ApiErrorResponse<PathRootError> pathRootErrorResponse = new ApiErrorResponse.Serializer<PathRootError>(Serializer.INSTANCE)
317+
.deserialize(response.getBody());
318+
if (pathRootErrorResponse.getUserMessage() != null) {
319+
message = pathRootErrorResponse.getUserMessage().toString();
320+
}
321+
PathRootError pathRootError = pathRootErrorResponse.getError();
322+
networkError = new PathRootErrorException(requestId, message, pathRootError);
323+
} catch (JsonProcessingException ex) {
324+
throw new BadResponseException(requestId, "Bad JSON: " + ex.getMessage(), ex);
325+
} catch (IOException ex) {
326+
throw new NetworkIOException(ex);
327+
}
328+
break;
294329
case 429:
295330
try {
296331
int backoffSecs = Integer.parseInt(getFirstHeader(response, "Retry-After"));
@@ -336,6 +371,12 @@ public static DbxException unexpectedStatus(HttpRequestor.Response response)
336371
return networkError;
337372
}
338373

374+
private static String messageFromResponse(HttpRequestor.Response response, String requestId) throws NetworkIOException, BadResponseException {
375+
byte[] body = loadErrorBody(response);
376+
String message = parseErrorBody(requestId, response.getStatusCode(), body);
377+
return message;
378+
}
379+
339380
public static <T> T readJsonFromResponse(JsonReader<T> reader, HttpRequestor.Response response)
340381
throws BadResponseException, NetworkIOException {
341382
try {
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.dropbox.core;
2+
3+
import com.dropbox.core.v2.common.PathRootError;
4+
5+
/**
6+
* Gets thrown when an invalid path rood is supplied.
7+
*/
8+
public class PathRootErrorException extends DbxException {
9+
private static final long serialVersionUID = 0;
10+
11+
private final PathRootError pathRootError;
12+
13+
public PathRootError getPathRootError() {
14+
return pathRootError;
15+
}
16+
17+
public PathRootErrorException(String requestId, String message, PathRootError pathRootError) {
18+
super(requestId, message);
19+
this.pathRootError = pathRootError;
20+
}
21+
}

stone.gradle

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class ClientSpec {
1313
String javadoc = null
1414
String routeFilter = null
1515
String requestsClassnamePrefix = null
16+
String unusedClassesToGenerate = null
1617
}
1718

1819
def runStoneGenerator(StoneConfig config,
@@ -57,7 +58,6 @@ def runStoneGenerator(StoneConfig config,
5758
return
5859
}
5960

60-
def firstClient = true;
6161
for (int i = 0; i < config.clients.size; ++i) {
6262
def client = config.clients[i]
6363
def isFirst = i == 0
@@ -97,6 +97,9 @@ def runStoneGenerator(StoneConfig config,
9797
if (client.requestsClassnamePrefix != null) {
9898
args "--requests-classname-prefix", client.requestsClassnamePrefix
9999
}
100+
if (client.unusedClassesToGenerate != null) {
101+
args "--unused-classes-to-generate", client.unusedClassesToGenerate
102+
}
100103
}
101104
}
102105
}
@@ -140,6 +143,7 @@ project.sourceSets.all { SourceSet sourceSet ->
140143

141144

142145
generateStone {
146+
String unusedClassesToGenerate = 'PathRootError, AccessError, RateLimitError'
143147
config = new StoneConfig(
144148
packageName: 'com.dropbox.core.v2',
145149
globalRouteFilter: 'alpha_group=null and beta_group=null',
@@ -150,6 +154,7 @@ generateStone {
150154
javadoc: 'Base class for user auth clients.',
151155
requestsClassnamePrefix: "DbxUser",
152156
routeFilter: 'auth="user" or auth="noauth"',
157+
unusedClassesToGenerate: unusedClassesToGenerate,
153158
),
154159
new ClientSpec(
155160
name: 'DbxTeamClientV2Base',

0 commit comments

Comments
 (0)