Files
purchases-ios/Tests/UnitTests/Networking/BaseErrorTests.swift
T
NachoSoto d4ea390970 Introduced type-safe PurchasesError errors and fixed some incorrect returned error types (#1879)
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.
2022-09-05 13:46:07 -07:00

61 lines
1.8 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
//
// BaseErrorTests.swift
//
// Created by Nacho Soto on 4/7/22.
// swiftlint:disable multiline_parameters
import Nimble
@testable import RevenueCat
import XCTest
class BaseErrorTests: TestCase {
/// Compares the result of calling `asPurchasesError` on a `PurchasesErrorConvertible`
/// against the expected `ErrorCode`.
final func verifyPurchasesError(
_ error: PurchasesErrorConvertible,
expectedCode: ErrorCode,
underlyingError: Error? = nil,
userInfoKeys: [NSError.UserInfoKey]? = nil,
file: FileString = #file, line: UInt = #line
) {
let nsError = error.asPurchasesError as NSError
expect(
file: file, line: line,
nsError.domain
) == RCPurchasesErrorCodeDomain
expect(
file: file, line: line,
nsError.code
) == expectedCode.rawValue
if let underlyingError = underlyingError {
expect(
file: file, line: line,
nsError.userInfo[NSUnderlyingErrorKey] as? NSError
).to(matchError(underlyingError),
description: "Invalid underlying error")
} else {
expect(
file: file, line: line,
nsError.userInfo[NSUnderlyingErrorKey]
).to(beNil(), description: "Expected no underlying error")
}
if let userInfoKeys = userInfoKeys {
expect(nsError.userInfo.keys).to(contain(userInfoKeys as [String]))
}
}
}