cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
Want to learn some quick and useful tips to make your day easier? Check out how Calvin uses Replay to get feedback from other teams at Dropbox here.

Discuss Dropbox Developer & API

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Re: DropBox API Async Call, "DropBoxClient.Files.DownloadAsync" will not throw an exceptio

DropBox API Async Call, "DropBoxClient.Files.DownloadAsync" will not throw an exception (solution)

snaidamast
Explorer | Level 3

Issue:

I have been using DropBox for the storage of my new document management application's installation file as well as for updated, individual files that can be downloaded at a later date (after the initial installation).

 

Upon testing the following async call, DropBoxClient.Files.DownloadAsync, I saw that if a file has not been uploaded to my DropBox folder, where all of these application files are to be stored, this call will throw a "path not found" exception (which makes sense).

 

However, instead of having this error caught by my code's try-catch construct, the application being tested simply ends. 

 

According to replies from DropBox developers in and around 2017, this was a known issue with their API SDK, and consequently would be fixed.  However, my own testing for the past day or so appears to have shown that this issue still exists, even with latest version of the DropBox SDK\API, which I implemented using the NuGet Package Manager.

 

Solution:

I decided to test another async call to see if this situation is common to all of the DropBox API async calls or was merely an issue with the aforementioned async call.

 

As a result, I tested the following call prior to DownloadAsync call to see if a "path not found" exception would be caught as hoped.

 

Using the following call, DropBoxClient.Files.GetMetadataAsync, which merely retrieves the meta-data for the specified file, if the file does not exist in my DropBox folder, it will in fact throw the "path not found" exception and the exception will be caught properly with a try-catch construct...

 

As a result, if you are experiencing a similar issue with your code, simply use the DropBoxClient.Files.GetMetadataAsync call prior to the actual download call to test for your file's existence, while having the ability to trap for an error exception if one arises.

 

7 Replies 7

Greg-DB
Dropbox Staff

While you can certainly call GetMetadataAsync first, you should be able to directly catch an exception from DownloadAsync in the Dropbox .NET SDK using a catch block. 

 

I just gave this a try myself using the latest version of the Dropbox .NET SDK, currently v5.0.0, and it is working for me. I am able to catch the exception from DownloadAsync and see the 'path/not_found' error, without my app's execution ending. 

 

Can you let us know more about the issue you're seeing so we can look into it? For instance, please share:

  • the name and version number of your environment, e.g., what Visual Studio you're using
  • the steps to reproduce the issue, such as the relevant code snippet(s)
  • the full error/output you get

Thanks!

snaidamast
Explorer | Level 3

Hi Greg:

 

I am also using the 5.0.0 version of the DropBox API.

 

And yet with the DownloadAsync call, if a "path not found" error is thrown, the entire application abrubtly ends.  This is not occurring with the GetMetadataAsync call.

 

I am developing in VB.NET with Visual Studio 2019 under .NET Framework 4.6.

 

There is nothing I can show you since there are no error messages that are being displayed in any way.  I just try to manual trap such a situation when I am testing the applictaion...

Greg-DB
Dropbox Staff

Can you also share the relevant code showing how you're making the call and attempting to catch the exception so we can try to reproduce that the same way here? Thanks in advance! 

snaidamast
Explorer | Level 3

