3

How property handle errors in Dropbox C# SDK?

I want to use common method for handling errors from different API calls. This method should be used on top app level and in serevals API calls. For most clouds API (like Microsoft OneDrive and Google Drive API) I can do it because there is strictly defined list (enum will all error codes) and only one exception class for error handling. But in Dropbox C# SDK everything is contrariwise! There's no any error code list but there are dozen exception class (one exception template Dropbox.Api.ApiException<T> and great amount of errors object for T template parameter). Look for example on count of error classes for files operation - http://dropbox.github.io/dropbox-sdk-dotnet/html/N_Dropbox_Api_Files.htm

What the hell! How handle all of them ? Write giant catch() block ?

And worse, most of them use the same errors types!
For example, class Dropbox.Api.Files.LookupError that describes errors like "Not found", "Malformed Path" and so on is part of 21! others errors classes. For handling the simple "Not found" error I must be able to catch two dozen exceptions! Is it normal?

So, how property handle errors in Dropbox C# SDK?

4
  • It depends if you want your application to do something different in each case. If not, you can just handle the top-level exception type, and log/report the specific error for debugging/resolution later. If you need your client app to behave differently (apart from displaying a different error) in a specific case, then yes you have to handle that case specifically. And how is this clearly documented list of possible exceptions really any different to an enum provided in other SDKs? In both cases you have a bounded list of possible errors.
    – ADyson
    Commented Apr 27, 2017 at 15:28
  • @ADyson, I wish my app has to show user friendly error message from app resource (depends on user selected localization). So I need to handle exception carefully and display correct message (not debugger message or exception stack)! How to do it? Catching ALL!? Excuse me, where is "clearly documented"? Could you give me a link?
    – 23W
    Commented Apr 27, 2017 at 16:05
  • You don't regard your link at dropbox.github.io/dropbox-sdk-dotnet/html/… as documentation of the errors then? It seems to list them all. Have you actually tried this by the way? The exceptions might give you sufficiently friendly error messages that you can report back. If not, you could prepare your own and map them to the Dropbox errors. You'll get some child of DropboxException which will contain a message. Maybe they will have a correspondence to those error properties you pointed out.
    – ADyson
    Commented Apr 27, 2017 at 22:05
  • @ADyson, Yes, but those link isn't "documentation". It's only class list brief description, besides, there are other errors classes: for sharing and users method. How exception can give me friendly error message? Is exception message are translated for all required for me languages and how can I receive message by language code: ("de-DE", "en-US", "fr-FR" and so on) ? Mapping my messages to Dropbox errors is interesting idea. Can you give me rude example?
    – 23W
    Commented Apr 28, 2017 at 6:13

1 Answer 1

5

If you want to catch any arbitrary Dropbox exception, instead of handling specific ones, you can catch the parent type DropboxException, like this:

try {
    var account = await this.client.Users.GetCurrentAccountAsync();
    // use account
} catch (DropboxException ex) {
    // inspect and handle ex as desired
}

try {
    var list = await client.Files.ListFolderAsync(string.Empty);
    // use list
} catch (DropboxException ex) {
    // inspect and handle ex as desired
}


try {
    var download = await client.Files.DownloadAsync(path);
    // use download
} catch (DropboxException ex) {
    // inspect and handle ex as desired
}

Here's a more complete example showing how to catch a specific exception, and also how to inspect an exception caught generally:

try {
    var list = await client.Files.ListFolderAsync(string.Empty);
    // use list
} catch (ApiException<Dropbox.Api.Files.ListFolderError> ex) {
    // handle ListFolder-specific error
} catch (DropboxException ex) {
    // inspect and handle ex as desired
    if (ex is AuthException) {
        // handle AuthException, which can happen on any call
        if (((AuthException)ex).ErrorResponse.IsInvalidAccessToken) {
            // handle invalid access token case
        }
    } else if (ex is HttpException) {
        // handle HttpException, which can happen on any call
    }
}
5
  • Thank you. Can you give me few details? Example, I have method that performs folder scan (like ls or dir command line). This method calls Dropbox.Api.Files.ListFolder???Async, ..Files.GetMetadataAsync, ..Sharing.ListFolder???Async, ...Sharing.ListFile???Async, ...Users.GetAccount???Async. And now exception is appearing. How to define its reason from DropboxException? Or should I consistently cast it to ApiException<T> where T is one of ...Files.GetMetadataError, ...Files.ListFolderError, ...Files.ListFolderContinueError, ...Users.GetAccountError, and so on.
    – 23W
    Commented Apr 28, 2017 at 6:30
  • 1
    If you look at the method definition, e.g. dropbox.github.io/dropbox-sdk-dotnet/html/… it shows you what exceptions can be thrown by that method. In this example it says "ApiException<TError> Thrown if there is an error processing the request; This will contain a ListFolderError." So for each error trap you only need to consider the errors it's actually possible to throw from the method you're calling.
    – ADyson
    Commented Apr 28, 2017 at 8:40
  • @ADyson yep, and it's a big list of exceptions that i should to catch. Because i use many functions :) One exception with enum would be better, I think.
    – 23W
    Commented Apr 28, 2017 at 10:42
  • but for each method, the list is not so big. And you could write little subroutines which deal with each exception type, and then re-use them if the same exception can be thrown by different methods. Yes, there's work to do, but then if there was an enum, you'd still need a massive switch statement or something to decide what to do in each case. Of course, if you don't like the SDK provided, you can always write your own against the HTTP API :-)
    – ADyson
    Commented Apr 28, 2017 at 10:51
  • Handling specific errors (e.g., specific to any particular call) is probably a better practice, but the specificity of the error handling to have in your app is up to you. I updated my answer to show more examples of different amounts of error handling.
    – Greg
    Commented Apr 28, 2017 at 17:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.