【发布时间】:2018-02-10 14:01:12
【问题描述】:
我正在将一些使用旧的 Twilio C# API 的代码升级到新的 API,但现在我无法弄清楚您在发送消息后如何检查错误。我能看到的所有示例都没有错误检查。所以旧代码是这样的:
var attemptWithAlphanumericId = client.SendMessage(fromName, to, message);
if (attemptWithAlphanumericId.RestException == null)
{
return !string.Equals(attemptWithAlphanumericId.Status, "failed", StringComparison.OrdinalIgnoreCase);
}
if (attemptWithAlphanumericId.RestException.Status == "400"
&& attemptWithAlphanumericId.RestException.Code == "21212")
{
var attemptWithFallbackNumber = client.SendMessage(fallbackNumber, to, message);
if (attemptWithFallbackNumber.RestException == null)
{
return !string.Equals(attemptWithFallbackNumber.Status, "failed", StringComparison.OrdinalIgnoreCase);
}
throw new InvalidOperationException(attemptWithFallbackNumber.RestException.Message);
}
throw new InvalidOperationException(attemptWithAlphanumericId.RestException.Message);
但是有了我开始使用的新 API 样式:
var twilioResponseMessage = MessageResource.Create(
to: new PhoneNumber(to),
from: new PhoneNumber(_from),
body: message
);
...响应消息对象上没有RestException 属性。我要如何检查RestException 的状态和代码?
【问题讨论】:
-
新库将引发 TwilioException,您可以在 try/catch 块中捕获它。
-
有相关文档的链接吗?
-
我现在做 :) 我们今天刚刚添加了这些。抱歉,以前没有,这绝对是我们的疏忽。 twilio.com/docs/libraries/csharp#handle-errors 和 twilio.com/docs/libraries/csharp/…