From ac8a4ae5ac1154d0dd5a1c142d1b1006fd68d0f9 Mon Sep 17 00:00:00 2001 From: tisfeng Date: Fri, 26 Jan 2024 00:30:14 +0800 Subject: [PATCH] perf: improve error message for Gemini --- Easydict.xcodeproj/project.pbxproj | 12 +++++++++ .../Service/Gemini/GeminiService.swift | 13 +++++++++- .../Extensions/String/String+Regex.swift | 26 +++++++++++++++++++ 3 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 Easydict/NewApp/Utility/Extensions/String/String+Regex.swift diff --git a/Easydict.xcodeproj/project.pbxproj b/Easydict.xcodeproj/project.pbxproj index 1b991b984..5d2a48f3b 100644 --- a/Easydict.xcodeproj/project.pbxproj +++ b/Easydict.xcodeproj/project.pbxproj @@ -84,6 +84,7 @@ 03882F9029D95044005B5A52 /* ToastWindowController.xib in Resources */ = {isa = PBXBuildFile; fileRef = 03882F8829D95044005B5A52 /* ToastWindowController.xib */; }; 03882F9129D95044005B5A52 /* CTCommon.m in Sources */ = {isa = PBXBuildFile; fileRef = 03882F8929D95044005B5A52 /* CTCommon.m */; }; 03882F9229D95044005B5A52 /* Info.plist in Resources */ = {isa = PBXBuildFile; fileRef = 03882F8C29D95044005B5A52 /* Info.plist */; }; + 038A72402B62C0B9004995E3 /* String+Regex.swift in Sources */ = {isa = PBXBuildFile; fileRef = 038A723F2B62C0B9004995E3 /* String+Regex.swift */; }; 038EA1AA2B41169C008A6DD1 /* ZipArchive in Frameworks */ = {isa = PBXBuildFile; productRef = 038EA1A92B41169C008A6DD1 /* ZipArchive */; }; 038EA1AD2B41282F008A6DD1 /* MJExtension in Frameworks */ = {isa = PBXBuildFile; productRef = 038EA1AC2B41282F008A6DD1 /* MJExtension */; }; 0396D611292C932F006A11D9 /* EZSelectLanguageCell.m in Sources */ = {isa = PBXBuildFile; fileRef = 0396D610292C932F006A11D9 /* EZSelectLanguageCell.m */; }; @@ -446,6 +447,7 @@ 03882F8A29D95044005B5A52 /* CTView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CTView.h; sourceTree = ""; }; 03882F8B29D95044005B5A52 /* CoolToast.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CoolToast.h; sourceTree = ""; }; 03882F8C29D95044005B5A52 /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; + 038A723F2B62C0B9004995E3 /* String+Regex.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "String+Regex.swift"; sourceTree = ""; }; 0396D60F292C932F006A11D9 /* EZSelectLanguageCell.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = EZSelectLanguageCell.h; sourceTree = ""; }; 0396D610292C932F006A11D9 /* EZSelectLanguageCell.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = EZSelectLanguageCell.m; sourceTree = ""; }; 0396D613292CC4C3006A11D9 /* EZLocalStorage.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = EZLocalStorage.h; sourceTree = ""; }; @@ -1200,6 +1202,14 @@ path = Kit; sourceTree = ""; }; + 038A723E2B62C07B004995E3 /* String */ = { + isa = PBXGroup; + children = ( + 038A723F2B62C0B9004995E3 /* String+Regex.swift */, + ); + path = String; + sourceTree = ""; + }; 0396D612292CBDFD006A11D9 /* Storage */ = { isa = PBXGroup; children = ( @@ -2260,6 +2270,7 @@ EA9943E62B534D7C00EE7B97 /* Extensions */ = { isa = PBXGroup; children = ( + 038A723E2B62C07B004995E3 /* String */, EAED41F02B54B1A60005FE0A /* QueryService+ConfigurableService */, EA9943E72B534D8900EE7B97 /* LanguageDetectOptimizeExtensions.swift */, EA9943ED2B5353AB00EE7B97 /* WindowTypeExtensions.swift */, @@ -2773,6 +2784,7 @@ C415C0AD2B450D4800A9D231 /* GeminiService.swift in Sources */, 62A2D03F2A82967F007EEB01 /* EZBingRequest.m in Sources */, 03BDA7BE2A26DA280079D04F /* XPMCountedArgument.m in Sources */, + 038A72402B62C0B9004995E3 /* String+Regex.swift in Sources */, 03D35DAA2AA6C49B00B023FE /* NSString+EZRegex.m in Sources */, 03B022FE29231FA6001C7E63 /* EZBaseQueryViewController.m in Sources */, DC6D9C892B3969510055EFFC /* Appearance.swift in Sources */, diff --git a/Easydict/Feature/Service/Gemini/GeminiService.swift b/Easydict/Feature/Service/Gemini/GeminiService.swift index aa9386a91..b959b1550 100644 --- a/Easydict/Feature/Service/Gemini/GeminiService.swift +++ b/Easydict/Feature/Service/Gemini/GeminiService.swift @@ -121,7 +121,18 @@ public final class GeminiService: QueryService { result.translatedResults = [resultString] completion(result, nil) } catch { - let ezError = EZError(type: .API, description: nil, errorDataMessage: String(describing: error)) + /** + https://github.com/google/generative-ai-swift/issues/89 + + String(describing: error) + + "internalError(underlying: GoogleGenerativeAI.RPCError(httpResponseCode: 400, message: \"API key not valid. Please pass a valid API key.\", status: GoogleGenerativeAI.RPCStatus.invalidArgument))" + */ + let ezError = EZError(nsError: error) + let errorString = String(describing: error) + let errorMessage = errorString.extract(withPattern: "message: \"([^\"]*)\"") ?? errorString + ezError?.errorDataMessage = errorMessage + completion(result, ezError) } } diff --git a/Easydict/NewApp/Utility/Extensions/String/String+Regex.swift b/Easydict/NewApp/Utility/Extensions/String/String+Regex.swift new file mode 100644 index 000000000..0acccba89 --- /dev/null +++ b/Easydict/NewApp/Utility/Extensions/String/String+Regex.swift @@ -0,0 +1,26 @@ +// +// String+Regex.swift +// Easydict +// +// Created by tisfeng on 2024/1/26. +// Copyright © 2024 izual. All rights reserved. +// + +import Foundation + +extension String { + func extract(withPattern pattern: String) -> String? { + do { + let regex = try NSRegularExpression(pattern: pattern) + let range = NSRange(location: 0, length: utf16.count) + if let match = regex.firstMatch(in: self, options: [], range: range) { + if let range = Range(match.range(at: 1), in: self) { + return String(self[range]) + } + } + } catch { + print("Invalid regex: \(error.localizedDescription)") + } + return nil + } +}