Skip to content

Commit 2a5a577

Browse files
authored
PR-4962: Error cases changes (#11)
* feat: added new error code convention * feat: major version bump * Trigger Build * Trigger Build
1 parent 9f18992 commit 2a5a577

File tree

2 files changed

+31
-7
lines changed

2 files changed

+31
-7
lines changed

PayrailsCSE.podspec

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
Pod::Spec.new do |s|
1010
s.name = 'PayrailsCSE'
11-
s.version = '0.2.0'
11+
s.version = '1.0.0'
1212
s.summary = 'Payrails client-side encryption SDK'
1313

1414
# This description is used to generate tags and improve search results.

Sources/PayrailsCSE/PayrailsCSE.swift

+30-6
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,13 @@ public struct TokenizeResponse: Codable {
1313
public let errors: [PayrailsError]?
1414
}
1515

16+
17+
1618
public struct PayrailsCSE {
1719
var cseConfig: CSEConfiguration?
1820

19-
public init(data: String, version: String) {
20-
let config = parseConfig(data: data)
21+
public init(data: String, version: String) throws {
22+
let config = try parseConfig(data: data)
2123
cseConfig = config
2224
}
2325

@@ -117,13 +119,27 @@ public struct PayrailsCSE {
117119
task.resume()
118120
}
119121

120-
private func parseConfig(data: String) -> CSEConfiguration {
122+
private func parseConfig(data: String) throws -> CSEConfiguration {
121123
guard let decodedData = Data(base64Encoded: data) else {
122-
fatalError("Failed to decode Base64 data")
124+
throw NSError(
125+
domain: "payrails:client.cse",
126+
code: 0,
127+
userInfo: [
128+
"code": "payrails:client.cse:configuration.malformed",
129+
"detail": "The provided configuration string is invalid. Please ensure it matches the required format and structure as defined in the documentation",
130+
"docUrl": "https://docs.payrails.com/docs/sdk#initializing-the-sdk"
131+
])
123132
}
124133

125134
guard let config = try? JSONDecoder().decode(CSEConfiguration.self, from: decodedData) else {
126-
fatalError("Failed to parse CSEConfiguration")
135+
throw NSError(
136+
domain: "payrails:client.cse",
137+
code: 0,
138+
userInfo: [
139+
"code": "payrails:client.cse:configuration.malformed",
140+
"detail": "The provided configuration string is invalid. Please ensure it matches the required format and structure as defined in the documentation",
141+
"docUrl": "https://docs.payrails.com/docs/sdk#initializing-the-sdk"
142+
])
127143
}
128144

129145
return config
@@ -146,7 +162,15 @@ public struct PayrailsCSE {
146162
]
147163

148164
guard let publicKeyRef = SecKeyCreateWithData(publicKeyData as CFData, options as CFDictionary, &error) else {
149-
fatalError("Failed to create public key: \(error!)")
165+
debugPrint("Failed to create public key: \(error!)")
166+
throw NSError(
167+
domain: "payrails:client.cse",
168+
code: 0,
169+
userInfo: [
170+
"code": "payrails:client.cse:configuration.invalid",
171+
"detail": "The provided configuration is missing the public key required for encryption",
172+
"docUrl": "https://docs.payrails.com/docs/tokenize-cards-with-client-side-encryption",
173+
])
150174
}
151175

152176
return publicKeyRef

0 commit comments

Comments
 (0)