-
Notifications
You must be signed in to change notification settings - Fork 316
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Introduced NetworkError
and BackendError
#1437
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,193 @@ | ||
// | ||
// 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 | ||
// | ||
// BackendError.swift | ||
// | ||
// Created by Nacho Soto on 4/7/22. | ||
|
||
// swiftlint:disable multiline_parameters | ||
|
||
import Foundation | ||
|
||
/// An `Error` produced by ``Backend``. | ||
enum BackendError: Error, Equatable { | ||
|
||
case networkError(NetworkError) | ||
case missingAppUserID(Source) | ||
case emptySubscriberAttributes(Source) | ||
case missingReceiptFile(Source) | ||
case missingTransactionProductIdentifier(Source) | ||
case unexpectedBackendResponse(UnexpectedBackendResponseError, extraContext: String?, Source) | ||
|
||
} | ||
Comment on lines
+19
to
+28
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this was one of the biggest things I was looking forward to when we started the Swift migration ❤️ |
||
|
||
extension BackendError { | ||
|
||
static func missingAppUserID( | ||
file: String = #fileID, function: String = #function, line: UInt = #line | ||
) -> Self { | ||
return .missingAppUserID(.init(file: file, function: function, line: line)) | ||
} | ||
|
||
static func emptySubscriberAttributes( | ||
file: String = #fileID, function: String = #function, line: UInt = #line | ||
) -> Self { | ||
return .emptySubscriberAttributes(.init(file: file, function: function, line: line)) | ||
} | ||
|
||
static func missingTransactionProductIdentifier( | ||
file: String = #fileID, function: String = #function, line: UInt = #line | ||
) -> Self { | ||
return .missingTransactionProductIdentifier(.init(file: file, function: function, line: line)) | ||
} | ||
|
||
static func missingReceiptFile( | ||
file: String = #fileID, function: String = #function, line: UInt = #line | ||
) -> Self { | ||
return .missingReceiptFile(.init(file: file, function: function, line: line)) | ||
} | ||
|
||
static func unexpectedBackendResponse( | ||
_ error: UnexpectedBackendResponseError, | ||
extraContext: String? = nil, | ||
file: String = #fileID, function: String = #function, line: UInt = #line | ||
) -> Self { | ||
return .unexpectedBackendResponse(error, | ||
extraContext: extraContext, | ||
.init(file: file, function: function, line: line)) | ||
} | ||
|
||
} | ||
|
||
extension BackendError: ErrorCodeConvertible { | ||
|
||
var asPurchasesError: Error { | ||
switch self { | ||
case let .networkError(error): | ||
return error.asPurchasesError | ||
|
||
case let .missingAppUserID(source): | ||
return ErrorUtils.missingAppUserIDError(fileName: source.file, | ||
functionName: source.function, | ||
line: source.line) | ||
|
||
case .emptySubscriberAttributes: | ||
return ErrorCode.emptySubscriberAttributes | ||
|
||
case let .missingReceiptFile(source): | ||
return ErrorUtils.missingReceiptFileError(fileName: source.file, | ||
functionName: source.function, | ||
line: source.line) | ||
|
||
case let .missingTransactionProductIdentifier(source): | ||
return ErrorUtils.unknownError( | ||
message: Strings.purchase.skpayment_missing_product_identifier.description, | ||
fileName: source.file, | ||
functionName: source.function, | ||
line: source.line | ||
) | ||
|
||
case let .unexpectedBackendResponse(error, extraContext, source): | ||
return ErrorUtils.unexpectedBackendResponse(withSubError: error, | ||
extraContext: extraContext, | ||
fileName: source.file, | ||
functionName: source.function, | ||
line: source.line) | ||
} | ||
} | ||
|
||
} | ||
|
||
extension BackendError: DescribableError { } | ||
|
||
extension BackendError { | ||
|
||
/// Whether the operation producing this error actually synced the data. | ||
var successfullySynced: Bool { | ||
return self.networkError?.successfullySynced ?? false | ||
Comment on lines
+112
to
+113
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yessss |
||
} | ||
|
||
/// Whether the operation producing this error can be completed. | ||
/// If `false`, the underlying error was fatal. | ||
var finishable: Bool { | ||
return self.networkError?.finishable ?? false | ||
Comment on lines
+118
to
+119
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I might cry |
||
} | ||
|
||
private var networkError: NetworkError? { | ||
switch self { | ||
case let .networkError(networkError): | ||
return networkError | ||
|
||
case .missingAppUserID, | ||
.emptySubscriberAttributes, | ||
.missingReceiptFile, | ||
.missingTransactionProductIdentifier, | ||
.unexpectedBackendResponse: | ||
return nil | ||
} | ||
} | ||
|
||
} | ||
|
||
extension BackendError { | ||
|
||
enum UnexpectedBackendResponseError: Error, Equatable { | ||
|
||
/// Login call failed due to a problem with the response. | ||
case loginResponseDecoding | ||
|
||
/// Received a bad response after posting an offer- "offers" couldn't be read from response. | ||
case postOfferIdBadResponse | ||
|
||
/// Received a bad response after posting an offer- "offers" was totally missing. | ||
case postOfferIdMissingOffersInResponse | ||
|
||
/// Received a bad response after posting an offer- there was an issue with the signature. | ||
case postOfferIdSignature | ||
|
||
/// getOffer call failed with an invalid response. | ||
case getOfferUnexpectedResponse | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. feels like the naming for "unexpected thing came from the backend" is inconsistent: I think we should settle on naming and make them the same if they represent the same concept of "the backend returned an object that we didn't expect" There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah I agree. These are the already existing |
||
|
||
/// A call that is supposed to retrieve a CustomerInfo failed because the CustomerInfo in the response was nil. | ||
case customerInfoNil | ||
|
||
/// A call that is supposed to retrieve a CustomerInfo failed because the json object couldn't be parsed. | ||
case customerInfoResponseParsing(error: NSError, json: String) | ||
} | ||
|
||
} | ||
|
||
extension BackendError.UnexpectedBackendResponseError: DescribableError { | ||
|
||
var description: String { | ||
switch self { | ||
case .loginResponseDecoding: | ||
return "Unable to decode response returned from login." | ||
case .postOfferIdBadResponse: | ||
return "Unable to decode response returned from posting offer for signing." | ||
case .postOfferIdMissingOffersInResponse: | ||
return "Missing offers from response returned from posting offer for signing." | ||
case .postOfferIdSignature: | ||
return "Signature error encountered in response returned from posting offer for signing." | ||
case .getOfferUnexpectedResponse: | ||
return "Unknown error encountered while getting offerings." | ||
case .customerInfoNil: | ||
return "Unable to instantiate a CustomerInfoResponse, CustomerInfo in response was nil." | ||
case .customerInfoResponseParsing: | ||
return "Unable to instantiate a CustomerInfoResponse due to malformed json." | ||
} | ||
} | ||
|
||
} | ||
|
||
extension BackendError { | ||
|
||
typealias Source = ErrorSource | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[not for this PR] This feels like it shouldn't be the responsibility of
Backend
, digging up a receipt, since it's even performed by an entirely different classThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah this is sent by
PurchasesOrchestrator
. I didn't want to make this even bigger than it already was, but a nice follow up would be to add something likePurchaseError
forPurchaseOrchestrator
level errors.