Skip to content

Commit

Permalink
refactor: improve EZError, remove errorMessage in EZQueryResult
Browse files Browse the repository at this point in the history
  • Loading branch information
tisfeng committed Dec 14, 2023
1 parent a2b9824 commit 9c076c9
Show file tree
Hide file tree
Showing 21 changed files with 171 additions and 128 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ - (void)translate:(NSString *)text from:(EZLanguage)from to:(EZLanguage)to compl

EZError *error = nil;
if (htmlString.length == 0) {
error = [EZError errorWithType:EZErrorTypeNoResultsFound message:nil];
error = [EZError errorWithType:EZErrorTypeNoResultsFound description:nil];
}

completion(self.result, error);
Expand Down
2 changes: 1 addition & 1 deletion Easydict/Feature/Service/Apple/EZAppleService.m
Original file line number Diff line number Diff line change
Expand Up @@ -801,7 +801,7 @@ - (void)ocrImage:(NSImage *)image
[self ocrImage:image language:tryLanguage autoDetect:YES completion:completion];
return;
} else {
error = [EZError errorWithType:EZErrorTypeAPI message:NSLocalizedString(@"ocr_result_is_empty", nil)];
error = [EZError errorWithType:EZErrorTypeAPI description:NSLocalizedString(@"ocr_result_is_empty", nil)];

// We try to use Japanese before, but failed, so need to reset to auto.
ocrResult.from = EZLanguageAuto;
Expand Down
4 changes: 2 additions & 2 deletions Easydict/Feature/Service/Apple/EZScriptExecutor.m
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ - (NSTask *)runAppleScriptWithTask:(NSString *)script completionHandler:(void (^
NSArray *array = [errorString componentsSeparatedByString:@"execution error: "];
if (array.count > 1) {
errorString = [array[1] trim];
error = [EZError errorWithType:EZErrorTypeAPI message:errorString];
error = [EZError errorWithType:EZErrorTypeAPI description:errorString];
}
}

Expand Down Expand Up @@ -102,7 +102,7 @@ - (void)runAppleScript:(NSString *)script completionHandler:(void (^)(NSString *
if (errorInfo) {
MMLogInfo(@"runAppleScript errorInfo: %@", errorInfo);
NSString *errorString = errorInfo[NSAppleScriptErrorMessage];
error = [EZError errorWithType:EZErrorTypeAPI message:errorString];
error = [EZError errorWithType:EZErrorTypeAPI description:errorString];
}

CFAbsoluteTime endTime = CFAbsoluteTimeGetCurrent();
Expand Down
32 changes: 16 additions & 16 deletions Easydict/Feature/Service/Baidu/EZBaiduTranslate.m
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ - (nullable NSString *)wordLink:(EZQueryModel *)queryModel {

- (void)translate:(NSString *)text from:(EZLanguage)from to:(EZLanguage)to completion:(nonnull void (^)(EZQueryResult *, NSError *_Nullable))completion {
if (!text.length) {
completion(self.result, [EZError errorWithType:EZErrorTypeParam message:@"翻译的文本为空" request:nil]);
completion(self.result, [EZError errorWithType:EZErrorTypeParam description:@"翻译的文本为空" request:nil]);
return;
}

Expand Down Expand Up @@ -243,7 +243,7 @@ - (void)translate:(NSString *)text from:(EZLanguage)from to:(EZLanguage)to compl
mm_strongify(self)
NSLog(@"Baidu token: %@, gtk: %@", token, gtk);
if (!error && (!token || !gtk)) {
error = [EZError errorWithType:EZErrorTypeAPI message:@"Get token failed."];
error = [EZError errorWithType:EZErrorTypeAPI description:@"Get token failed."];
}
if (error) {
completion(self.result, error);
Expand All @@ -261,7 +261,7 @@ - (void)translate:(NSString *)text from:(EZLanguage)from to:(EZLanguage)to compl

- (void)detectText:(NSString *)text completion:(nonnull void (^)(EZLanguage, NSError *_Nullable))completion {
if (!text.length) {
completion(EZLanguageAuto, [EZError errorWithType:EZErrorTypeParam message:@"识别语言的文本为空" request:nil]);
completion(EZLanguageAuto, [EZError errorWithType:EZErrorTypeParam description:@"识别语言的文本为空" request:nil]);
return;
}

Expand All @@ -284,20 +284,20 @@ - (void)detectText:(NSString *)text completion:(nonnull void (^)(EZLanguage, NSE
if ([from isKindOfClass:NSString.class] && from.length) {
completion([self languageEnumFromCode:from], nil);
} else {
completion(EZLanguageAuto, [EZError errorWithType:EZErrorTypeUnsupportedLanguage message:nil request:reqDict]);
completion(EZLanguageAuto, [EZError errorWithType:EZErrorTypeUnsupportedLanguage description:nil request:reqDict]);
}
return;
}
completion(EZLanguageAuto, [EZError errorWithType:EZErrorTypeAPI message:@"判断语言失败" request:reqDict]);
completion(EZLanguageAuto, [EZError errorWithType:EZErrorTypeAPI description:@"判断语言失败" request:reqDict]);
} failure:^(NSURLSessionDataTask *_Nullable task, NSError *_Nonnull error) {
[reqDict setObject:error forKey:EZTranslateErrorRequestErrorKey];
completion(EZLanguageAuto, [EZError errorWithType:EZErrorTypeNetwork message:@"判断语言失败" request:reqDict]);
completion(EZLanguageAuto, [EZError errorWithType:EZErrorTypeNetwork description:@"判断语言失败" request:reqDict]);
}];
}

- (void)textToAudio:(NSString *)text fromLanguage:(EZLanguage)from completion:(void (^)(NSString *_Nullable, NSError *_Nullable))completion {
if (!text.length) {
completion(nil, [EZError errorWithType:EZErrorTypeParam message:@"获取音频的文本为空" request:nil]);
completion(nil, [EZError errorWithType:EZErrorTypeParam description:@"获取音频的文本为空" request:nil]);
return;
}

Expand Down Expand Up @@ -357,7 +357,7 @@ - (NSString *)getAudioURLWithText:(NSString *)text langCode:(NSString *)ttsLangC

- (void)ocr:(NSImage *)image from:(EZLanguage)from to:(EZLanguage)to completion:(void (^)(EZOCRResult *_Nullable, NSError *_Nullable))completion {
if (!image) {
completion(nil, [EZError errorWithType:EZErrorTypeParam message:@"图片为空" request:nil]);
completion(nil, [EZError errorWithType:EZErrorTypeParam description:@"图片为空" request:nil]);
return;
}

Expand Down Expand Up @@ -427,16 +427,16 @@ - (void)ocr:(NSImage *)image from:(EZLanguage)from to:(EZLanguage)to completion:
message = @"百度翻译OCR接口数据解析异常";
}
[reqDict setObject:responseObject ?: [NSNull null] forKey:EZTranslateErrorRequestResponseKey];
completion(nil, [EZError errorWithType:EZErrorTypeAPI message:message ?: @"识别图片文本失败" request:reqDict]);
completion(nil, [EZError errorWithType:EZErrorTypeAPI description:message ?: @"识别图片文本失败" request:reqDict]);
} failure:^(NSURLSessionDataTask *_Nullable task, NSError *_Nonnull error) {
[reqDict setObject:error forKey:EZTranslateErrorRequestErrorKey];
completion(nil, [EZError errorWithType:EZErrorTypeNetwork message: @"识别图片文本失败" request:reqDict]);
completion(nil, [EZError errorWithType:EZErrorTypeNetwork description: @"识别图片文本失败" request:reqDict]);
}];
}

- (void)ocrAndTranslate:(NSImage *)image from:(EZLanguage)from to:(EZLanguage)to ocrSuccess:(void (^)(EZOCRResult *_Nonnull, BOOL))ocrSuccess completion:(void (^)(EZOCRResult *_Nullable, EZQueryResult *_Nullable, NSError *_Nullable))completion {
if (!image) {
completion(nil, nil, [EZError errorWithType:EZErrorTypeParam message:@"图片为空" request:nil]);
completion(nil, nil, [EZError errorWithType:EZErrorTypeParam description:@"图片为空" request:nil]);
return;
}
mm_weakify(self);
Expand Down Expand Up @@ -492,7 +492,7 @@ - (void)sendTranslateRequest:(NSString *)text from:(EZLanguage)from to:(EZLangua
}
NSMutableDictionary *reqDict = [NSMutableDictionary dictionaryWithObjectsAndKeys:url, EZTranslateErrorRequestURLKey, params, EZTranslateErrorRequestParamKey, nil];
[reqDict setObject:error forKey:EZTranslateErrorRequestErrorKey];
completion(self.result, [EZError errorWithType:EZErrorTypeNetwork message: nil request:reqDict]);
completion(self.result, [EZError errorWithType:EZErrorTypeNetwork description: nil request:reqDict]);
}];

[self.queryModel setStopBlock:^{
Expand Down Expand Up @@ -743,7 +743,7 @@ - (void)parseResponseObject:(id _Nullable)responseObject completion:(nonnull voi
// If error, update cookie.
[self updateCookieAndToken];

NSError *error = [EZError errorWithType:EZErrorTypeAPI message: message request:reqDict];
NSError *error = [EZError errorWithType:EZErrorTypeAPI description: message request:reqDict];
MMLogInfo(@"baidu API error: %@", error);

[self webViewTranslate:completion];
Expand Down Expand Up @@ -781,11 +781,11 @@ - (void)sendGetTokenAndGtkRequestWithCompletion:(void (^)(NSString *_Nullable to
completion(token, gtk, nil);
} else {
[reqDict setObject:responseObject ?: [NSNull null] forKey:EZTranslateErrorRequestResponseKey];
completion(nil, nil, [EZError errorWithType:EZErrorTypeAPI message: @"获取 token 失败" request:reqDict]);
completion(nil, nil, [EZError errorWithType:EZErrorTypeAPI description: @"获取 token 失败" request:reqDict]);
}
} failure:^(NSURLSessionDataTask *_Nullable task, NSError *_Nonnull error) {
[reqDict setObject:error forKey:EZTranslateErrorRequestErrorKey];
completion(nil, nil, [EZError errorWithType:EZErrorTypeNetwork message: @"获取 token 失败" request:reqDict]);
completion(nil, nil, [EZError errorWithType:EZErrorTypeNetwork description: @"获取 token 失败" request:reqDict]);
}];
}

Expand All @@ -801,7 +801,7 @@ - (void)updateCookieAndToken {
[self sendGetTokenAndGtkRequestWithCompletion:^(NSString *token, NSString *gtk, NSError *error) {
NSLog(@"Baidu token: %@, gtk: %@", token, gtk);
if (!error && (!token || !gtk)) {
error = [EZError errorWithType:EZErrorTypeAPI message:@"Get token failed."];
error = [EZError errorWithType:EZErrorTypeAPI description:@"Get token failed."];
}

self.token = token;
Expand Down
20 changes: 10 additions & 10 deletions Easydict/Feature/Service/Bing/EZBingRequest.m
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ - (void)fetchBingConfig:(void (^)(void))callback failure:(nonnull void (^)(NSErr
[self.htmlSession GET:url parameters:nil progress:nil success:^(NSURLSessionDataTask *_Nonnull task, id _Nullable responseObject) {
NSError *error;
if (![responseObject isKindOfClass:[NSData class]]) {
error = [EZError errorWithType:EZErrorTypeAPI message: @"bing htmlSession responseObject is not NSData" request:nil];
error = [EZError errorWithType:EZErrorTypeAPI description: @"bing htmlSession responseObject is not NSData" request:nil];
failure(error);
NSLog(@"bing html responseObject type is %@", [responseObject class]);
return;
Expand All @@ -289,35 +289,35 @@ - (void)fetchBingConfig:(void (^)(void))callback failure:(nonnull void (^)(NSErr

NSString *IG = [self getIGValueFromHTML:responseString];
if (IG.length == 0) {
error = [EZError errorWithType:EZErrorTypeAPI message: @"bing IG is empty" request:nil];
error = [EZError errorWithType:EZErrorTypeAPI description: @"bing IG is empty" request:nil];
failure(error);
return;
}
NSLog(@"bing IG: %@", IG);

NSString *IID = [self getValueOfDataIidFromHTML:responseString];
if (IID.length == 0) {
error = [EZError errorWithType:EZErrorTypeAPI message: @"bing IID is empty" request:nil];
error = [EZError errorWithType:EZErrorTypeAPI description: @"bing IID is empty" request:nil];
failure(error);
return;
}
NSLog(@"bing IID: %@", IID);

NSArray *arr = [self getParamsAbusePreventionHelperArrayFromHTML:responseString];
if (arr.count != 3) {
error = [EZError errorWithType:EZErrorTypeAPI message: @"bing get key and token failed" request:nil];
error = [EZError errorWithType:EZErrorTypeAPI description: @"bing get key and token failed" request:nil];
failure(error);
return;
}
NSString *key = arr[0];
if (key.length == 0) {
error = [EZError errorWithType:EZErrorTypeAPI message: @"bing key is empey" request:nil];
error = [EZError errorWithType:EZErrorTypeAPI description: @"bing key is empey" request:nil];
failure(error);
return;
}
NSString *token = arr[1];
if (token.length == 0) {
error = [EZError errorWithType:EZErrorTypeAPI message: @"bing token is empey" request:nil];
error = [EZError errorWithType:EZErrorTypeAPI description: @"bing token is empey" request:nil];
failure(error);
return;
}
Expand Down Expand Up @@ -362,7 +362,7 @@ - (void)translateText:(NSString *)text from:(NSString *)from to:(NSString *)to c
[self.translateSession POST:self.bingConfig.ttranslatev3URLString parameters:translateParameters
progress:nil success:^(NSURLSessionDataTask *_Nonnull task, id _Nullable responseObject) {
if (![responseObject isKindOfClass:[NSData class]]) {
self.translateError = [EZError errorWithType:EZErrorTypeAPI message:@"bing translate responseObject is not NSData" request:nil];
self.translateError = [EZError errorWithType:EZErrorTypeAPI description:@"bing translate responseObject is not NSData" request:nil];
NSLog(@"bing translate responseObject type: %@", [responseObject class]);
[self executeCallback];
return;
Expand All @@ -375,7 +375,7 @@ - (void)translateText:(NSString *)text from:(NSString *)from to:(NSString *)to c
// if you use a VPN, you can try replacing nodes,or try adding `bing.com` into a direct rule
// https://immersivetranslate.com/docs/faq/#429-%E9%94%99%E8%AF%AF
if (response.statusCode == 429) {
self.translateError = [EZError errorWithType:EZErrorTypeAPI message:@"429 error, Bing translate too many requests" request:nil];
self.translateError = [EZError errorWithType:EZErrorTypeAPI description:@"429 error, Bing translate too many requests" request:nil];
} else {
self.translateError = error;
}
Expand All @@ -391,7 +391,7 @@ - (void)translateText:(NSString *)text from:(NSString *)from to:(NSString *)to c
[self.translateSession POST:self.bingConfig.tlookupv3URLString parameters:dictParameters
progress:nil success:^(NSURLSessionDataTask *_Nonnull task, id _Nullable responseObject) {
if (![responseObject isKindOfClass:[NSData class]]) {
self.lookupError = [EZError errorWithType:EZErrorTypeAPI message:@"bing translate responseObject is not NSData" request:nil];
self.lookupError = [EZError errorWithType:EZErrorTypeAPI description:@"bing translate responseObject is not NSData" request:nil];
NSLog(@"bing lookup responseObject type: %@", [responseObject class]);
[self executeCallback];
return;
Expand Down Expand Up @@ -453,7 +453,7 @@ - (void)translateTextFromDict:(NSString *)text completion:(void (^)(NSDictionary
@"q": text,
} progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
if (![responseObject isKindOfClass:[NSDictionary class]]) {
completion(nil, [EZError errorWithType:EZErrorTypeAPI message:@"bing dict translate json parse fail" request:nil]);
completion(nil, [EZError errorWithType:EZErrorTypeAPI description:@"bing dict translate json parse fail" request:nil]);
return;
}
completion(responseObject, nil);
Expand Down
12 changes: 6 additions & 6 deletions Easydict/Feature/Service/Bing/EZBingService.m
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ - (void)bingTranslate:(NSString *)text useDictQuery:(BOOL)useDictQuery from:(non
completion(self.result, translateError);
} @catch (NSException *exception) {
MMLogInfo(@"微软翻译接口数据解析异常 %@", exception);
completion(self.result, [EZError errorWithType:EZErrorTypeAPI message:@"bing translate data parse failed" request:nil]);
completion(self.result, [EZError errorWithType:EZErrorTypeAPI description:@"bing translate data parse failed" request:nil]);
}
}];
}
Expand Down Expand Up @@ -229,7 +229,7 @@ - (NSString *)maxTextLength:(NSString *)text fromLanguage:(EZLanguage)from {

- (nullable NSError *)processTranslateResult:(NSData *)translateData text:(NSString *)text from:(EZLanguage)from to:(EZLanguage)to needRetry:(BOOL *)needRetry {
if (translateData.length == 0) {
return [EZError errorWithType:EZErrorTypeAPI message:@"bing translate data is empty" request:nil];
return [EZError errorWithType:EZErrorTypeAPI description:@"bing translate data is empty" request:nil];
}
NSArray *json = [NSJSONSerialization JSONObjectWithData:translateData options:0 error:nil];
if (![json isKindOfClass:[NSArray class]]) {
Expand All @@ -244,7 +244,7 @@ - (nullable NSError *)processTranslateResult:(NSData *)translateData text:(NSStr
}
}
}
return [EZError errorWithType:EZErrorTypeAPI message:msg request:nil];
return [EZError errorWithType:EZErrorTypeAPI description:msg request:nil];
}
EZBingTranslateModel *translateModel = [EZBingTranslateModel mj_objectArrayWithKeyValuesArray:json].firstObject;
self.result.from = translateModel.detectedLanguage.language ? [self languageEnumFromCode:translateModel.detectedLanguage.language] : from;
Expand Down Expand Up @@ -343,12 +343,12 @@ - (void)parseBingDictTranslate:(NSDictionary *)json word:(NSString *)word comple
@try {
NSArray *value = json[@"value"];
if (value.count == 0) {
completion(self.result, [EZError errorWithType:EZErrorTypeAPI message:@"bing dict value is empty" request:nil]);
completion(self.result, [EZError errorWithType:EZErrorTypeAPI description:@"bing dict value is empty" request:nil]);
return;
}
NSArray *meaningGroups = value.firstObject[@"meaningGroups"];
if (meaningGroups.count == 0) {
completion(self.result, [EZError errorWithType:EZErrorTypeAPI message:@"bing dict translate meaning groups is empty" request:nil]);
completion(self.result, [EZError errorWithType:EZErrorTypeAPI description:@"bing dict translate meaning groups is empty" request:nil]);
return;
}

Expand Down Expand Up @@ -483,7 +483,7 @@ - (void)parseBingDictTranslate:(NSDictionary *)json word:(NSString *)word comple
completion(self.result, nil);
} @catch (NSException *exception) {
MMLogInfo(@"微软词典接口数据解析异常 %@", exception);
completion(self.result, [EZError errorWithType:EZErrorTypeAPI message:@"bing dict translate data parse failed" request:nil]);
completion(self.result, [EZError errorWithType:EZErrorTypeAPI description:@"bing dict translate data parse failed" request:nil]);
}
}

Expand Down
7 changes: 4 additions & 3 deletions Easydict/Feature/Service/Caiyun/CaiyunService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public final class CaiyunService: QueryService {
guard transType != .unsupported else {
let showingFrom = EZLanguageManager.shared().showingLanguageName(from)
let showingTo = EZLanguageManager.shared().showingLanguageName(to)
let error = EZError.init(type: .unsupportedLanguage, message: "\(showingFrom) ——> \(showingTo)")
let error = EZError.init(type: .unsupportedLanguage, description: "\(showingFrom) --> \(showingTo)")
completion(result, error)
return
}
Expand Down Expand Up @@ -96,11 +96,12 @@ public final class CaiyunService: QueryService {
result.translatedResults = value.target
completion(result, nil)
case let .failure(error):
let ezError = EZError.init(nsError: error)
if let data = response.data {
result.errorMessage = String(data: data, encoding: .utf8)
ezError?.errorDataMessage = String(data: data, encoding: .utf8);
}
NSLog("Caiyun lookup error \(error)")
completion(result, error)
completion(result, ezError)
}
}
queryModel.setStop({
Expand Down
Loading

0 comments on commit 9c076c9

Please sign in to comment.