From 973a0c4c4f73c689409be30a72ea5707b161a9d9 Mon Sep 17 00:00:00 2001 From: NachoSoto Date: Fri, 14 Oct 2022 10:25:01 -0700 Subject: [PATCH] Fixed iOS 12 tests --- Sources/Error Handling/ErrorUtils.swift | 40 ++++++++++++++++++------- 1 file changed, 30 insertions(+), 10 deletions(-) diff --git a/Sources/Error Handling/ErrorUtils.swift b/Sources/Error Handling/ErrorUtils.swift index 1d756f397c..dea775db6e 100644 --- a/Sources/Error Handling/ErrorUtils.swift +++ b/Sources/Error Handling/ErrorUtils.swift @@ -290,16 +290,36 @@ enum ErrorUtils { withUntypedError error: Error, fileName: String = #fileID, functionName: String = #function, line: UInt = #line ) -> PurchasesError { - switch error { - case let purchasesError as PurchasesError: - return purchasesError - case let convertible as PurchasesErrorConvertible: - return convertible.asPurchasesError - default: - return ErrorUtils.unknownError( - error: error, - fileName: fileName, functionName: functionName, line: line - ) + if #available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.2, *) { + switch error { + case let purchasesError as PurchasesError: + return purchasesError + // This line crashes on iOS 12.x only (see https://github.com/RevenueCat/purchases-ios/pull/1982). + case let convertible as PurchasesErrorConvertible: + return convertible.asPurchasesError + default: + return ErrorUtils.unknownError( + error: error, + fileName: fileName, functionName: functionName, line: line + ) + } + } else { + switch error { + case let purchasesError as PurchasesError: + return purchasesError + // `as PurchasesErrorConvertible` crashes on iOS 12.x, so these explicit casts are a workaround. + // We can't guarantee that these are exhaustive, but at least this covers the most common cases, + // and the `default` below ensures that we don't crash for the rest. + case let backendError as BackendError: + return backendError.asPurchasesError + case let networkError as NetworkError: + return networkError.asPurchasesError + default: + return ErrorUtils.unknownError( + error: error, + fileName: fileName, functionName: functionName, line: line + ) + } } }