Here is my code...

 

        Private Async Sub Dropbox_DownloadApplicationFiles(ByVal poDropboxMethodDataStructure  As DropboxMethodDataStructure)

            Dim liResult                      As Integer = 0
            Dim lsApplicationDirectory        As String = My.Application.Info.DirectoryPath + "\"

            Dim loFileByteArray               As Object

            Dim loDropboxMethodDataStructure  As New DropboxMethodDataStructure()

            Dim loDropBoxClient               As DropboxClient
            Dim loDropBoxClientResponse       As Object


            Try

                loDropboxMethodDataStructure.RESULT = 0

                loDropBoxClient = New DropboxClient(csDropBoxUserAccessToken)

                For Each lsFileName As String In poDropboxMethodDataStructure.FILE_NAMES_ARRAYLIST
                    ' test for existence of dropbox file  (if file not found (path not found error), this async call will throw an exception
                    loDropBoxClientResponse = Await loDropBoxClient.Files.GetMetadataAsync("/CDOCS" + "/" + lsFileName)

                    ' download dropbox file (this async call will NOT throw an exception
                    loDropBoxClientResponse = Await loDropBoxClient.Files.DownloadAsync("/CDOCS" + "/" + lsFileName)

                    ' write dropbox file to application directory
                    loFileByteArray = Await loDropBoxClientResponse.GetContentAsByteArrayAsync()
                    System.IO.File.WriteAllBytes(lsApplicationDirectory + "\" + lsFileName, loFileByteArray)

                    ' TESTING ONLY !!!
                    If (lsFileName.Equals("CDOCS.exe")) Then
                        Exit For
                    End If
                Next
            Catch loDropboxException As DropboxException
                loDropboxMethodDataStructure.RESULT = -2
                loDropboxMethodDataStructure.METHOD_MESSAGE = "APPLICATION UPDATE ERROR!!!  (Method: Dropbox_DownloadApplicationFiles): " + loDropboxException.Message + Environment.NewLine + Environment.NewLine + "Please contact Black Falcon Software..."
            Catch loException As Exception
                loDropboxMethodDataStructure.RESULT = -1
                loDropboxMethodDataStructure.METHOD_MESSAGE = "APPLICATION UPDATE ERROR!!!  (Method: Dropbox_DownloadApplicationFiles): " + loException.Message + Environment.NewLine + Environment.NewLine + "Please contact Black Falcon Software..."
            End Try

            liResult = loDropboxMethodDataStructure.RESULT

            If (liResult < 0) Then
                Console.WriteLine("")
                Console.WriteLine("")
                Console.WriteLine("")
                Console.WriteLine("--------------")
                Console.WriteLine("-- ERROR!!! --")
                Console.WriteLine("--------------")
                Console.WriteLine(loDropboxMethodDataStructure.METHOD_MESSAGE)
                Console.WriteLine("--------------")
                Console.WriteLine("")
                Console.WriteLine("")
                Console.WriteLine("Press any key to end application...")

                Console.ReadLine()
                End
            End If


            ' ---
            ' end of async processes -> display success message
            ' ---
            Console.WriteLine("CDOCS Application update completed successfully... Please press any key to launch the CDOCS application...")
            Console.ReadLine()
        End Sub

Greg-DB
Dropbox Staff

Thanks! We'll work on reproducing the issue using this.

Greg-DB
Dropbox Staff

I just tried running a stripped down version of this code in the same environment (using v5.0.0 of the Dropbox .NET SDK, in a project using VB.NET and .NET Framework 4.6.1 in Visual Studio 2019) and it works correctly for me. I'm able to trigger and catch an exception from DownloadAsync for a non-existant path.

 

Here's the simplified code:

 

    Private Async Sub Test()

        Console.WriteLine("Running Test...")

        Dim loDropBoxClient               As DropboxClient
        Dim loDropBoxClientResponse       As Object

        Dim csDropBoxUserAccessToken = "<ACCESS_TOKEN>"

        Try

            loDropBoxClient = New DropboxClient(csDropBoxUserAccessToken)

            loDropBoxClientResponse = Await loDropBoxClient.Files.DownloadAsync("/nonexistantpath")
            Console.WriteLine("Got response:")
            Console.WriteLine(loDropBoxClientResponse)

        Catch loDropboxException As DropboxException
            Console.WriteLine("Got DropboxException:")
            Console.WriteLine(loDropboxException)
        Catch loException As Exception
            Console.WriteLine("Got Exception:")
            Console.WriteLine(loException)

        End Try

        Console.WriteLine("Test complete.")
        Console.ReadLine()

    End Sub

Could you give that a try and let me know the issue does or doesn't reproduce with that for you?

 

snaidamast
Explorer | Level 3

Hi Greg...

 

I just tried my code again as you suggested and it is now trapping the error as one would expect.

 

However, for some reason, when I submitted this issue, the code in question was in fact not working and causing my application simply to terminate.  Why, I do not know unless some type of buffer issue  was cuasing the problem.

 

Steve Naidamast

Sr. Software Engineer

Need more support?