From 284b248ac7863912bbb1e6b453fee280e322393b Mon Sep 17 00:00:00 2001 From: esiayo <41133734+blackxfiied@users.noreply.github.com> Date: Sat, 9 Nov 2024 09:52:34 +0800 Subject: [PATCH] refactor: create unified error for legendary metadata retrieval failure --- Mythic/Utilities/Legendary/LegendaryInterface.swift | 11 ++++------- .../Utilities/Legendary/LegendaryInterfaceExt.swift | 11 ++++++++--- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/Mythic/Utilities/Legendary/LegendaryInterface.swift b/Mythic/Utilities/Legendary/LegendaryInterface.swift index e29a643f..979ecc1f 100644 --- a/Mythic/Utilities/Legendary/LegendaryInterface.swift +++ b/Mythic/Utilities/Legendary/LegendaryInterface.swift @@ -59,6 +59,7 @@ final class Legendary { } // MARK: - Methods + // TODO: FIXME: better error handling using ``UnableToRetrieveError`` /** Executes Legendary's command-line process with the specified arguments and handles its output and input interactions. @@ -435,13 +436,13 @@ final class Legendary { let installedData = try JSON(data: Data(contentsOf: URL(filePath: "\(configLocation)/installed.json"))) guard let platformString = installedData[game.id]["platform"].string else { - throw UnableToGetPlatformError() + throw UnableToRetrieveError() } switch platformString { case "Mac": return .macOS case "Windows": return .windows - default: throw UnableToGetPlatformError() + default: throw UnableToRetrieveError() } } @@ -595,12 +596,8 @@ final class Legendary { */ static func getGameLaunchArguments(game: Mythic.Game) throws -> [String] { let installedData = try JSON(data: Data(contentsOf: URL(filePath: "\(configLocation)/installed.json"))) - guard let platformString = installedData[game.id]["platform"].string else { - throw UnableToGetPlatformError() - } - guard let arguments = installedData[game.id]["launch_parameters"].string else { - throw UnableToGetPlatformError() + throw UnableToRetrieveError() } return arguments.components(separatedBy: .whitespaces) diff --git a/Mythic/Utilities/Legendary/LegendaryInterfaceExt.swift b/Mythic/Utilities/Legendary/LegendaryInterfaceExt.swift index ea0015a7..14653181 100644 --- a/Mythic/Utilities/Legendary/LegendaryInterfaceExt.swift +++ b/Mythic/Utilities/Legendary/LegendaryInterfaceExt.swift @@ -22,9 +22,14 @@ extension Legendary { case normal case tall } - - struct UnableToGetPlatformError: LocalizedError { - var errorDescription: String? = "Mythic is unable to get the platform of this game." + + enum RetrievalType { + case platform + case launchArguments + } + + struct UnableToRetrieveError: LocalizedError { + var errorDescription: String? = "Mythic is unable to retrive the requested metadata for this game." } struct IsNotLegendaryError: LocalizedError {