Skip to content

Commit

Permalink
fix: resolve error type in EZBingService and EZYoudaoTranslate
Browse files Browse the repository at this point in the history
  • Loading branch information
tisfeng committed Dec 14, 2023
1 parent f414edf commit a2b9824
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 9 deletions.
4 changes: 2 additions & 2 deletions Easydict/Feature/Service/Bing/EZBingService.m
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ - (void)bingTranslate:(NSString *)text useDictQuery:(BOOL)useDictQuery from:(non
mm_strongify(self)
@try {
if (translateError) {
self.result.error = translateError;
self.result.error = [EZError errorWithNSError:translateError];
NSLog(@"bing translate error %@", translateError);
} else {
BOOL needRetry;
Expand All @@ -143,7 +143,7 @@ - (void)bingTranslate:(NSString *)text useDictQuery:(BOOL)useDictQuery from:(non
}
self.canRetry = YES;
if (error) {
self.result.error = error;
self.result.error = [EZError errorWithNSError:error];
completion(self.result, error);
return;
}
Expand Down
3 changes: 3 additions & 0 deletions Easydict/Feature/Service/Model/EZError.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ typedef NS_ENUM(NSUInteger, EZErrorType) {
EZErrorTypeUnsupportedLanguage, // 不支持的语言
EZErrorTypeNoResultsFound, // 未查询到结果
EZErrorTypeInsufficientQuota, // 内置 API key 额度不足
EZErrorTypeWarppedNSError, // warp NSError
};

/// 错误,不支持的语言
Expand All @@ -47,6 +48,8 @@ FOUNDATION_EXPORT NSError *EZQueryUnsupportedLanguageError(EZQueryService *servi

+ (instancetype)timeoutError;

+ (nullable EZError *)errorWithNSError:(nullable NSError *)error;

@end

NS_ASSUME_NONNULL_END
36 changes: 31 additions & 5 deletions Easydict/Feature/Service/Model/EZError.m
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
if (!to) {
unsupportLanguage = service.queryModel.queryTargetLanguage;
}

NSString *showUnsupportLanguage = [EZLanguageManager.shared showingLanguageName:unsupportLanguage];
NSError *error = [EZError errorWithType:EZErrorTypeUnsupportedLanguage message:showUnsupportLanguage request:nil];
return error;
Expand All @@ -35,6 +35,9 @@ + (instancetype)errorWithType:(EZErrorType)type
request:(id _Nullable)request {
NSString *errorString = nil;
switch (type) {
case EZErrorTypeNone:
errorString = @"";
break;
case EZErrorTypeParam:
errorString = NSLocalizedString(@"error_parameter", nil);
break;
Expand All @@ -53,25 +56,35 @@ + (instancetype)errorWithType:(EZErrorType)type
case EZErrorTypeInsufficientQuota:
errorString = NSLocalizedString(@"error_insufficient_quota", nil);
break;

default:
errorString = NSLocalizedString(@"error_unknown", nil);
break;
}

if (errorString.length) {
errorString = [NSString stringWithFormat:@"%@, %@", NSLocalizedString(@"query_failed", nil), errorString];
}

errorString = [NSString stringWithFormat:@"%@, %@", NSLocalizedString(@"query_failed", nil), errorString];
if (message.length) {
errorString = [NSString stringWithFormat:@"%@: %@", errorString, message];
}

if (type == EZErrorTypeWarppedNSError) {
errorString = message;
}

NSMutableDictionary *userInfo = [NSMutableDictionary dictionary];
[userInfo setObject:errorString forKey:NSLocalizedDescriptionKey];
if (errorString) {
[userInfo setObject:errorString forKey:NSLocalizedDescriptionKey];
}
if (request) {
[userInfo setObject:request forKey:EZTranslateErrorRequestKey];
}

EZError *error = [EZError errorWithDomain:EZBundleId code:type userInfo:userInfo.copy];
error.type = type;

return error;
}

Expand All @@ -84,4 +97,17 @@ + (instancetype)timeoutError {
return [self errorWithType:EZErrorTypeTimeout message:errorString request:nil];
}

+ (nullable EZError *)errorWithNSError:(nullable NSError *)error {
if (!error) {
return nil;
}

if ([error isKindOfClass:EZError.class]) {
return (EZError *)error;
}

EZError *ezError = [EZError errorWithType:EZErrorTypeWarppedNSError message:error.localizedDescription request:nil];
return ezError;
}

@end
2 changes: 1 addition & 1 deletion Easydict/Feature/Service/Youdao/EZYoudaoTranslate.m
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ - (void)queryYoudaoDictAndTranslation:(NSString *)text from:(EZLanguage)from to:
[self webTranslate:text from:from to:to completion:^(EZQueryResult *_Nullable result, NSError *_Nullable error) {
if (error) {
NSLog(@"translateYoudaoAPI error: %@", error);
self.result.error = error;
self.result.error = [EZError errorWithNSError:error];
}
dispatch_group_leave(group);
}];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -682,7 +682,7 @@ - (void)queryWithModel:(EZQueryModel *)queryModel
if (error) {
NSLog(@"query error: %@", error);
}
result.error = error;
result.error = [EZError errorWithNSError:error];

// Auto convert to traditional Chinese if needed.
if (service.autoConvertTraditionalChinese &&
Expand Down

0 comments on commit a2b9824

Please sign in to comment.