From 8182cb4a3762ad1d1f885e10de7ed8a6d849fbd3 Mon Sep 17 00:00:00 2001 From: yaochenfeng <282696845@qq.com> Date: Sun, 15 Jun 2025 15:46:31 +0800 Subject: [PATCH 1/2] =?UTF-8?q?feat:=20=E5=A4=84=E7=90=86ServiceError?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Sources/DFService/ServiceError.swift | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/Sources/DFService/ServiceError.swift b/Sources/DFService/ServiceError.swift index fad26c7..62e5369 100644 --- a/Sources/DFService/ServiceError.swift +++ b/Sources/DFService/ServiceError.swift @@ -8,11 +8,19 @@ public enum ServiceError: Error { /// - Parameters: /// - code: 错误代码 /// - message: 错误消息 - /// - debugMessage: 可选的调试消息,默认为 nil - /// - Example: `ServiceError.api(code: 404, message: "Not Found")` - /// - Example: `ServiceError.api(code: 500, message: "Internal Server Error", debugMessage: "Database connection failed")` - case api(code: Int, message: String, debugMessage: String? = nil) + /// - detail: 可选的调试消息,默认为 nil + case api(code: Int, message: String, detail: String? = nil) /// 未实现 case notImplemented(String = #fileID + " " + #function) + /// 自定义错误 + case custom(Error) + + public static func from(_ error: Error) -> ServiceError { + if let serviceError = error as? ServiceError { + return serviceError + } else { + return .custom(error) + } + } } From 75974b5befc56052773ed2d05868b5cd3ceb9990 Mon Sep 17 00:00:00 2001 From: yaochenfeng <282696845@qq.com> Date: Sun, 15 Jun 2025 16:05:21 +0800 Subject: [PATCH 2/2] =?UTF-8?q?feat:=20ServiceError=20=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E9=94=99=E8=AF=AF=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Sources/DFService/ServiceError.swift | 42 +++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/Sources/DFService/ServiceError.swift b/Sources/DFService/ServiceError.swift index 62e5369..75d236c 100644 --- a/Sources/DFService/ServiceError.swift +++ b/Sources/DFService/ServiceError.swift @@ -3,6 +3,9 @@ /// /// - api: Indicates an API error with an associated error code and message. /// - notImplemented: Indicates that a feature or method is not yet implemented. The default value includes the file and function name. + +import Foundation + public enum ServiceError: Error { /// API 错误,包含错误代码和消息 /// - Parameters: @@ -12,7 +15,7 @@ public enum ServiceError: Error { case api(code: Int, message: String, detail: String? = nil) /// 未实现 - case notImplemented(String = #fileID + " " + #function) + case notImplemented(String = #function) /// 自定义错误 case custom(Error) @@ -24,3 +27,40 @@ public enum ServiceError: Error { } } } + +/// 扩展 ServiceError 以提供错误描述 +extension ServiceError: CustomStringConvertible, CustomDebugStringConvertible { + public var errorCode: Int { + switch self { + case .api(let code, _, _): + return code + case .notImplemented: + return 0 // 未实现的错误没有特定的错误代码 + case .custom(let error): + return (error as NSError).code // 使用 NSError 的代码 + } + } + public var description: String { + switch self { + case .api(_, let message, _): + return message + case .notImplemented(let location): + return "未实现: \(location)" + case .custom(let error): + return error.localizedDescription + } + } + + public var debugDescription: String { + + switch self { + case .api(let code, let message, let detail): + return + "ServiceError api - Code: \(code), Message: \(message), Detail: \(detail ?? "No detail")" + case .notImplemented(let location): + return "ServiceError - Location: \(location)" + case .custom(let error): + return "ServiceError - \(error.localizedDescription)" + } + } +}