mirror of
https://github.com/RevenueCat/purchases-ios
synced 2026-11-11 22:55:55 +05:45
This is the last change from what was started with #1437. That PR introduced a few private error types, but for the errors returned in the public `APIs` we were still passing around `Error`s everywhere, which didn't provide any compile-time guarantees. ### Goals: - Ensure that public APIs *only* return `ErrorCode`s wrapped with the additional `userInfo`, and not accidentally return other types like `SKError`, `BackendError`, `ErrorCode` directly, etc. - Guarantee type-safety when passing errors around within our internal implementations instead of assuming certain thrown errors are of a particular type. ### Changes: - Created `PurchasesError` for wrapping an `ErrorCode` with the additional `userInfo`. - Changed returned errors from `Error` to `NSError`. This is a change in the API, but the new type is a more specific `Error` so it shouldn't lead to any changes in client apps. - Because `PurchasesError` isn't implicitly convertible to `NSError`, this ensures that we don't return those values directly, and instead use `PurchasesError.asPublicError`. - Changed `ErrorCodeConvertible` to a now more precise `PurchasesErrorConvertible`. Thanks to this new type-safety this PR fixes at least 3 bugs where we were returning private errors instead of `ErrorCode`s. ### Testing: Thanks to #1871 we know that the returned errors are still convertible to `ErrorCode` so users can `switch` over them.
68 lines
1.9 KiB
Swift
68 lines
1.9 KiB
Swift
//
|
|
// Copyright RevenueCat Inc. All Rights Reserved.
|
|
//
|
|
// Licensed under the MIT License (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// https://opensource.org/licenses/MIT
|
|
//
|
|
// PurchasesError.swift
|
|
//
|
|
// Created by Nacho Soto on 8/31/22.
|
|
|
|
import Foundation
|
|
|
|
/// An error returned by a `RevenueCat` public API.
|
|
public typealias PublicError = NSError
|
|
|
|
/// An internal error representation, containing an `ErrorCode` and additional `userInfo`.
|
|
///
|
|
/// `ErrorCode` is essentially only domain (`ErrorCode.domain`) and a code, but can't contain any more information
|
|
/// unless it's converted into an `NSError`.
|
|
/// This serves that same purpose, but allows us to pass these around in a type-safe manner,
|
|
/// being able to distinguish them from any other `NSError`.
|
|
internal struct PurchasesError: Error {
|
|
|
|
let error: ErrorCode
|
|
let userInfo: [String: Any]
|
|
|
|
}
|
|
|
|
extension PurchasesError {
|
|
|
|
/// Converts this error into an error that can be used in a public API.
|
|
/// The error returned by this can be converted to ``ErrorCode``.
|
|
/// Example:
|
|
/// ```
|
|
/// let error = ErrorUtils.unknownError().asPublicError
|
|
/// let errorCode = error as? ErrorCode
|
|
/// ```
|
|
var asPublicError: PublicError {
|
|
return NSError(domain: Self.errorDomain, code: self.errorCode, userInfo: self.userInfo)
|
|
}
|
|
|
|
}
|
|
|
|
// MARK: -
|
|
|
|
extension PurchasesError: CustomNSError {
|
|
|
|
static let errorDomain: String = ErrorCode.errorDomain
|
|
|
|
var errorCode: Int { return (self.error as NSError).code }
|
|
var errorUserInfo: [String: Any] { return self.userInfo }
|
|
|
|
}
|
|
|
|
// MARK: -
|
|
|
|
extension PurchasesError {
|
|
|
|
/// Overload of the default initializer with `NSError.UserInfoKey` as user info key type.
|
|
init(error: ErrorCode, userInfo: [NSError.UserInfoKey: Any]) {
|
|
self.init(error: error, userInfo: userInfo as [String: Any])
|
|
}
|
|
|
|
}
|