diff --git a/Easydict/Feature/Service/Apple/AppleDictionary/EZAppleDictionary.m b/Easydict/Feature/Service/Apple/AppleDictionary/EZAppleDictionary.m index c235b8084..a9a0ba75e 100644 --- a/Easydict/Feature/Service/Apple/AppleDictionary/EZAppleDictionary.m +++ b/Easydict/Feature/Service/Apple/AppleDictionary/EZAppleDictionary.m @@ -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); diff --git a/Easydict/Feature/Service/Apple/EZAppleService.m b/Easydict/Feature/Service/Apple/EZAppleService.m index a01207325..9e3f2f069 100644 --- a/Easydict/Feature/Service/Apple/EZAppleService.m +++ b/Easydict/Feature/Service/Apple/EZAppleService.m @@ -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; diff --git a/Easydict/Feature/Service/Apple/EZScriptExecutor.m b/Easydict/Feature/Service/Apple/EZScriptExecutor.m index 0b00dd327..9a80d868d 100644 --- a/Easydict/Feature/Service/Apple/EZScriptExecutor.m +++ b/Easydict/Feature/Service/Apple/EZScriptExecutor.m @@ -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]; } } @@ -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(); diff --git a/Easydict/Feature/Service/Baidu/EZBaiduTranslate.m b/Easydict/Feature/Service/Baidu/EZBaiduTranslate.m index 071d8f256..6fdeef8bb 100644 --- a/Easydict/Feature/Service/Baidu/EZBaiduTranslate.m +++ b/Easydict/Feature/Service/Baidu/EZBaiduTranslate.m @@ -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; } @@ -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); @@ -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; } @@ -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; } @@ -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; } @@ -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); @@ -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:^{ @@ -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]; @@ -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]); }]; } @@ -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; diff --git a/Easydict/Feature/Service/Bing/EZBingRequest.m b/Easydict/Feature/Service/Bing/EZBingRequest.m index 47c7bfb70..3d6849b35 100644 --- a/Easydict/Feature/Service/Bing/EZBingRequest.m +++ b/Easydict/Feature/Service/Bing/EZBingRequest.m @@ -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; @@ -289,7 +289,7 @@ - (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; } @@ -297,7 +297,7 @@ - (void)fetchBingConfig:(void (^)(void))callback failure:(nonnull void (^)(NSErr 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; } @@ -305,19 +305,19 @@ - (void)fetchBingConfig:(void (^)(void))callback failure:(nonnull void (^)(NSErr 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; } @@ -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; @@ -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; } @@ -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; @@ -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); diff --git a/Easydict/Feature/Service/Bing/EZBingService.m b/Easydict/Feature/Service/Bing/EZBingService.m index 72d2b9e30..502bf8e86 100644 --- a/Easydict/Feature/Service/Bing/EZBingService.m +++ b/Easydict/Feature/Service/Bing/EZBingService.m @@ -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]); } }]; } @@ -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]]) { @@ -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; @@ -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; } @@ -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]); } } diff --git a/Easydict/Feature/Service/Caiyun/CaiyunService.swift b/Easydict/Feature/Service/Caiyun/CaiyunService.swift index 3aad37e93..d50cc9124 100644 --- a/Easydict/Feature/Service/Caiyun/CaiyunService.swift +++ b/Easydict/Feature/Service/Caiyun/CaiyunService.swift @@ -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 } @@ -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({ diff --git a/Easydict/Feature/Service/DeepL/EZDeepLTranslate.m b/Easydict/Feature/Service/DeepL/EZDeepLTranslate.m index 45635aaf7..d5749a2af 100644 --- a/Easydict/Feature/Service/DeepL/EZDeepLTranslate.m +++ b/Easydict/Feature/Service/DeepL/EZDeepLTranslate.m @@ -233,6 +233,7 @@ - (void)deepLWebTranslate:(NSString *)text from:(EZLanguage)from to:(EZLanguage) if (error) { NSLog(@"deepLWebTranslate error: %@", error); + EZError *ezError = [EZError errorWithNSError:error]; BOOL useOfficialAPI = (self.authKey.length > 0) && (self.apiType == EZDeepLTranslationAPIWebFirst); if (useOfficialAPI) { @@ -256,12 +257,12 @@ - (void)deepLWebTranslate:(NSString *)text from:(EZLanguage)from to:(EZLanguage) if (!jsonError) { NSString *errorMessage = json[@"error"][@"message"]; if (errorMessage.length) { - self.result.errorMessage = errorMessage; + ezError.errorDataMessage = errorMessage; } } } - completion(self.result, error); + completion(self.result, ezError); return; } diff --git a/Easydict/Feature/Service/Google/EZGoogleTranslate.m b/Easydict/Feature/Service/Google/EZGoogleTranslate.m index c34ecc166..8aadd6342 100644 --- a/Easydict/Feature/Service/Google/EZGoogleTranslate.m +++ b/Easydict/Feature/Service/Google/EZGoogleTranslate.m @@ -218,7 +218,7 @@ - (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; } @@ -275,7 +275,7 @@ - (void)ocr:(NSImage *)image 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; } @@ -292,7 +292,7 @@ - (void)ocrAndTranslate:(NSImage *)image 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; } @@ -323,7 +323,7 @@ - (void)ocrAndTranslate:(NSImage *)image /// This API can get word info, like pronunciation, but transaltion may be inaccurate, compare to web transaltion. - (void)webApptranslate:(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; } @@ -500,14 +500,14 @@ - (void)sendWebAppTranslate:(NSString *)text from:(EZLanguage)from to:(EZLanguag if (responseObject) { completion(responseObject, sign, reqDict, nil); } else { - completion(nil, nil, nil, [EZError errorWithType:EZErrorTypeAPI message: nil request:reqDict]); + completion(nil, nil, nil, [EZError errorWithType:EZErrorTypeAPI description: nil request:reqDict]); } } failure:^(NSURLSessionDataTask *_Nullable task, NSError *_Nonnull error) { if (error.code == NSURLErrorCancelled) { return; } [reqDict setObject:error forKey:EZTranslateErrorRequestErrorKey]; - completion(nil, nil, nil, [EZError errorWithType:EZErrorTypeNetwork message: nil request:reqDict]); + completion(nil, nil, nil, [EZError errorWithType:EZErrorTypeNetwork description: nil request:reqDict]); }]; [self.queryModel setStopBlock:^{ @@ -559,11 +559,11 @@ - (void)sendGetWebAppTKKRequestWithCompletion:(void (^)(NSString *_Nullable TKK, [reqDict setObject:responseObject ?: [NSNull null] forKey:EZTranslateErrorRequestResponseKey]; - completion(nil, [EZError errorWithType:EZErrorTypeAPI message: @"谷歌翻译获取 tkk 失败" request:reqDict]); + completion(nil, [EZError errorWithType:EZErrorTypeAPI description: @"谷歌翻译获取 tkk 失败" request:reqDict]); } } failure:^(NSURLSessionDataTask *_Nullable task, NSError *_Nonnull error) { [reqDict setObject:error forKey:EZTranslateErrorRequestErrorKey]; - completion(nil, [EZError errorWithType:EZErrorTypeNetwork message: @"谷歌翻译获取 tkk 失败" request:reqDict]); + completion(nil, [EZError errorWithType:EZErrorTypeNetwork description: @"谷歌翻译获取 tkk 失败" request:reqDict]); }]; } @@ -635,14 +635,14 @@ - (void)sendGTXTranslate:(NSString *)text if (responseObject) { completion(responseObject, sign, reqDict, nil); } else { - completion(nil, nil, nil, [EZError errorWithType:EZErrorTypeAPI message: nil request:reqDict]); + completion(nil, nil, nil, [EZError errorWithType:EZErrorTypeAPI description: nil request:reqDict]); } } failure:^(NSURLSessionDataTask *_Nullable task, NSError *_Nonnull error) { if (error.code == NSURLErrorCancelled) { return; } [reqDict setObject:error forKey:EZTranslateErrorRequestErrorKey]; - completion(nil, nil, nil, [EZError errorWithType:EZErrorTypeNetwork message: nil request:reqDict]); + completion(nil, nil, nil, [EZError errorWithType:EZErrorTypeNetwork description: nil request:reqDict]); }]; [self.queryModel setStopBlock:^{ @@ -657,7 +657,7 @@ - (void)gtxTranslate:(NSString *)text EZQueryResult *result = self.result; if (!text.length) { - completion(result, [EZError errorWithType:EZErrorTypeParam message:@"翻译的文本为空" request:nil]); + completion(result, [EZError errorWithType:EZErrorTypeParam description:@"翻译的文本为空" request:nil]); return; } @@ -734,14 +734,14 @@ - (void)gtxTranslate:(NSString *)text } } [reqDict setObject:responseObject ?: [NSNull null] forKey:EZTranslateErrorRequestResponseKey]; - completion(result, [EZError errorWithType:EZErrorTypeAPI message: message request:reqDict]); + completion(result, [EZError errorWithType:EZErrorTypeAPI description: message request:reqDict]); }]; } - (void)gtxDetectText:(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; } @@ -780,14 +780,14 @@ - (void)gtxDetectText:(NSString *)text exception); } [reqDict setObject:responseObject forKey:EZTranslateErrorRequestResponseKey]; - completion(EZLanguageAuto, [EZError errorWithType:EZErrorTypeAPI message: message ?: @"识别语言失败" request:reqDict]); + completion(EZLanguageAuto, [EZError errorWithType:EZErrorTypeAPI description: message ?: @"识别语言失败" request:reqDict]); }]; } - (void)webAppDetectText:(NSString *)text completion:(nonnull void (^)(EZLanguage, NSError *_Nullable))completion { if (!text.length) { completion(EZLanguageAuto, - [EZError errorWithType:EZErrorTypeParam message:@"识别语言的文本为空" request:nil]); + [EZError errorWithType:EZErrorTypeParam description:@"识别语言的文本为空" request:nil]); return; } @@ -850,7 +850,7 @@ - (void)webAppDetectText:(NSString *)text completion:(nonnull void (^)(EZLanguag MMLogInfo(@"谷歌翻译接口语言解析失败 %@", exception); } [reqDict setObject:responseObject forKey:EZTranslateErrorRequestResponseKey]; - completion(EZLanguageAuto, [EZError errorWithType:EZErrorTypeAPI message: message ?: @"识别语言失败" request:reqDict]); + completion(EZLanguageAuto, [EZError errorWithType:EZErrorTypeAPI description: message ?: @"识别语言失败" request:reqDict]); }]; } diff --git a/Easydict/Feature/Service/Model/EZDetectManager.m b/Easydict/Feature/Service/Model/EZDetectManager.m index dbfb962f5..40d6c9bff 100644 --- a/Easydict/Feature/Service/Model/EZDetectManager.m +++ b/Easydict/Feature/Service/Model/EZDetectManager.m @@ -95,7 +95,7 @@ - (void)detectText:(NSString *)queryText completion:(void (^)(EZQueryModel *_Non if (queryText.length == 0) { NSString *errorString = @"detectText cannot be nil"; NSLog(@"%@", errorString); - completion(self.queryModel, [EZError errorWithType:EZErrorTypeParam message:errorString]); + completion(self.queryModel, [EZError errorWithType:EZErrorTypeParam description:errorString]); return; } diff --git a/Easydict/Feature/Service/Model/EZError.h b/Easydict/Feature/Service/Model/EZError.h index 3891d7b43..cce074023 100644 --- a/Easydict/Feature/Service/Model/EZError.h +++ b/Easydict/Feature/Service/Model/EZError.h @@ -28,7 +28,7 @@ typedef NS_ENUM(NSUInteger, EZErrorType) { EZErrorTypeUnsupportedLanguage, // 不支持的语言 EZErrorTypeNoResultsFound, // 未查询到结果 EZErrorTypeInsufficientQuota, // 内置 API key 额度不足 - EZErrorTypeWarppedNSError, // warp NSError + EZErrorTypeWarppedNSError, // Warp NSError }; /// 错误,不支持的语言 @@ -38,17 +38,33 @@ FOUNDATION_EXPORT NSError *EZQueryUnsupportedLanguageError(EZQueryService *servi @interface EZError : NSError @property (nonatomic, assign) EZErrorType type; +@property (nonatomic, copy, nullable) NSString *errorDataMessage; + ++ (instancetype)errorWithType:(EZErrorType)type; + ++ (instancetype)errorWithType:(EZErrorType)type + description:(nullable NSString *)description; + ++ (instancetype)errorWithType:(EZErrorType)type + description:(nullable NSString *)description + errorDataMessage:(nullable NSString *)errorDataMessage; + (instancetype)errorWithType:(EZErrorType)type - message:(NSString *_Nullable)message; + description:(nullable NSString *)description + request:(id _Nullable)request; + (instancetype)errorWithType:(EZErrorType)type - message:(NSString *_Nullable)message + description:(nullable NSString *)description + errorDataMessage:(nullable NSString *)errorDataMessage request:(id _Nullable)request; + (instancetype)timeoutError; + +#pragma mark - Wrap NSError + + (nullable EZError *)errorWithNSError:(nullable NSError *)error; ++ (nullable EZError *)errorWithNSError:(nullable NSError *)error errorDataMessage:(nullable NSString *)errorMessage; @end diff --git a/Easydict/Feature/Service/Model/EZError.m b/Easydict/Feature/Service/Model/EZError.m index e0eb4103d..ab353f615 100644 --- a/Easydict/Feature/Service/Model/EZError.m +++ b/Easydict/Feature/Service/Model/EZError.m @@ -21,18 +21,40 @@ if (!to) { unsupportLanguage = service.queryModel.queryTargetLanguage; } - + NSString *showUnsupportLanguage = [EZLanguageManager.shared showingLanguageName:unsupportLanguage]; - NSError *error = [EZError errorWithType:EZErrorTypeUnsupportedLanguage message:showUnsupportLanguage request:nil]; + NSError *error = [EZError errorWithType:EZErrorTypeUnsupportedLanguage description:showUnsupportLanguage request:nil]; return error; } @implementation EZError ++ (instancetype)errorWithType:(EZErrorType)type { + return [self errorWithType:type description:nil]; +} + + (instancetype)errorWithType:(EZErrorType)type - message:(NSString *_Nullable)message + description:(nullable NSString *)description request:(id _Nullable)request { + return [self errorWithType:type description:description errorDataMessage:nil request:request]; +} + ++ (instancetype)errorWithType:(EZErrorType)type + description:(nullable NSString *)description { + return [self errorWithType:type description:description request:nil]; +} + ++ (instancetype)errorWithType:(EZErrorType)type + description:(nullable NSString *)description + errorDataMessage:(nullable NSString *)errorDataMessage { + return [self errorWithType:type description:description errorDataMessage:errorDataMessage request:nil]; +} + ++ (instancetype)errorWithType:(EZErrorType)type + description:(nullable NSString *)description + errorDataMessage:(nullable NSString *)errorDataMessage + request:(id)request { NSString *errorString = nil; switch (type) { case EZErrorTypeNone: @@ -61,19 +83,19 @@ + (instancetype)errorWithType:(EZErrorType)type errorString = NSLocalizedString(@"error_unknown", nil); break; } - + + NSString *queryFailedString = NSLocalizedString(@"query_failed", nil); if (errorString.length) { - errorString = [NSString stringWithFormat:@"%@, %@", NSLocalizedString(@"query_failed", nil), errorString]; + errorString = [NSString stringWithFormat:@"%@, %@", queryFailedString, errorString]; } - - if (message.length) { - errorString = [NSString stringWithFormat:@"%@: %@", errorString, message]; + if (description.length) { + errorString = [NSString stringWithFormat:@"%@: %@", errorString, description]; } if (type == EZErrorTypeWarppedNSError) { - errorString = message; + errorString = [NSString stringWithFormat:@"%@: %@", queryFailedString, description]; } - + NSMutableDictionary *userInfo = [NSMutableDictionary dictionary]; if (errorString) { [userInfo setObject:errorString forKey:NSLocalizedDescriptionKey]; @@ -81,32 +103,33 @@ + (instancetype)errorWithType:(EZErrorType)type if (request) { [userInfo setObject:request forKey:EZTranslateErrorRequestKey]; } - - EZError *error = [EZError errorWithDomain:EZBundleId code:type userInfo:userInfo.copy]; + + EZError *error = [self errorWithDomain:EZBundleId code:type userInfo:userInfo.copy]; error.type = type; - + error.errorDataMessage = errorDataMessage; + return error; } -+ (instancetype)errorWithType:(EZErrorType)type message:(NSString *)message { - return [self errorWithType:type message:message request:nil]; -} - + (instancetype)timeoutError { - NSString *errorString = [NSString stringWithFormat:@"Timeout of %.1f exceeded", EZNetWorkTimeoutInterval]; - return [self errorWithType:EZErrorTypeTimeout message:errorString request:nil]; + NSString *description = [NSString stringWithFormat:@"Timeout of %.1f exceeded", EZNetWorkTimeoutInterval]; + return [self errorWithType:EZErrorTypeTimeout description:description]; } + +#pragma mark - Wrap NSError + + (nullable EZError *)errorWithNSError:(nullable NSError *)error { - if (!error) { - return nil; - } - - if ([error isKindOfClass:EZError.class]) { + return [self errorWithNSError:error errorDataMessage:nil]; +} + ++ (nullable EZError *)errorWithNSError:(nullable NSError *)error + errorDataMessage:(nullable NSString *)errorDataMessage { + if (!error || [error isKindOfClass:EZError.class]) { return (EZError *)error; } - EZError *ezError = [EZError errorWithType:EZErrorTypeWarppedNSError message:error.localizedDescription request:nil]; + EZError *ezError = [self errorWithType:EZErrorTypeWarppedNSError description:error.localizedDescription errorDataMessage:errorDataMessage]; return ezError; } diff --git a/Easydict/Feature/Service/Model/EZQueryResult.h b/Easydict/Feature/Service/Model/EZQueryResult.h index 4ad1ca6df..797b15269 100644 --- a/Easydict/Feature/Service/Model/EZQueryResult.h +++ b/Easydict/Feature/Service/Model/EZQueryResult.h @@ -135,8 +135,6 @@ NS_ASSUME_NONNULL_BEGIN @property (nonatomic, copy, nullable) NSString *translatedText; @property (nonatomic, strong, nullable) EZError *error; -@property (nonatomic, copy, nullable) NSString *errorMessage; -//@property (nonatomic, assign) EZErrorType errorType; @property (nonatomic, assign) BOOL manulShow; diff --git a/Easydict/Feature/Service/Model/EZQueryResult.m b/Easydict/Feature/Service/Model/EZQueryResult.m index b838a2005..66edc6d2d 100644 --- a/Easydict/Feature/Service/Model/EZQueryResult.m +++ b/Easydict/Feature/Service/Model/EZQueryResult.m @@ -205,7 +205,7 @@ - (nullable NSString *)translatedText { } - (BOOL)hasShowingResult { - if (self.hasTranslatedResult || self.error || self.errorMessage.length || self.HTMLString.length) { + if (self.hasTranslatedResult || self.error || self.HTMLString.length) { return YES; } return NO; @@ -258,7 +258,6 @@ - (void)reset { self.promptURL = nil; self.showBigWord = NO; self.translateResultsTopInset = 0; - self.errorMessage = nil; self.isFinished = YES; self.manulShow = NO; self.HTMLString = nil; diff --git a/Easydict/Feature/Service/Model/EZQueryService.m b/Easydict/Feature/Service/Model/EZQueryService.m index 70a030750..b7cc43e1c 100644 --- a/Easydict/Feature/Service/Model/EZQueryService.m +++ b/Easydict/Feature/Service/Model/EZQueryService.m @@ -170,8 +170,10 @@ - (BOOL)prehandleQueryTextLanguage:(NSString *)text // Some services need API Key, the built-in API key free quota may not be enough for all users to use, so it is provided to new users first. if (self.needPrivateAPIKey && !self.hasPrivateAPIKey && ![EZLocalStorage.shared hasFreeQuotaLeft:self]) { - NSError *error = [EZError errorWithType:EZErrorTypeInsufficientQuota message:nil request:nil]; - self.result.errorMessage = NSLocalizedString(@"insufficient_quota_prompt", nil); + EZError *error = [EZError errorWithType:EZErrorTypeInsufficientQuota + description:nil + errorDataMessage:NSLocalizedString(@"insufficient_quota_prompt", nil)]; + self.result.promptURL = self.link; completion(self.result, error); return YES; diff --git a/Easydict/Feature/Service/Niutrans/EZNiuTransTranslate.m b/Easydict/Feature/Service/Niutrans/EZNiuTransTranslate.m index 0e3c8886c..3001a153a 100644 --- a/Easydict/Feature/Service/Niutrans/EZNiuTransTranslate.m +++ b/Easydict/Feature/Service/Niutrans/EZNiuTransTranslate.m @@ -162,7 +162,7 @@ - (void)niuTransTranslate:(NSString *)text from:(EZLanguage)from to:(EZLanguage) message = [NSString stringWithFormat:@"%@, %@", errorCode, errorMsg]; } NSError *error = [EZError errorWithType:EZErrorTypeAPI - message:message + description:message request:task.currentRequest]; completion(self.result, error); } diff --git a/Easydict/Feature/Service/OpenAI/EZOpenAIService.m b/Easydict/Feature/Service/OpenAI/EZOpenAIService.m index dd5079009..53d36c008 100644 --- a/Easydict/Feature/Service/OpenAI/EZOpenAIService.m +++ b/Easydict/Feature/Service/OpenAI/EZOpenAIService.m @@ -410,6 +410,7 @@ - (void)startStreamChat:(NSDictionary *)parameters return; } + EZError *ezError = [EZError errorWithNSError:error]; NSData *errorData = error.userInfo[AFNetworkingOperationFailingURLResponseDataErrorKey]; if (errorData) { /** @@ -425,10 +426,10 @@ - (void)startStreamChat:(NSDictionary *)parameters NSError *jsonError; NSDictionary *json = [NSJSONSerialization JSONObjectWithData:errorData options:kNilOptions error:&jsonError]; if (!jsonError) { - self.result.errorMessage = [self getJsonErrorMessageWithJson:json]; + ezError.errorDataMessage = [self getJsonErrorMessageWithJson:json]; } } - completion(nil, error); + completion(nil, ezError); }]; [self.queryModel setStopBlock:^{ @@ -619,7 +620,7 @@ - (nullable NSString *)parseContentFromJSONata:(NSData *)data */ NSArray *choices = json[@"choices"]; if (choices.count == 0) { - NSError *error = [EZError errorWithType:EZErrorTypeAPI message:@"no result."]; + NSError *error = [EZError errorWithType:EZErrorTypeAPI description:@"no result."]; /** may be return error json { @@ -633,7 +634,7 @@ - (nullable NSString *)parseContentFromJSONata:(NSData *)data */ if (json[@"error"]) { - error = [EZError errorWithType:EZErrorTypeAPI message:[self getJsonErrorMessageWithJson:json]]; + error = [EZError errorWithType:EZErrorTypeAPI description:[self getJsonErrorMessageWithJson:json]]; } return nil; @@ -683,7 +684,7 @@ - (void)startCompletion:(NSString *)prompt completion:(void (^)(NSString *_Nulla NSArray *choices = json[@"choices"]; if (choices.count == 0) { - NSError *error = [EZError errorWithType:EZErrorTypeAPI message:@"no result."]; + NSError *error = [EZError errorWithType:EZErrorTypeAPI description:@"no result."]; /** may be return error json { @@ -696,7 +697,7 @@ - (void)startCompletion:(NSString *)prompt completion:(void (^)(NSString *_Nulla } */ if (json[@"error"]) { - error = [EZError errorWithType:EZErrorTypeAPI message:[self getJsonErrorMessageWithJson:json]]; + error = [EZError errorWithType:EZErrorTypeAPI description:[self getJsonErrorMessageWithJson:json]]; } completion(nil, error); diff --git a/Easydict/Feature/Service/Tencent/TencentService.swift b/Easydict/Feature/Service/Tencent/TencentService.swift index 99a625988..853d2f8c2 100644 --- a/Easydict/Feature/Service/Tencent/TencentService.swift +++ b/Easydict/Feature/Service/Tencent/TencentService.swift @@ -87,7 +87,7 @@ public final class TencentService: 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 } @@ -125,15 +125,17 @@ public final class TencentService: QueryService { completion(result, nil) case let .failure(error): NSLog("Tencent lookup error \(error)") + let ezError = EZError.init(nsError: error) + if let data = response.data { do { let errorResponse = try JSONDecoder().decode(TencentErrorResponse.self, from: data) - result.errorMessage = errorResponse.response.error.message + ezError?.errorDataMessage = errorResponse.response.error.message } catch { NSLog("Failed to decode error response: \(error)") } } - completion(result, error) + completion(result, ezError) } } queryModel.setStop({ diff --git a/Easydict/Feature/Service/Youdao/EZYoudaoTranslate.m b/Easydict/Feature/Service/Youdao/EZYoudaoTranslate.m index f3627ecc5..f94c5a456 100644 --- a/Easydict/Feature/Service/Youdao/EZYoudaoTranslate.m +++ b/Easydict/Feature/Service/Youdao/EZYoudaoTranslate.m @@ -284,7 +284,7 @@ - (nullable NSString *)youdaoDictForeignLangauge:(EZQueryModel *)queryModel { - (void)translate:(NSString *)text from:(EZLanguage)from to:(EZLanguage)to completion:(void (^)(EZQueryResult *result, NSError *_Nullable error))completion { if (!text.length) { - completion(self.result, [EZError errorWithType:EZErrorTypeParam message:@"翻译的文本为空" request:nil]); + completion(self.result, [EZError errorWithType:EZErrorTypeParam description:@"翻译的文本为空" request:nil]); return; } @@ -293,7 +293,7 @@ - (void)translate:(NSString *)text from:(EZLanguage)from to:(EZLanguage)to compl - (void)detectText:(NSString *)text completion:(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; } @@ -311,7 +311,7 @@ - (void)detectText:(NSString *)text completion:(void (^)(EZLanguage, NSError *_N - (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; } @@ -336,7 +336,7 @@ - (void)textToAudio:(NSString *)text fromLanguage:(EZLanguage)from completion:(v - (void)ocr:(NSImage *)image from:(EZLanguage)from to:(EZLanguage)to completion:(void (^)(EZOCRResult *_Nullable result, NSError *_Nullable error))completion { if (!image) { - completion(nil, [EZError errorWithType:EZErrorTypeParam message:@"图片为空" request:nil]); + completion(nil, [EZError errorWithType:EZErrorTypeParam description:@"图片为空" request:nil]); return; } @@ -389,16 +389,16 @@ - (void)ocr:(NSImage *)image from:(EZLanguage)from to:(EZLanguage)to completion: } } [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; } @@ -438,13 +438,12 @@ - (void)ocrAndTranslate:(NSImage *)image from:(EZLanguage)from to:(EZLanguage)to - (void)queryYoudaoDictAndTranslation:(NSString *)text from:(EZLanguage)from to:(EZLanguage)to completion:(void (^)(EZQueryResult *result, NSError *_Nullable error))completion { if (!text.length) { - completion(self.result, [EZError errorWithType:EZErrorTypeParam message:@"翻译的文本为空" request:nil]); + completion(self.result, [EZError errorWithType:EZErrorTypeParam description:@"翻译的文本为空" request:nil]); return; } if (self.queryTextType == EZQueryTextTypeNone) { - self.result.errorMessage = NSLocalizedString(@"no_results_found", nil); - completion(self.result, nil); + completion(self.result, [EZError errorWithType:EZErrorTypeNoResultsFound description:nil]); return; } @@ -480,7 +479,7 @@ - (void)queryYoudaoDictAndTranslation:(NSString *)text from:(EZLanguage)from to: /// Query Youdao dict, unofficial API - (void)queryYoudaoDict:(NSString *)text from:(EZLanguage)from to:(EZLanguage)to completion:(void (^)(EZQueryResult *result, NSError *_Nullable error))completion { if (!text.length) { - completion(self.result, [EZError errorWithType:EZErrorTypeParam message:@"翻译的文本为空" request:nil]); + completion(self.result, [EZError errorWithType:EZErrorTypeParam description:@"翻译的文本为空" request:nil]); return; } @@ -499,8 +498,7 @@ - (void)queryYoudaoDict:(NSString *)text from:(EZLanguage)from to:(EZLanguage)to // If Youdao Dictionary does not support the language, try querying translate API. if (!enableDictionary || !supportQueryDictionaryLanguage || !shouldQueryDictionary) { - self.result.errorMessage = NSLocalizedString(@"no_results_found", nil); - completion(self.result, nil); + completion(self.result, [EZError errorWithType:EZErrorTypeNoResultsFound]); return; } @@ -537,7 +535,7 @@ - (void)queryYoudaoDict:(NSString *)text from:(EZLanguage)from to:(EZLanguage)to } } [reqDict setObject:responseObject ?: [NSNull null] forKey:EZTranslateErrorRequestResponseKey]; - self.result.error = [EZError errorWithType:EZErrorTypeAPI message: message request:reqDict]; + self.result.error = [EZError errorWithType:EZErrorTypeAPI description: message request:reqDict]; completion(self.result, self.result.error); } failure:^(NSURLSessionDataTask *_Nullable task, NSError *_Nonnull error) { if (error.code == NSURLErrorCancelled) { @@ -545,7 +543,7 @@ - (void)queryYoudaoDict:(NSString *)text from:(EZLanguage)from to:(EZLanguage)to } [reqDict setObject:error forKey:EZTranslateErrorRequestErrorKey]; - self.result.error = [EZError errorWithType:EZErrorTypeNetwork message: nil request:reqDict]; + self.result.error = [EZError errorWithType:EZErrorTypeNetwork description: nil request:reqDict]; completion(self.result, self.result.error); }]; @@ -611,12 +609,12 @@ - (void)youdaoWebTranslate:(NSString *)text from:(EZLanguage)from to:(EZLanguage return; } } - completion(self.result, [EZError errorWithType:EZErrorTypeAPI message:@"翻译失败" request:nil]); + completion(self.result, [EZError errorWithType:EZErrorTypeAPI description:@"翻译失败" request:nil]); } failure:^(NSURLSessionDataTask *_Nullable task, NSError *_Nonnull error) { if (error.code == NSURLErrorCancelled) { return; } - completion(self.result, [EZError errorWithType:EZErrorTypeNetwork message:@"翻译失败" request:nil]); + completion(self.result, [EZError errorWithType:EZErrorTypeNetwork description:@"翻译失败" request:nil]); }]; [self.queryModel setStopBlock:^{ @@ -632,7 +630,7 @@ - (NSString *)signWithSalt:(NSString *)salt word:(NSString *)word { // TODO: Use a stable Youdao translation API. - (void)youdaoAIDemoTranslate:(NSString *)text from:(EZLanguage)from to:(EZLanguage)to completion:(void (^)(EZQueryResult *result, NSError *_Nullable error))completion { if (!text.length) { - completion(self.result, [EZError errorWithType:EZErrorTypeParam message:@"翻译的文本为空" request:nil]); + completion(self.result, [EZError errorWithType:EZErrorTypeParam description:@"翻译的文本为空" request:nil]); return; } @@ -799,10 +797,10 @@ - (void)youdaoAIDemoTranslate:(NSString *)text from:(EZLanguage)from to:(EZLangu } } [reqDict setObject:responseObject ?: [NSNull null] forKey:EZTranslateErrorRequestResponseKey]; - completion(self.result, [EZError errorWithType:EZErrorTypeAPI message: message request:reqDict]); + completion(self.result, [EZError errorWithType:EZErrorTypeAPI description: message request:reqDict]); } failure:^(NSURLSessionDataTask *_Nullable task, NSError *_Nonnull error) { [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]); }]; } @@ -822,7 +820,7 @@ - (void)requestYoudaoCookie { - (void)webViewTranslate:(nonnull void (^)(EZQueryResult *, NSError *_Nullable))completion { NSString *wordLink = [self wordLink:self.queryModel]; if ([wordLink isEqualToString:kYoudaoTranslatetURL]) { - NSError *error = [EZError errorWithType:EZErrorTypeUnsupportedLanguage message:nil request:nil]; + NSError *error = [EZError errorWithType:EZErrorTypeUnsupportedLanguage description:nil request:nil]; completion(self.result, error); return; } diff --git a/Easydict/Feature/ViewController/View/ResultView/EZResultView.m b/Easydict/Feature/ViewController/View/ResultView/EZResultView.m index 264b36d1f..f1d07d94f 100644 --- a/Easydict/Feature/ViewController/View/ResultView/EZResultView.m +++ b/Easydict/Feature/ViewController/View/ResultView/EZResultView.m @@ -315,7 +315,7 @@ - (void)updateAllButtonStatus { - (void)updateErrorImage { BOOL hideWarningImage = YES; - if (!self.result.hasTranslatedResult && (self.result.error.type || self.result.errorMessage.length)) { + if (!self.result.hasTranslatedResult && self.result.error.type) { hideWarningImage = NO; } self.errorImageView.hidden = hideWarningImage; diff --git a/Easydict/Feature/ViewController/View/WordResultView/EZWordResultView.m b/Easydict/Feature/ViewController/View/WordResultView/EZWordResultView.m index fb29933fe..f35589ac5 100644 --- a/Easydict/Feature/ViewController/View/WordResultView/EZWordResultView.m +++ b/Easydict/Feature/ViewController/View/WordResultView/EZWordResultView.m @@ -79,11 +79,13 @@ - (void)refreshWithResult:(EZQueryResult *)result { NSFont *typeTextFont = [NSFont systemFontOfSize:13 weight:NSFontWeightMedium]; NSFont *textFont = typeTextFont; - NSString *errorDescription = result.error.localizedDescription; - if (result.errorMessage.length) { - errorDescription = [errorDescription stringByAppendingFormat:@"\n\n%@", result.errorMessage]; + EZError *error = result.error; + NSString *errorDescription = error.localizedDescription; + NSString *errorDataMessage = error.errorDataMessage; + if (errorDataMessage) { + errorDescription = [errorDescription stringByAppendingFormat:@"\n\n%@", errorDataMessage]; if (!errorDescription && !result.hasTranslatedResult) { - errorDescription = result.errorMessage; + errorDescription = errorDataMessage; } }