Skip to content

Commit 2f5bbdd

Browse files
authored
Merge pull request #314 from zapcannon87/developer
refactor(error): formatted description
2 parents 28f07de + 0c222bc commit 2f5bbdd

File tree

1 file changed

+45
-19
lines changed

1 file changed

+45
-19
lines changed

Sources/Foundation/Error.swift

Lines changed: 45 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,19 @@
88

99
import Foundation
1010

11+
/// LeanCloud SDK Defining Error
1112
public struct LCError: Error {
1213
public typealias UserInfo = [String: Any]
1314

1415
public let code: Int
1516
public let reason: String?
1617
public let userInfo: UserInfo?
1718

18-
/// Underlying error.
19+
/// underlying error, only when `code` equals to `9977`, then this property is non-nil.
1920
public private(set) var underlyingError: Error?
2021

2122
/// ref: https://github.com/leancloud/rfcs/wiki/SDK-Internal-Error-Definition
22-
enum InternalErrorCode: Int {
23+
public enum InternalErrorCode: Int {
2324
// session/client
2425
case commandTimeout = 9000
2526
case connectionLost = 9001
@@ -38,7 +39,7 @@ public struct LCError: Error {
3839
case inconsistency = 9976
3940
case underlyingError = 9977
4041

41-
var description: String? {
42+
var message: String? {
4243
switch self {
4344
case .commandTimeout:
4445
return "Out command timeout"
@@ -71,8 +72,9 @@ public struct LCError: Error {
7172
}
7273
}
7374
}
74-
75-
enum ServerErrorCode: Int {
75+
76+
/// ref: https://leancloud.cn/docs/error_code.html
77+
public enum ServerErrorCode: Int {
7678
case objectNotFound = 101
7779
case sessionConflict = 4111
7880
case sessionTokenExpired = 4112
@@ -85,7 +87,7 @@ public struct LCError: Error {
8587

8688
- parameter error: The error to be converted.
8789
*/
88-
init(error: Error) {
90+
public init(error: Error) {
8991
if let error = error as? LCError {
9092
self = error
9193
} else {
@@ -102,40 +104,64 @@ public struct LCError: Error {
102104
}
103105

104106
init(code: InternalErrorCode, reason: String? = nil, userInfo: UserInfo? = nil) {
105-
self = LCError(code: code.rawValue, reason: (reason ?? code.description), userInfo: userInfo)
107+
self = LCError(code: code.rawValue, reason: (reason ?? code.message), userInfo: userInfo)
106108
}
107109

108110
init(code: ServerErrorCode, reason: String? = nil, userInfo: UserInfo? = nil) {
109111
self = LCError(code: code.rawValue, reason: reason, userInfo: userInfo)
110112
}
111113
}
112114

113-
extension LCError: LocalizedError {
115+
extension LCError: CustomStringConvertible, CustomDebugStringConvertible {
114116

115-
public var failureReason: String? {
116-
return reason ?? underlyingError?.localizedDescription
117+
public var description: String {
118+
var description = "\(LCError.self)(code: \(self.code)"
119+
if let reason = self.reason {
120+
description += ", reason: \"\(reason)\""
121+
}
122+
if let userInfo = self.userInfo {
123+
description += ", userInfo: \(userInfo)"
124+
}
125+
if let underlyingError = self.underlyingError {
126+
description += ", underlyingError: \(underlyingError)"
127+
}
128+
return "\(description))"
117129
}
118130

131+
public var debugDescription: String {
132+
return self.description
133+
}
119134
}
120135

121-
extension LCError: CustomNSError {
136+
extension LCError: LocalizedError {
137+
138+
public var errorDescription: String? {
139+
return self.description
140+
}
141+
142+
public var failureReason: String? {
143+
return self.reason
144+
?? self.underlyingError?.localizedDescription
145+
}
146+
}
122147

148+
extension LCError: CustomNSError {
149+
123150
public static var errorDomain: String {
124151
return String(describing: self)
125152
}
126-
153+
154+
public var errorCode: Int {
155+
return self.code
156+
}
157+
127158
public var errorUserInfo: [String : Any] {
128-
if let userInfo = userInfo {
159+
if let userInfo = self.userInfo {
129160
return userInfo
130-
} else if let underlyingError = underlyingError {
161+
} else if let underlyingError = self.underlyingError {
131162
return (underlyingError as NSError).userInfo
132163
} else {
133164
return [:]
134165
}
135166
}
136-
137-
public var errorCode: Int {
138-
return code
139-
}
140-
141167
}

0 commit comments

Comments
 (0)