How to handle error in Dropbox C # SDK?

How do property handling errors in Dropbox C # SDK?

I want to use a common error handling method from different API calls. This method should be used at the top level of the application and in calls to the serevals APIs. For most cloud APIs (like Microsoft OneDrive and Google Drive APIs) I can do this because there is a well-defined list (all error codes are listed) and only one exception class for handling errors. But in the Dropbox C # SDK, the opposite is true. There is no list of error codes, but there are a dozen exception classes (one exception pattern Dropbox.Api.ApiException<T>

and a lot of errors for a template parameter T

). Look at an example for the number of error classes for files to work - http://dropbox.github.io/dropbox-sdk-dotnet/html/N_Dropbox_Api_Files.htm

What the heck! How to deal with them all? Write a giant block catch()

?

And even worse, most of them use the same error types!
For example, a class Dropbox.Api.Files.LookupError

that describes errors such as "Not Found", "Invalid Path", etc. is part 21 ! other error classes. To handle a simple "Not Found" error, I have to catch two dozen exceptions! This is normal?

So how are property handling errors in the Dropbox C # SDK?

+3


source to share


1 answer


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, as well as how to check for an exception caught in its entirety:

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
    }
}

      

+3


source







All Articles