locked
MobileServiceClient InvokeApiAsync error handling RRS feed

  • Question

  • Hi,

    I'm using InokeApiAsync to access custom API to get a set of records from my backend. This works well and returns the list in JSON format.

    However, I have been testing my code for how it handles error cases. When the server reports an error, e.g. not found, the method returns html/text which the JSON parser trips over. As part of debugging, I added conditional handling in my HttpHandler to convert these html/text responses to JSON. This allowed the JSON parser to work.

    Now when I call InvokeApiAsync to a non-existent endpoint, I get

    Microsoft.WindowsAzure.MobileServices.MobileServiceInvalidOperationException: 'The request could not be completed.  (Not Found)'


    six times (in the HttpHandler) prior to the exception being caught in my surrounding try-catch.

    Does InvokeApiAsync do automatic retry? I'm confused why I see 6 exceptions before being caught in my try-catch. What is the best way to handle errors with InvokeApiAsync (e.g. not found, timeout, unauthorised)?

    Thanks,

    Tom.

    Here is how I am making the call

    try
    {
        var items = await (Table.MobileServiceClient.InvokeApiAsync("{xxx/non-existing-endpoint}", HttpMethod.Get, parameters));                
    }
    catch (MobileServiceInvalidOperationException e)
    {
        Debug.Print("catch");
    }



    • Edited by TomEverin Monday, January 13, 2020 8:38 AM added code sample
    Monday, January 13, 2020 8:34 AM

Answers

  • Hi Tom,

    Luckily, the Mobile Services SDK is on github. Having looked, InvokeApiAsync calls InternalInvokeApiAsync which calls RequestAsync on MobileServicesHttpClient (basically an HttpClient wrapper). RequestAsync eventually calls SendRequestAsync.

    In looking at that method, InovkeApiAsync does not do any automatic retries that I can see so I'm not entirely certain why you're getting that exception six times. As to the best way to handle exceptions, ThrowInvalidResponse ensures that MobileServiceInvalidOperationException is the exception thrown should there be a non success status code or invalid content that doesn't correspond to your Json object.


    Thanks in advance, Ryan

    Tuesday, January 14, 2020 3:03 PM

All replies

  • Hi Tom,

    Luckily, the Mobile Services SDK is on github. Having looked, InvokeApiAsync calls InternalInvokeApiAsync which calls RequestAsync on MobileServicesHttpClient (basically an HttpClient wrapper). RequestAsync eventually calls SendRequestAsync.

    In looking at that method, InovkeApiAsync does not do any automatic retries that I can see so I'm not entirely certain why you're getting that exception six times. As to the best way to handle exceptions, ThrowInvalidResponse ensures that MobileServiceInvalidOperationException is the exception thrown should there be a non success status code or invalid content that doesn't correspond to your Json object.


    Thanks in advance, Ryan

    Tuesday, January 14, 2020 3:03 PM
  • Thanks Ryan, turned out that my VS exception settings were too broad hence breaking on more exceptions than expected.

    Sunday, January 26, 2020 9:38 PM
  • If you examine the exception, you will note that the status code is in there - it's just in a property that is not serialized. Just surround your InvokeApiAsync() call with a try/catch and test for the StatusCode. It should be a lot easier than writing your own HTTP Client code for the same purpose.

    Specifically, MobileServiceInvalidOperationException contains the HttpResponse of the failed request, so you can check exception.Response.StatusCode value.


    Monday, January 27, 2020 5:02 AM