Skip to content

Commit

Permalink
~ Package loading error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
buresdv committed Nov 17, 2024
1 parent dad7b5c commit 319e44a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 12 deletions.
4 changes: 2 additions & 2 deletions Cork/Errors/Package Loading Error.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ enum PackageLoadingError: LocalizedError
/// When attempting to get the list of raw URLs from the folder containing the packages, the function for loading packages returned nil, therefore, an error occured
case couldNotReadContentsOfParentFolder(failureReason: String)
case failedWhileLoadingPackages(failureReason: String?)
case failedWhileLoadingCertainPackage(String, URL, failureReason: String)
case failedWhileLoadingCertainPackage(String, URL, failureReason: String, associatedError: Error?)
case packageDoesNotHaveAnyVersionsInstalled(String)
case packageIsNotAFolder(String, URL)

Expand All @@ -31,7 +31,7 @@ enum PackageLoadingError: LocalizedError
{
return String(localized: "error.package-loading.could-not-load-packages")
}
case .failedWhileLoadingCertainPackage(let string, let uRL, let failureReason):
case .failedWhileLoadingCertainPackage(let string, let uRL, let failureReason, let associatedError):
return String(localized: "error.package-loading.could-not-load-\(string)-at-\(uRL.absoluteString)-because-\(failureReason)", comment: "Couldn't load package (package name) at (package URL) because (failure reason)")
case .packageDoesNotHaveAnyVersionsInstalled(let string):
return String(localized: "error.package-loading.\(string)-does-not-have-any-versions-installed")
Expand Down
6 changes: 5 additions & 1 deletion Cork/Logic/Load up Tapped Taps.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ func loadUpTappedTaps() async -> [BrewTap]
{
var finalAvailableTaps: [BrewTap] = .init()

let contentsOfTapFolder: [URL] = try! getContentsOfFolder(targetFolder: AppConstants.shared.tapPath, options: .skipsHiddenFiles)
var contentsOfTapFolder: [URL] {
let rawContents: [URL]? = try? getContentsOfFolder(targetFolder: AppConstants.shared.tapPath, options: .skipsHiddenFiles)

return rawContents ?? .init()
}

AppConstants.shared.logger.debug("Contents of tap folder: \(contentsOfTapFolder)")

Expand Down
27 changes: 18 additions & 9 deletions Cork/Logic/Package Loading/Load Up Installed Packages.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ extension BrewDataStorage
do
{
return try await self.loadInstalledPackagesFromFolder(
packageTypeToLoad: packageTypeToLoad)
packageTypeToLoad: packageTypeToLoad,
appState: appState
)
}
catch let packageLoadingError
{
Expand All @@ -47,7 +49,7 @@ extension BrewDataStorage
appState.showAlert(
errorToShow: .couldNotLoadAnyPackages(packageLoadingError))
case .failedWhileLoadingCertainPackage(
let offendingPackage, let offendingPackageURL, let failureReason
let offendingPackage, let offendingPackageURL, let failureReason, let associatedError
):
appState.showAlert(
errorToShow: .couldNotLoadCertainPackage(
Expand All @@ -70,11 +72,13 @@ extension BrewDataStorage
}
}

// MARK: - Helper Functions
private extension BrewDataStorage
{
/// Load packages from disk, and convert them into ``BrewPackage``s
func loadInstalledPackagesFromFolder(
packageTypeToLoad: PackageType
packageTypeToLoad: PackageType,
appState: AppState
) async throws(PackageLoadingError) -> Set<BrewPackage>?
{
do
Expand Down Expand Up @@ -150,6 +154,11 @@ private extension BrewDataStorage
/// Gets URL to installed versions of a package provided as ``packageURL``
/// `/opt/homebrew/Cellar/cmake/3.30.5`, `/opt/homebrew/Cellar/cmake/3.30.4`
let versionURLs: [URL] = try getContentsOfFolder(targetFolder: packageURL, options: [.skipsHiddenFiles])

guard !versionURLs.isEmpty else
{
throw PackageLoadingError.packageDoesNotHaveAnyVersionsInstalled(packageName)
}

/// Gets the name of the version, which at this stage is the last path component of the `versionURLs` URL
let versionNamesForPackage: [String] = versionURLs.map
Expand Down Expand Up @@ -177,23 +186,23 @@ private extension BrewDataStorage
switch intentionalInstallationDiscoveryError
{
case .failedToDetermineMostRelevantVersion(let packageURL):
throw PackageLoadingError.failedWhileLoadingCertainPackage(packageName, packageURL, failureReason: String(localized: "error.package-loading.could-not-load-version-to-check-from-available-versions"))
throw PackageLoadingError.failedWhileLoadingCertainPackage(packageName, packageURL, failureReason: String(localized: "error.package-loading.could-not-load-version-to-check-from-available-versions"), associatedError: intentionalInstallationDiscoveryError)
case .failedToReadInstallationRecepit(let packageURL):
throw PackageLoadingError.failedWhileLoadingCertainPackage(packageName, packageURL, failureReason: String(localized: "error.package-loading.could-not-convert-contents-of-install-receipt-to-data"))
throw PackageLoadingError.failedWhileLoadingCertainPackage(packageName, packageURL, failureReason: String(localized: "error.package-loading.could-not-convert-contents-of-install-receipt-to-data"), associatedError: intentionalInstallationDiscoveryError)
case .failedToParseInstallationReceipt(let packageURL):
throw PackageLoadingError.failedWhileLoadingCertainPackage(packageName, packageURL, failureReason: String(localized: "error.package-loading.could-not-decode-installa-receipt"))
throw PackageLoadingError.failedWhileLoadingCertainPackage(packageName, packageURL, failureReason: String(localized: "error.package-loading.could-not-decode-installa-receipt"), associatedError: intentionalInstallationDiscoveryError)
case .installationReceiptMissingCompletely(let packageURL):
throw PackageLoadingError.failedWhileLoadingCertainPackage(packageName, packageURL, failureReason: String(localized: "error.package-loading.missing-install-receipt"))
throw PackageLoadingError.failedWhileLoadingCertainPackage(packageName, packageURL, failureReason: String(localized: "error.package-loading.missing-install-receipt"), associatedError: intentionalInstallationDiscoveryError)
case .unexpectedFolderName(let packageURL):
throw PackageLoadingError.failedWhileLoadingCertainPackage(packageName, packageURL, failureReason: String(localized: "error.package-loading.unexpected-folder-name"))
throw PackageLoadingError.failedWhileLoadingCertainPackage(packageName, packageURL, failureReason: String(localized: "error.package-loading.unexpected-folder-name"), associatedError: intentionalInstallationDiscoveryError)
}
}
}
catch let loadingError
{
AppConstants.shared.logger.error("Failed while loading package \(packageURL.lastPathComponent, privacy: .public): \(loadingError.localizedDescription)")

throw .failedWhileLoadingCertainPackage(packageURL.lastPathComponent, packageURL, failureReason: loadingError.localizedDescription)
throw .failedWhileLoadingCertainPackage(packageURL.lastPathComponent, packageURL, failureReason: loadingError.localizedDescription, associatedError: nil)
}
}
}

0 comments on commit 319e44a

Please sign in to comment.