77
88import Foundation
99
10- public enum MatrixError : String , Error , Codable {
10+ public enum MatrixCommonErrorCode : String , Error , Codable {
1111 case Forbidden = " M_FORBIDDEN "
1212 case Unknown = " M_UNKNOWN "
1313 case UnknownToken = " M_UNKNOWN_TOKEN "
@@ -42,9 +42,86 @@ public enum MatrixError: String, Error, Codable {
4242 case InvalidParam = " M_INVALID_PARAM "
4343}
4444
45+ public struct MatrixErrorCode : RawRepresentable , Error , Codable {
46+ var common : MatrixCommonErrorCode ?
47+ var string : String ?
48+
49+ public var rawValue : String {
50+ if let common = common {
51+ return common. rawValue
52+ }
53+ return string!
54+ }
55+
56+ public init ? ( rawValue: String ) {
57+ self . common = MatrixCommonErrorCode ( rawValue: rawValue)
58+ if common == nil {
59+ self . string = rawValue
60+ }
61+ }
62+
63+ public init ( _ common: MatrixCommonErrorCode ) {
64+ self . common = common
65+ }
66+ }
67+
68+ extension MatrixErrorCode {
69+ public init ( from decoder: Decoder ) throws {
70+ let container = try decoder. singleValueContainer ( )
71+ let rawValue = try container. decode ( String . self)
72+ self . init ( rawValue: rawValue) !
73+ }
74+
75+ public func encode( to encoder: Encoder ) throws {
76+ var container = encoder. singleValueContainer ( )
77+ try container. encode ( self . rawValue)
78+ }
79+ }
80+
81+ extension MatrixErrorCode : ExpressibleByStringLiteral {
82+ public init ( stringLiteral value: StringLiteralType ) {
83+ self . init ( rawValue: value) !
84+ }
85+ }
86+
87+ public extension MatrixErrorCode {
88+ static let Forbidden = MatrixErrorCode ( . Forbidden)
89+ static let Unknown = MatrixErrorCode ( . Unknown)
90+ static let UnknownToken = MatrixErrorCode ( . UnknownToken)
91+ static let BadJSON = MatrixErrorCode ( . BadJSON)
92+ static let NotFound = MatrixErrorCode ( . NotFound)
93+ static let LimitExceeded = MatrixErrorCode ( . LimitExceeded)
94+ static let UserInUse = MatrixErrorCode ( . UserInUse)
95+ static let RoomInUse = MatrixErrorCode ( . RoomInUse)
96+ static let BadPagination = MatrixErrorCode ( . BadPagination)
97+ static let Unauthorized = MatrixErrorCode ( . Unauthorized)
98+ static let OldVersion = MatrixErrorCode ( . OldVersion)
99+ static let Unrecognized = MatrixErrorCode ( . Unrecognized)
100+ static let LoginEmailURLNotYet = MatrixErrorCode ( . LoginEmailURLNotYet)
101+ static let ThreePIDAuthFailed = MatrixErrorCode ( . ThreePIDAuthFailed)
102+ static let ThreePIDInUse = MatrixErrorCode ( . ThreePIDInUse)
103+ static let ThreePIDNotFound = MatrixErrorCode ( . ThreePIDNotFound)
104+ static let ServerNotTrusted = MatrixErrorCode ( . ServerNotTrusted)
105+ static let GuestAccessForbidden = MatrixErrorCode ( . GuestAccessForbidden)
106+ static let ConsentNotGiven = MatrixErrorCode ( . ConsentNotGiven)
107+ static let ResourceLimitExceeded = MatrixErrorCode ( . ResourceLimitExceeded)
108+ static let BackupWrongKeysVersion = MatrixErrorCode ( . BackupWrongKeysVersion)
109+ static let PasswordTooShort = MatrixErrorCode ( . PasswordTooShort)
110+ static let PasswordNoDigit = MatrixErrorCode ( . PasswordNoDigit)
111+ static let PasswordNoUppercase = MatrixErrorCode ( . PasswordNoUppercase)
112+ static let PasswordNoLowercase = MatrixErrorCode ( . PasswordNoLowercase)
113+ static let PasswordNoSymbol = MatrixErrorCode ( . PasswordNoSymbol)
114+ static let PasswordInDictionary = MatrixErrorCode ( . PasswordInDictionary)
115+ static let PasswordWeak = MatrixErrorCode ( . PasswordWeak)
116+ static let TermsNotSigned = MatrixErrorCode ( . TermsNotSigned)
117+ static let InvalidPepper = MatrixErrorCode ( . InvalidPepper)
118+ static let Exclusive = MatrixErrorCode ( . Exclusive)
119+ static let InvalidParam = MatrixErrorCode ( . InvalidParam)
120+ }
121+
45122public struct MatrixServerError : Error , Codable {
46123 /// Error code
47- public var errcode : MatrixError
124+ public var errcode : MatrixErrorCode
48125
49126 /// Error message reported by the server
50127 public var error : String
@@ -57,7 +134,25 @@ public struct MatrixServerError: Error, Codable {
57134 public init ( json: Data , code: Int ? = nil ) throws {
58135 let decoder = JSONDecoder ( )
59136
60- self = try decoder. decode ( Self . self, from: json)
137+ do {
138+ self = try decoder. decode ( Self . self, from: json)
139+ } catch {
140+ throw MatrixInvalidError ( data: json, code: code)
141+ }
61142 self . code = code
62143 }
63144}
145+
146+ public struct MatrixInvalidError : Error , LocalizedError {
147+ public var data : Data
148+ public var code : Int ?
149+
150+ public init ( data: Data , code: Int ? = nil ) {
151+ self . data = data
152+ self . code = code
153+ }
154+
155+ var localisedDescription : String {
156+ NSLocalizedString ( " Failed to parse error result for code \( code ?? - 1 ) " , comment: " MatrixInvalidError " )
157+ }
158+ }
0 commit comments