Search code examples
c#asp.net-mvctwiliotwilio-api

How to handle Rest Exceptions in the new Twilio libraries?


So after some much needed updates to other areas of our software, we're updating our Twilio libraries to the latest version.

I'm looking through the API documentation, and I say this being a huge fan of how easy it was to get started originally with this API, but I'm finding the API reference a little lacking.

I wanted to better understand how the new 'CallResource' class works, but I can't find anything on it. Specifically, how it handles exceptions, if at all.

This is how it was handled in the old code:

            if (call.RestException == null)
            {
                Debug.WriteLine(call.Sid.ToString()
                                + " " + call.StartTime.ToString()
                                + " " + call.Status.ToString());
                so.twillio_sid = call.Sid;
                so.status = call.Status;
                db.SaveChanges();
            }
            else
            {
                so.offer_status = ShiftOfferStatus.Failed;
                so.status = call.RestException.Message.ToString();
                callout.status = CalloutStatus.inprogressWaitingNext;
                db.SaveChanges();
                Debug.WriteLine(call.RestException.Message.ToString());
            }

How should I go about checking if the call was initiated successfully in the new interfaces?


Solution

  • So, turns out the new approach is:

    try
    {
        //make a call
    } 
    catch(ApiException e)
    {
        //handle exception state here
    }
    

    This document ended up being where the answer lies. Unfortunately the new code samples don't contain any error handling.

    https://www.twilio.com/docs/libraries/csharp/migrating-your-csharp-dot-net-application-twilio-sdk-4x-5x