Skip to content

Commit

Permalink
PR-4962: Error cases changes (#11)
Browse files Browse the repository at this point in the history
* feat: added new error code convention

* feat: major version bump

* Trigger Build

* Trigger Build
  • Loading branch information
grgmgd authored Feb 27, 2024
1 parent 9f18992 commit 2a5a577
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
2 changes: 1 addition & 1 deletion PayrailsCSE.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

Pod::Spec.new do |s|
s.name = 'PayrailsCSE'
s.version = '0.2.0'
s.version = '1.0.0'
s.summary = 'Payrails client-side encryption SDK'

# This description is used to generate tags and improve search results.
Expand Down
36 changes: 30 additions & 6 deletions Sources/PayrailsCSE/PayrailsCSE.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ public struct TokenizeResponse: Codable {
public let errors: [PayrailsError]?
}



public struct PayrailsCSE {
var cseConfig: CSEConfiguration?

public init(data: String, version: String) {
let config = parseConfig(data: data)
public init(data: String, version: String) throws {
let config = try parseConfig(data: data)
cseConfig = config
}

Expand Down Expand Up @@ -117,13 +119,27 @@ public struct PayrailsCSE {
task.resume()
}

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

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

return config
Expand All @@ -146,7 +162,15 @@ public struct PayrailsCSE {
]

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

return publicKeyRef
Expand Down

0 comments on commit 2a5a577

Please sign in to comment.