Ray
Ray

Reputation: 523

Twilio new API 5.x

I just want to validate if someone is being testing the new Twilio helper classes 5.x for C# and how Exception management is being managed now. The new API broke my code because of the following:

I have in my code a Send Message method like this one:

public static MessageResource SendMessage(SmsMessage smsMessage, out MessageResource msgResult)
{
    _log.DebugFormat( "[ClassName: {0}] [Executing Method: {1}]", MethodBase.GetCurrentMethod().DeclaringType.Name, MethodBase.GetCurrentMethod().Name );

    SmsConfiguration smsRestClient = new  SmsConfiguration( smsMessage.ChannelName );

    msgResult = MessageResource.Create(
                  from: new PhoneNumber(smsMessage.MessageFrom),
                    to: new PhoneNumber(smsMessage.MessageTo),
                  body: smsMessage.MessageBody,
              mediaUrl: new List<Uri>() { new Uri( smsMessage.MessageAttachments ) },
        statusCallback: new Uri( smsRestClient.ConfigToSmsStatusCallbackUrl.isValidUrl( false ) ? smsRestClient.ConfigToSmsStatusCallbackUrl : null)
    );

    return msgResult;
}

The new static method Init is being performed within SmsConfiguration, no problems there. But in my 4.x version instead of MessageResource I've been using the Message class and that Message class had a RestException which contained all the error from Twilio, now if I used MessageResource I need to use a Try..Catch and msgResult never got populated. Because of that every single code line form that point on explode because that msgResult variable is only been populated with successful messages passed to Twilio.

Any ideas?

Upvotes: 2

Views: 112

Answers (1)

Ray
Ray

Reputation: 523

I already found a solution! Must implement the ApiException instead of the SystemException. With the APIException I created an Object of type MessageResult with the Exception and the MessageResource as properties, then fill accordingly with a boolean flag for RestException simulation.

Upvotes: 1

Related Questions