3

I am trying to create API calls to read file from azure data lake store. However I am unable to show proper response and error for specific file found and not found respectively.

I am able to connect with azure data lake and fetch data as well, used try-catch properly. Please help me with proper response body and response codes for c# APIs.

try
        {
            string aa = GetItemInfo("/myfolder/subfolder/testfile.txt");
            return new string[]
            {
            "Hello",
            aa,
            "World"

        };
            }
catch {
            return new string[]
         {
            "Hello",
            "World"

     };

My code is working. As I am new with API calls on C# I am unable to figure out to correct method to do so.

Shubham
  • 35
  • 1
  • 5

2 Answers2

3

You have to create the following

var responseMessage = new HttpResponseMessage>(errors, HttpStatusCode.BadRequest);

throw new HttpResponseException(responseMessage);

you can find the answer here and also here

Henry
  • 311
  • 2
  • 6
2

There are many reasons which will cause an exception. For example, network issue or token is expired. To show proper response and error for specific file found and not found, I suggest you check the path is exist or not before read the item information.

public static  bool ItemExist(string path)
{
    return _adlsFileSystemClient.FileSystem.PathExists(accountName, path);
}

Code which use ItemExist method to check whether the path is exist.

string path = "/myfolder/subfolder/testfile.txt";
if (ItemExist(path))
{
    string aa = GetItemInfo(path);
    return new string[]
    {
        "Hello",
        aa,
        "World"
    };
}
else
{
    var responseMessage = new HttpResponseMessage(HttpStatusCode.NotFound);
    responseMessage.Content = new StringContent("The file path which you requested is not found");
    throw new HttpResponseException(responseMessage);
}

For other unexpected exceptions, I suggest you use Global Error Handling in your ASP.NET Web API.

Amor
  • 8,325
  • 2
  • 19
  • 21
  • Thankyou for the bool function, that worked. Also, please help me out how to copy a file from one folder to another using c# on azure – Shubham Jun 28 '17 at 00:46
  • Please reference this thread [how to Copy files inside Azure Data lake store using C#](https://stackoverflow.com/questions/42973705/how-to-copy-files-inside-azure-data-lake-store-using-c-sharp) – Amor Jun 28 '17 at 01:20
  • Thankyou for the link, but that one is for moving data. I just want to copy same data from one folder to another. – Shubham Jun 28 '17 at 01:33
  • 1
    I am sorry to tell that copy method is not implemented in C# SDK currently. I suggest you do the copy action by downloading file and uploading the file. – Amor Jun 28 '17 at 01:37
  • Oh okay. Thank-you for the help over this. I will do download and upload then. – Shubham Jun 28 '17 at 04:46