我正在将一些使用旧 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);
Run Code Online (Sandbox Code Playgroud)
但随着我开始使用的新 API 风格:
var twilioResponseMessage = MessageResource.Create(
to: new PhoneNumber(to),
from: new PhoneNumber(_from),
body: message
);
Run Code Online (Sandbox Code Playgroud)
RestException...响应消息对象没有属性。我该如何检查 的RestException状态和代码?
这是来自 twilio 开发网站的示例:https://www.twilio.com/docs/libraries/csharp-dotnet/usage-guide#handling-errors
try
{
var message = MessageResource.Create(
body: "This is the ship that made the Kessel Run in fourteen parsecs?",
from: new Twilio.Types.PhoneNumber("+15017122661"),
to: new Twilio.Types.PhoneNumber("+15558675310")
);
Console.WriteLine(message.Sid);
}
catch (ApiException e)
{
Console.WriteLine(e.Message);
Console.WriteLine($"Twilio Error {e.Code} - {e.MoreInfo}");
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2230 次 |
| 最近记录: |