From 053fcfec0b838f9308fc1f8fd66695e529b3dc02 Mon Sep 17 00:00:00 2001 From: cKey <35512826+colinin@users.noreply.github.com> Date: Fri, 6 Nov 2020 12:48:01 +0800 Subject: [PATCH] Update aliyun SMS error code --- .../Abp/Sms/Aliyun/AliyunSmsException.cs | 28 +++++++++ .../Abp/Sms/Aliyun/AliyunSmsResponse.cs | 57 +++++++++++++++++-- .../LINYUN/Abp/Sms/Aliyun/AliyunSmsSender.cs | 12 ++-- .../Sms/Aliyun/Localization/Resources/en.json | 24 ++++++++ .../Localization/Resources/zh-Hans.json | 24 ++++++++ 5 files changed, 134 insertions(+), 11 deletions(-) create mode 100644 aspnet-core/modules/common/LINGYUN.Abp.Sms.Aliyun/LINYUN/Abp/Sms/Aliyun/AliyunSmsException.cs diff --git a/aspnet-core/modules/common/LINGYUN.Abp.Sms.Aliyun/LINYUN/Abp/Sms/Aliyun/AliyunSmsException.cs b/aspnet-core/modules/common/LINGYUN.Abp.Sms.Aliyun/LINYUN/Abp/Sms/Aliyun/AliyunSmsException.cs new file mode 100644 index 000000000..9b8795eed --- /dev/null +++ b/aspnet-core/modules/common/LINGYUN.Abp.Sms.Aliyun/LINYUN/Abp/Sms/Aliyun/AliyunSmsException.cs @@ -0,0 +1,28 @@ +using Microsoft.Extensions.Logging; +using Volo.Abp; +using Volo.Abp.ExceptionHandling; +using Volo.Abp.Localization; +using Volo.Abp.Logging; + +namespace LINYUN.Abp.Sms.Aliyun +{ + public class AliyunSmsException : AbpException, IHasErrorCode, ILocalizeErrorMessage, IHasLogLevel + { + public AliyunSmsException(string code, string message) + :base(message) + { + Code = code; + LogLevel = LogLevel.Warning; + } + + public LogLevel LogLevel { get; set; } + + public string Code { get; } + + public string LocalizeMessage(LocalizationContext context) + { + return AliyunSmsResponse.GetErrorMessage(Code, Message) + .Localize(context.LocalizerFactory); + } + } +} diff --git a/aspnet-core/modules/common/LINGYUN.Abp.Sms.Aliyun/LINYUN/Abp/Sms/Aliyun/AliyunSmsResponse.cs b/aspnet-core/modules/common/LINGYUN.Abp.Sms.Aliyun/LINYUN/Abp/Sms/Aliyun/AliyunSmsResponse.cs index 66e623ffa..28ca1b510 100644 --- a/aspnet-core/modules/common/LINGYUN.Abp.Sms.Aliyun/LINYUN/Abp/Sms/Aliyun/AliyunSmsResponse.cs +++ b/aspnet-core/modules/common/LINGYUN.Abp.Sms.Aliyun/LINYUN/Abp/Sms/Aliyun/AliyunSmsResponse.cs @@ -16,11 +16,60 @@ namespace LINYUN.Abp.Sms.Aliyun return "ok".Equals(Code, StringComparison.CurrentCultureIgnoreCase); } - public ILocalizableString GetErrorMessage() + public static ILocalizableString GetErrorMessage(string code, string message) { - Check.NotNullOrWhiteSpace(Code, nameof(Code)); - switch (Code) + // TODO: 把前缀写入本地化文档里面? + Check.NotNullOrWhiteSpace(code, nameof(code)); + switch (code) { + case "isv.SMS_SIGNATURE_SCENE_ILLEGAL": + return LocalizableString.Create("SMS_SIGNATURE_SCENE_ILLEGAL"); + case "isv.DENY_IP_RANGE": + return LocalizableString.Create("DENY_IP_RANGE"); + case "isv.MOBILE_COUNT_OVER_LIMIT": + return LocalizableString.Create("MOBILE_COUNT_OVER_LIMIT"); + case "isv.BUSINESS_LIMIT_CONTROL": + return LocalizableString.Create("BUSINESS_LIMIT_CONTROL"); + case "SignatureDoesNotMatch": + return LocalizableString.Create("SignatureDoesNotMatch"); + case "InvalidTimeStamp.Expired": + return LocalizableString.Create("InvalidTimeStampExpired"); + case "SignatureNonceUsed": + return LocalizableString.Create("SignatureNonceUsed"); + case "InvalidVersion": + return LocalizableString.Create("InvalidVersion"); + case "InvalidAction.NotFound": + return LocalizableString.Create("InvalidActionNotFound"); + case "isv.SIGN_COUNT_OVER_LIMIT": + return LocalizableString.Create("SIGN_COUNT_OVER_LIMIT"); + case "isv.TEMPLATE_COUNT_OVER_LIMIT": + return LocalizableString.Create("TEMPLATE_COUNT_OVER_LIMIT"); + case "isv.SIGN_NAME_ILLEGAL": + return LocalizableString.Create("SIGN_NAME_ILLEGAL"); + case "isv.SIGN_FILE_LIMIT": + return LocalizableString.Create("SIGN_FILE_LIMIT"); + case "isv.SIGN_OVER_LIMIT": + return LocalizableString.Create("SIGN_OVER_LIMIT"); + case "isv.TEMPLATE_OVER_LIMIT": + return LocalizableString.Create("TEMPLATE_OVER_LIMIT"); + case "SIGNATURE_BLACKLIST": + return LocalizableString.Create("SIGNATURE_BLACKLIST"); + case "isv.SHORTURL_OVER_LIMIT": + return LocalizableString.Create("SHORTURL_OVER_LIMIT"); + case "isv.NO_AVAILABLE_SHORT_URL": + return LocalizableString.Create("NO_AVAILABLE_SHORT_URL"); + case "isv.SHORTURL_NAME_ILLEGAL": + return LocalizableString.Create("SHORTURL_NAME_ILLEGAL"); + case "isv.SOURCEURL_OVER_LIMIT": + return LocalizableString.Create("SOURCEURL_OVER_LIMIT"); + case "isv.SHORTURL_TIME_ILLEGAL": + return LocalizableString.Create("SHORTURL_TIME_ILLEGAL"); + case "isv.PHONENUMBERS_OVER_LIMIT": + return LocalizableString.Create("PHONENUMBERS_OVER_LIMIT"); + case "isv.SHORTURL_STILL_AVAILABLE": + return LocalizableString.Create("SHORTURL_STILL_AVAILABLE"); + case "isv.SHORTURL_NOT_FOUND": + return LocalizableString.Create("SHORTURL_NOT_FOUND"); case "isv.SMS_TEMPLATE_ILLEGAL": return LocalizableString.Create("SMS_TEMPLATE_ILLEGAL"); case "isv.SMS_SIGNATURE_ILLEGAL": @@ -68,7 +117,7 @@ namespace LINYUN.Abp.Sms.Aliyun case "isv.TEMPLATE_PARAMS_ILLEGAL": return LocalizableString.Create("TEMPLATE_PARAMS_ILLEGAL"); default: - throw new AbpException("no error code define!"); + throw new AbpException($"no error code: {code} define, message: {message}"); } } diff --git a/aspnet-core/modules/common/LINGYUN.Abp.Sms.Aliyun/LINYUN/Abp/Sms/Aliyun/AliyunSmsSender.cs b/aspnet-core/modules/common/LINGYUN.Abp.Sms.Aliyun/LINYUN/Abp/Sms/Aliyun/AliyunSmsSender.cs index f4d57211f..51d1df128 100644 --- a/aspnet-core/modules/common/LINGYUN.Abp.Sms.Aliyun/LINYUN/Abp/Sms/Aliyun/AliyunSmsSender.cs +++ b/aspnet-core/modules/common/LINGYUN.Abp.Sms.Aliyun/LINYUN/Abp/Sms/Aliyun/AliyunSmsSender.cs @@ -65,15 +65,11 @@ namespace LINYUN.Abp.Sms.Aliyun var aliyunResponse = JsonSerializer.Deserialize(responseContent); if (!aliyunResponse.IsSuccess()) { - var localizerFactory = ServiceProvider.GetRequiredService(); - var localizerError = aliyunResponse.GetErrorMessage().Localize(localizerFactory); if (Options.VisableErrorToClient) { - var localizer = ServiceProvider.GetRequiredService>(); - localizerError = localizer["SendMessageFailed", localizerError]; - throw new UserFriendlyException(localizerError); + throw new AliyunSmsException(aliyunResponse.Code, aliyunResponse.Message); } - throw new AbpException($"Text message sending failed:{localizerError}!"); + throw new AbpException($"Text message sending failed, code:{aliyunResponse.Code}, message:{aliyunResponse.Message}!"); } } catch(ServerException se) @@ -121,7 +117,9 @@ namespace LINYUN.Abp.Sms.Aliyun if (Environment.IsDevelopment()) { // check phone number length... - Check.NotNullOrWhiteSpace(Options.DeveloperPhoneNumber, nameof(Options.DeveloperPhoneNumber), + Check.NotNullOrWhiteSpace( + Options.DeveloperPhoneNumber, + nameof(Options.DeveloperPhoneNumber), maxLength: 11, minLength: 11); request.AddQueryParameters("PhoneNumbers", Options.DeveloperPhoneNumber); } diff --git a/aspnet-core/modules/common/LINGYUN.Abp.Sms.Aliyun/LINYUN/Abp/Sms/Aliyun/Localization/Resources/en.json b/aspnet-core/modules/common/LINGYUN.Abp.Sms.Aliyun/LINYUN/Abp/Sms/Aliyun/Localization/Resources/en.json index 2f947f883..5ef98e5a2 100644 --- a/aspnet-core/modules/common/LINGYUN.Abp.Sms.Aliyun/LINYUN/Abp/Sms/Aliyun/Localization/Resources/en.json +++ b/aspnet-core/modules/common/LINGYUN.Abp.Sms.Aliyun/LINYUN/Abp/Sms/Aliyun/Localization/Resources/en.json @@ -2,6 +2,30 @@ "culture": "en", "texts": { "SendMessageFailed": "Text message sending failed:{0}", + "SMS_SIGNATURE_SCENE_ILLEGAL": "Signature scene used by SMS is illegal", + "DENY_IP_RANGE": "Region where source IP address is disabled", + "MOBILE_COUNT_OVER_LIMIT": "Number of phone Numbers exceeds limit.", + "BUSINESS_LIMIT_CONTROL": "Business limit_limit, SMS frequency beyond limit.", + "SignatureDoesNotMatch": "Signature encryption error.", + "InvalidTimeStampExpired": "Please use GMT time: Timestamp error caused by time zone difference. The time the request was issued and the time the server received the request is not within 15 minutes.", + "SignatureNonceUsed": "Unique random number repetition, SignatureNonce is unique random number, used to prevent network replay attacks, use different random Numbers for different requests.", + "InvalidVersion": "Version error.Please confirm the Version of the interface. The Version of the SMS API is 2017-05-25.", + "InvalidActionNotFound": "Error interface name specified in parameter Action.Use the correct interface address and interface name in the parameter Action.", + "SIGN_COUNT_OVER_LIMIT": "The number of signatures requested in a natural day exceeds the limit.Arrange the number of signature requests for each day and reapply the next day.", + "TEMPLATE_COUNT_OVER_LIMIT": "The number of templates requested in a natural day exceeds the limit.Arrange the number of template applications for each day reasonably, and reapply the next day..", + "SIGN_NAME_ILLEGAL": "Signature name does not comply with the specification.See: https://help.aliyun.com/document_detail/108076.html?spm=a2c4g.11186623.2.12.610966fa9Q0QYe", + "SIGN_FILE_LIMIT": "Signature authentication material attachment size exceeds limit.", + "SIGN_OVER_LIMIT": "Number of signature characters exceeding the limit.", + "TEMPLATE_OVER_LIMIT": "Number of signature characters exceeding the limit.", + "SIGNATURE_BLACKLIST": "Signature blacklist, signature contains illegal information, please modify the signature", + "SHORTURL_OVER_LIMIT": "The number of short chains created in a day exceeds the limit", + "NO_AVAILABLE_SHORT_URL": "No valid short chain", + "SHORTURL_NAME_ILLEGAL": "Short chain name illegal", + "SOURCEURL_OVER_LIMIT": "Number of original link characters exceeds limit", + "SHORTURL_TIME_ILLEGAL": "Short chain is valid beyond the limit", + "PHONENUMBERS_OVER_LIMIT": "Upload more phone Numbers than the limit", + "SHORTURL_STILL_AVAILABLE": "Short links generated by the original link are still valid", + "SHORTURL_NOT_FOUND": "No short chain to remove", "SMS_TEMPLATE_ILLEGAL": "template is not valid (not present or blocked)", "SMS_SIGNATURE_ILLEGAL": "the signature is not valid (not present or blocked)", "MOBILE_NUMBER_ILLEGAL": "invalid phone number ", diff --git a/aspnet-core/modules/common/LINGYUN.Abp.Sms.Aliyun/LINYUN/Abp/Sms/Aliyun/Localization/Resources/zh-Hans.json b/aspnet-core/modules/common/LINGYUN.Abp.Sms.Aliyun/LINYUN/Abp/Sms/Aliyun/Localization/Resources/zh-Hans.json index a56cd5852..f2ba1b6a5 100644 --- a/aspnet-core/modules/common/LINGYUN.Abp.Sms.Aliyun/LINYUN/Abp/Sms/Aliyun/Localization/Resources/zh-Hans.json +++ b/aspnet-core/modules/common/LINGYUN.Abp.Sms.Aliyun/LINYUN/Abp/Sms/Aliyun/Localization/Resources/zh-Hans.json @@ -2,6 +2,30 @@ "culture": "zh-Hans", "texts": { "SendMessageFailed": "发送短信错误:{0}", + "SMS_SIGNATURE_SCENE_ILLEGAL": "短信所使用签名场景非法", + "DENY_IP_RANGE": "源IP地址所在的地区被禁用", + "MOBILE_COUNT_OVER_LIMIT": "手机号码数量超过限制", + "BUSINESS_LIMIT_CONTROL": "业务限流,短信发送频率超限。", + "SignatureDoesNotMatch": "签名(Signature)加密错误。", + "InvalidTimeStampExpired": "请使用GMT时间: 由于时区差异造成时间戳错误,发出请求的时间和服务器接收到请求的时间不在15分钟内。", + "SignatureNonceUsed": "唯一随机数重复,SignatureNonce为唯一随机数,用于防止网络重放攻击,不同请求请使用不同的随机数值。", + "InvalidVersion": "版本号(Version)错误。请确认接口的版本号,短信服务的API版本号(Version)为2017-05-25。", + "InvalidActionNotFound": "参数Action中指定的接口名错误。请在参数Action中使用正确的接口地址和接口名。", + "SIGN_COUNT_OVER_LIMIT": "一个自然日中申请签名数量超过限制。合理安排每天的签名申请数量,次日重新申请。", + "TEMPLATE_COUNT_OVER_LIMIT": "一个自然日中申请模板数量超过限制。合理安排每天的模板申请数量,次日重新申请。", + "SIGN_NAME_ILLEGAL": "签名名称不符合规范。See: https://help.aliyun.com/document_detail/108076.html?spm=a2c4g.11186623.2.12.610966fa9Q0QYe", + "SIGN_FILE_LIMIT": "签名认证材料附件大小超过限制。", + "SIGN_OVER_LIMIT": "签名字符数量超过限制。", + "TEMPLATE_OVER_LIMIT": "签名字符数量超过限制。", + "SIGNATURE_BLACKLIST": "签名黑名单,签名内容涉及违规信息,请修改签名内容", + "SHORTURL_OVER_LIMIT": "一天创建短链数量超过限制", + "NO_AVAILABLE_SHORT_URL": "无有效短链", + "SHORTURL_NAME_ILLEGAL": "短链名称非法", + "SOURCEURL_OVER_LIMIT": "原始链接字符数量超过限制", + "SHORTURL_TIME_ILLEGAL": "短链有效期期限超过限制", + "PHONENUMBERS_OVER_LIMIT": "上传手机号个数超过上限", + "SHORTURL_STILL_AVAILABLE": "原始链接生成的短链仍在有效期内", + "SHORTURL_NOT_FOUND": "没有可删除的短链", "SMS_TEMPLATE_ILLEGAL": "模板不合法(不存在或被拉黑)", "SMS_SIGNATURE_ILLEGAL": "签名不合法(不存在或被拉黑)", "MOBILE_NUMBER_ILLEGAL": "无效的电话号码",