Files
purchases-ios/Sources/Networking/HTTPClient/HTTPResponse.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

184 lines
5.4 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
//
// HTTPResponse.swift
//
// Created by César de la Vega on 4/19/21.
//
import Foundation
struct HTTPResponse<Body: HTTPResponseBody> {
typealias Result = Swift.Result<Self, NetworkError>
let statusCode: HTTPStatusCode
let body: Body
}
extension HTTPResponse: CustomStringConvertible {
var description: String {
if let bodyDescription = (self.body as? CustomStringConvertible)?.description {
return "HTTPResponse(statusCode: \(self.statusCode.rawValue), body: \(bodyDescription))"
} else {
return "HTTPResponse(statusCode: \(self.statusCode.rawValue), body: \(type(of: self.body))"
}
}
}
extension HTTPResponse where Body: OptionalType, Body.Wrapped: HTTPResponseBody {
/// Converts a `HTTPResponse<Body?>` into a `HTTPResponse<Body>?`
var asOptionalResponse: HTTPResponse<Body.Wrapped>? {
guard let body = self.body.asOptional else {
return nil
}
return .init(statusCode: self.statusCode, body: body)
}
}
extension HTTPResponse {
func mapBody<NewBody>(_ mapping: (Body) throws -> NewBody) rethrows -> HTTPResponse<NewBody> {
return .init(statusCode: self.statusCode, body: try mapping(self.body))
}
}
// MARK: -
/// The response content of a failed request.
struct ErrorResponse: Equatable {
var code: BackendErrorCode
var message: String?
var attributeErrors: [String: String] = [:]
}
extension ErrorResponse {
/// Converts this `ErrorResponse` into an `ErrorCode` backed by the corresponding `BackendErrorCode`.
func asBackendError(
with statusCode: HTTPStatusCode,
file: String = #fileID,
function: String = #function,
line: UInt = #line
) -> PurchasesError {
var userInfo: [NSError.UserInfoKey: Any] = [
.statusCode: statusCode.rawValue
]
if !self.attributeErrors.isEmpty {
userInfo[.attributeErrors] = self.attributeErrors
}
return ErrorUtils.backendError(
withBackendCode: self.code,
message: self.attributeErrors.isEmpty
? nil
: self.attributeErrors.description,
backendMessage: self.message,
extraUserInfo: userInfo,
fileName: file, functionName: function, line: line
)
}
}
extension ErrorResponse: Decodable {
private enum CodingKeys: String, CodingKey {
case code
case message
case attributeErrors
}
private struct AttributeError: Decodable {
let keyName: String
let message: String
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
let codeAsInteger = try? container.decodeIfPresent(Int.self, forKey: .code)
let codeAsString = try? container.decodeIfPresent(String.self, forKey: .code)
self.code = BackendErrorCode(code: codeAsInteger ?? codeAsString)
self.message = try container.decodeIfPresent(String.self, forKey: .message)
let attributeErrors = (
try? container.decodeIfPresent(Array<AttributeError>.self,
forKey: .attributeErrors)
) ?? []
self.attributeErrors = attributeErrors
.dictionaryAllowingDuplicateKeys { $0.keyName }
.mapValues { $0.message }
}
}
extension ErrorResponse {
/// For some endpoints the backend may return `ErrorResponse` inside of this wrapper.
private struct Wrapper: Decodable {
let attributesErrorResponse: ErrorResponse
}
private static func parseWrapper(_ data: Data) -> Wrapper? {
return try? JSONDecoder.default.decode(jsonData: data, logErrors: false)
}
/// Creates an `ErrorResponse` with the content of an `HTTPResponse`.
/// This method supports extracting error information from the root, or from inside `"attributes_error_response"`
/// - Note: if the error couldn't be decoded, a default error is created.
/// - Warning: this is "deprecated". Ideally in the future all `ErrorResponses` are created from `Data`.
static func from(_ dictionary: [String: Any]) -> Self {
guard let data = try? JSONSerialization.data(withJSONObject: dictionary) else {
return .defaultResponse
}
return Self.from(data)
}
/// Creates an `ErrorResponse` with the content of an `HTTPResponse`.
/// This method supports extracting error information from the root, or from inside `"attributes_error_response"`
/// - Note: if the error couldn't be decoded, a default error is created.
static func from(_ data: Data) -> Self {
do {
if let wrapper = Self.parseWrapper(data) {
return wrapper.attributesErrorResponse
} else {
return try JSONDecoder.default.decode(jsonData: data)
}
} catch {
Logger.error(Strings.codable.decoding_error(error))
return Self.defaultResponse
}
}
private static let defaultResponse: Self = .init(code: .unknownError,
message: nil)
}