|
1 | 1 | package com.dropbox.core; |
2 | 2 |
|
| 3 | +import com.fasterxml.jackson.core.JsonProcessingException; |
3 | 4 | import java.io.IOException; |
4 | 5 | import java.io.UnsupportedEncodingException; |
5 | 6 | import java.net.URI; |
|
17 | 18 | import com.dropbox.core.json.JsonReader; |
18 | 19 | import com.dropbox.core.util.IOUtil; |
19 | 20 | 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; |
20 | 24 | import com.dropbox.core.v2.callbacks.DbxGlobalCallbackFactory; |
21 | 25 | import com.dropbox.core.v2.callbacks.DbxNetworkErrorCallback; |
22 | 26 |
|
@@ -278,19 +282,50 @@ public static String parseErrorBody(String requestId, int statusCode, byte[] bod |
278 | 282 |
|
279 | 283 | public static DbxException unexpectedStatus(HttpRequestor.Response response) |
280 | 284 | throws NetworkIOException, BadResponseException { |
281 | | - DbxException networkError; |
282 | 285 |
|
283 | 286 | String requestId = getRequestId(response); |
284 | | - byte[] body = loadErrorBody(response); |
285 | | - String message = parseErrorBody(requestId, response.getStatusCode(), body); |
| 287 | + String message = null; |
| 288 | + DbxException networkError; |
286 | 289 |
|
287 | 290 | switch (response.getStatusCode()) { |
288 | 291 | case 400: |
| 292 | + message = DbxRequestUtil.messageFromResponse(response, requestId); |
289 | 293 | networkError = new BadRequestException(requestId, message); |
290 | 294 | break; |
291 | 295 | case 401: |
| 296 | + message = DbxRequestUtil.messageFromResponse(response, requestId); |
292 | 297 | networkError = new InvalidAccessTokenException(requestId, message); |
293 | 298 | 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; |
294 | 329 | case 429: |
295 | 330 | try { |
296 | 331 | int backoffSecs = Integer.parseInt(getFirstHeader(response, "Retry-After")); |
@@ -336,6 +371,12 @@ public static DbxException unexpectedStatus(HttpRequestor.Response response) |
336 | 371 | return networkError; |
337 | 372 | } |
338 | 373 |
|
| 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 | + |
339 | 380 | public static <T> T readJsonFromResponse(JsonReader<T> reader, HttpRequestor.Response response) |
340 | 381 | throws BadResponseException, NetworkIOException { |
341 | 382 | try { |
|