Skip to content

Commit 6d67057

Browse files
author
Stephen Cobbe
committed
Added global response handlers.
1 parent 877ed68 commit 6d67057

File tree

15 files changed

+264
-14
lines changed

15 files changed

+264
-14
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/build/
2+
/target/
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Global Handlers Example
2+
3+
This repo contains an advanced use-case example (global route error and network error response callbacks).
4+
5+
Client can supply a callback factory, which allows for consistent, global handling of route-specific error types, as well as general network errors.
6+
7+
Normally, error handling is done on a request-by-request basis. However, it is convenient to handle some error behavior consistently, regardless of the request or the endpoint.
8+
9+
For implementing global error handling for general networking errors like an auth error, `getNetworkErrorCallback` should be implemented. For implementing global error handling for route-specific errors, one of the `getRouteErrorCallback` should be implemented.
10+
11+
To run this example, please replace `"<ACCESS TOKEN>"` in `Main.java` with your access token. See the online tutorial for more details on how to generate an access token.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+
description = 'Global Callbacks Example (Dropbox Core API SDK)'
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.dropbox.core.examples.global_callbacks;
2+
3+
import com.dropbox.core.v2.callbacks.DbxGlobalCallbackFactory;
4+
import com.dropbox.core.v2.callbacks.DbxRouteErrorCallback;
5+
import com.dropbox.core.v2.callbacks.DbxNetworkErrorCallback;
6+
import com.dropbox.core.v2.files.ListFolderError;
7+
import com.dropbox.core.v2.files.LookupError;
8+
9+
public class DbxExampleGlobalCallbackFactory implements DbxGlobalCallbackFactory {
10+
@Override
11+
public <T> DbxRouteErrorCallback<T> createRouteErrorCallback(T routeError) {
12+
if (routeError instanceof ListFolderError) {
13+
return new DbxExampleListFolderErrorCallback<T>();
14+
} else if (routeError instanceof LookupError) {
15+
return new DbxExampleLookupErrorCallback<T>();
16+
}
17+
18+
return null;
19+
}
20+
21+
@Override
22+
public DbxExampleNetworkErrorCallback createNetworkErrorCallback() {
23+
return new DbxExampleNetworkErrorCallback();
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.dropbox.core.examples.global_callbacks;
2+
3+
import com.dropbox.core.v2.callbacks.DbxRouteErrorCallback;
4+
import com.dropbox.core.v2.files.ListFolderError;
5+
6+
public class DbxExampleListFolderErrorCallback<T> extends DbxRouteErrorCallback<T> {
7+
@Override
8+
public void run() {
9+
System.out.println("GLOBAL ROUTE ERROR HANDLER (ListFolderError): " + this.getRouteError() + "\n");
10+
// do some work
11+
}
12+
}
13+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.dropbox.core.examples.global_callbacks;
2+
3+
import com.dropbox.core.v2.callbacks.DbxRouteErrorCallback;
4+
import com.dropbox.core.v2.files.LookupError;
5+
6+
public class DbxExampleLookupErrorCallback<T> extends DbxRouteErrorCallback<T> {
7+
@Override
8+
public void run() {
9+
System.out.println("GLOBAL ROUTE ERROR HANDLER (LookupError): " + this.getRouteError() + "\n");
10+
// do some work
11+
}
12+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.dropbox.core.examples.global_callbacks;
2+
3+
import com.dropbox.core.v2.callbacks.DbxNetworkErrorCallback;
4+
import com.dropbox.core.InvalidAccessTokenException;
5+
6+
public class DbxExampleNetworkErrorCallback extends DbxNetworkErrorCallback {
7+
@Override
8+
public void run() {
9+
if (this.getNetworkError() instanceof InvalidAccessTokenException) {
10+
System.out.println("GLOBAL NETWORK ERROR HANDLER: " + this.getNetworkError() + "\n");
11+
// do some work
12+
}
13+
}
14+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.dropbox.core.examples.global_callbacks;
2+
3+
import com.dropbox.core.DbxException;
4+
import com.dropbox.core.DbxRequestConfig;
5+
import com.dropbox.core.DbxRequestUtil;
6+
import com.dropbox.core.v2.callbacks.DbxGlobalCallbackFactory;
7+
import com.dropbox.core.v2.callbacks.DbxRouteErrorCallback;
8+
import com.dropbox.core.v2.callbacks.DbxNetworkErrorCallback;
9+
import com.dropbox.core.v2.DbxClientV2;
10+
import com.dropbox.core.v2.files.FileMetadata;
11+
import com.dropbox.core.v2.files.ListFolderError;
12+
import com.dropbox.core.v2.files.ListFolderErrorException;
13+
import com.dropbox.core.v2.files.ListFolderResult;
14+
import com.dropbox.core.v2.files.Metadata;
15+
import com.dropbox.core.v2.users.FullAccount;
16+
17+
import java.util.List;
18+
19+
import java.io.FileInputStream;
20+
import java.io.InputStream;
21+
import java.io.IOException;
22+
23+
public class Main {
24+
private static final String ACCESS_TOKEN = "<ACCESS TOKEN>";
25+
26+
public static void main(String args[]) throws DbxException, IOException {
27+
// Create Dropbox client
28+
DbxRequestConfig config = new DbxRequestConfig("dropbox/java-tutorial");
29+
DbxClientV2 client = new DbxClientV2(config, ACCESS_TOKEN);
30+
31+
// Instantiate factory and set shared factory
32+
DbxRequestUtil.sharedCallbackFactory = new DbxExampleGlobalCallbackFactory();
33+
34+
try {
35+
// Get files and folder metadata from Dropbox root directory
36+
client.files().listFolder("/does/not/exist");
37+
}
38+
catch (ListFolderErrorException ex) {
39+
System.err.println("STANDARD ROUTE ERROR HANDLER: " + ex.errorValue + "\n");
40+
}
41+
catch (DbxException ex) {
42+
System.err.println("STANDARD NETWORK ERROR HANDLER: " + ex + "\n");
43+
}
44+
45+
// try {
46+
// // Get files and folder metadata from Dropbox root directory
47+
// client.auth().tokenRevoke();
48+
// client.files().listFolder("/does/not/exist");
49+
// }
50+
// catch (ListFolderErrorException ex) {
51+
// System.err.println("STANDARD ROUTE ERROR HANDLER2: " + ex.errorValue + "\n");
52+
// }
53+
// catch (DbxException ex) {
54+
// System.err.println("STANDARD NETWORK ERROR HANDLER2: " + ex + "\n");
55+
// }
56+
}
57+
}

examples/settings.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
rootProject.name = 'examples'
22
include ':account-info'
33
include ':authorize'
4+
include ':global-callbacks'
45
include ':longpoll'
56
include ':tutorial'
67
include ':upgrade-oauth1-token'

examples/tutorial/src/main/java/com/dropbox/core/examples/tutorial/Main.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
import com.dropbox.core.DbxException;
44
import com.dropbox.core.DbxRequestConfig;
5+
import com.dropbox.core.v2.callbacks.DbxGlobalCallbackFactory;
6+
import com.dropbox.core.v2.callbacks.DbxRouteErrorCallback;
7+
import com.dropbox.core.v2.callbacks.DbxNetworkErrorCallback;
58
import com.dropbox.core.v2.DbxClientV2;
69
import com.dropbox.core.v2.files.FileMetadata;
710
import com.dropbox.core.v2.files.ListFolderResult;

0 commit comments

Comments
 (0)