From 2a5a577d236449f9986f34f8555c24b7fb86bbb5 Mon Sep 17 00:00:00 2001 From: George Maged Date: Tue, 27 Feb 2024 19:39:26 +0200 Subject: [PATCH] PR-4962: Error cases changes (#11) * feat: added new error code convention * feat: major version bump * Trigger Build * Trigger Build --- PayrailsCSE.podspec | 2 +- Sources/PayrailsCSE/PayrailsCSE.swift | 36 ++++++++++++++++++++++----- 2 files changed, 31 insertions(+), 7 deletions(-) diff --git a/PayrailsCSE.podspec b/PayrailsCSE.podspec index 85c8c66..cac5cf8 100644 --- a/PayrailsCSE.podspec +++ b/PayrailsCSE.podspec @@ -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. diff --git a/Sources/PayrailsCSE/PayrailsCSE.swift b/Sources/PayrailsCSE/PayrailsCSE.swift index cc61bec..ce18c50 100644 --- a/Sources/PayrailsCSE/PayrailsCSE.swift +++ b/Sources/PayrailsCSE/PayrailsCSE.swift @@ -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 } @@ -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 @@ -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