Debrid: Unify errors

Error handling can be tedious with debrid because network errors
to ignore are present (such as -999). Create a function to properly
display the errors to the user and log them via print statments.

Signed-off-by: kingbri <bdashore3@proton.me>
This commit is contained in:
kingbri 2023-01-05 10:59:21 -05:00
parent 025d3797dc
commit 90ed4f8353
2 changed files with 42 additions and 58 deletions

View file

@ -116,6 +116,21 @@ public class DebridManager: ObservableObject {
} }
} }
// Wrapper function to match error descriptions
// Error can be suppressed to end user but must be printed in logs
func sendDebridError(_ error: Error, prefix: String, presentError: Bool = true, cancelString: String? = nil) async {
let error = error as NSError
if presentError {
if let cancelString, error.code == -999 {
toastModel?.updateToastDescription(cancelString, newToastType: .info)
} else if error.code != -999 {
toastModel?.updateToastDescription("\(prefix): \(error)")
}
}
print("\(prefix): \(error)")
}
// Cleans all cached IA values in the event of a full IA refresh // Cleans all cached IA values in the event of a full IA refresh
public func clearIAValues() { public func clearIAValues() {
realDebridIAValues = [] realDebridIAValues = []
@ -206,13 +221,7 @@ public class DebridManager: ObservableObject {
} }
} }
} catch { } catch {
let error = error as NSError await sendDebridError(error, prefix: "Hash population error")
if error.code != -999 {
toastModel?.updateToastDescription("Hash population error: \(error)")
}
print("Hash population error: \(error)")
} }
} }
@ -313,7 +322,6 @@ public class DebridManager: ObservableObject {
// Callback to finish debrid auth since functions can be split // Callback to finish debrid auth since functions can be split
func completeDebridAuth(_ debridType: DebridType, success: Bool = true) { func completeDebridAuth(_ debridType: DebridType, success: Bool = true) {
if enabledDebrids.count == 1, success { if enabledDebrids.count == 1, success {
print("Enabled debrids is 1!")
selectedDebridType = enabledDebrids.first selectedDebridType = enabledDebrids.first
} }
@ -358,11 +366,9 @@ public class DebridManager: ObservableObject {
return true return true
} catch { } catch {
toastModel?.updateToastDescription("RealDebrid authentication error: \(error)") await sendDebridError(error, prefix: "RealDebrid authentication error")
realDebrid.authTask?.cancel() realDebrid.authTask?.cancel()
print("RealDebrid authentication error: \(error)")
return false return false
} }
} }
@ -381,11 +387,9 @@ public class DebridManager: ObservableObject {
return true return true
} catch { } catch {
toastModel?.updateToastDescription("AllDebrid authentication error: \(error)") await sendDebridError(error, prefix: "AllDebrid authentication error")
allDebrid.authTask?.cancel() allDebrid.authTask?.cancel()
print("AllDebrid authentication error: \(error)")
return false return false
} }
} }
@ -397,15 +401,14 @@ public class DebridManager: ObservableObject {
validateAuthUrl(tempAuthUrl, useAuthSession: true) validateAuthUrl(tempAuthUrl, useAuthSession: true)
} catch { } catch {
toastModel?.updateToastDescription("Premiumize authentication error: \(error)") await sendDebridError(error, prefix: "Premiumize authentication error")
completeDebridAuth(.premiumize, success: false)
print("Premiumize authentication error (auth): \(error)") completeDebridAuth(.premiumize, success: false)
} }
} }
// Currently handles Premiumize callback // Currently handles Premiumize callback
public func handleCallback(url: URL?, error: Error?) { public func handleCallback(url: URL?, error: Error?) async {
do { do {
if let error { if let error {
throw Premiumize.PMError.AuthQuery(description: "OAuth callback Error: \(error)") throw Premiumize.PMError.AuthQuery(description: "OAuth callback Error: \(error)")
@ -419,10 +422,9 @@ public class DebridManager: ObservableObject {
throw Premiumize.PMError.AuthQuery(description: "The callback URL was invalid") throw Premiumize.PMError.AuthQuery(description: "The callback URL was invalid")
} }
} catch { } catch {
toastModel?.updateToastDescription("Premiumize authentication error: \(error)") await sendDebridError(error, prefix: "Premiumize authentication error (callback)")
completeDebridAuth(.premiumize, success: false)
print("Premiumize authentication error (callback): \(error)") completeDebridAuth(.premiumize, success: false)
} }
} }
@ -450,9 +452,7 @@ public class DebridManager: ObservableObject {
try await realDebrid.deleteTokens() try await realDebrid.deleteTokens()
enabledDebrids.remove(.realDebrid) enabledDebrids.remove(.realDebrid)
} catch { } catch {
toastModel?.updateToastDescription("RealDebrid logout error: \(error)") await sendDebridError(error, prefix: "RealDebrid logout error")
print("RealDebrid logout error: \(error)")
} }
} }
@ -539,21 +539,12 @@ public class DebridManager: ObservableObject {
case RealDebrid.RDError.EmptyTorrents: case RealDebrid.RDError.EmptyTorrents:
showDeleteAlert.toggle() showDeleteAlert.toggle()
default: default:
let error = error as NSError await sendDebridError(error, prefix: "RealDebrid download error", cancelString: "Download cancelled")
switch error.code { await deleteRdTorrent(torrentID: selectedRealDebridID, presentError: false)
case -999:
toastModel?.updateToastDescription("Download cancelled", newToastType: .info)
default:
toastModel?.updateToastDescription("RealDebrid download error: \(error)")
}
await deleteRdTorrent(torrentID: selectedRealDebridID)
} }
showLoadingProgress = false showLoadingProgress = false
print("RealDebrid download error: \(error)")
} }
} }
@ -567,8 +558,7 @@ public class DebridManager: ObservableObject {
// 5 minutes // 5 minutes
realDebridCloudTTL = Date().timeIntervalSince1970 + 300 realDebridCloudTTL = Date().timeIntervalSince1970 + 300
} catch { } catch {
toastModel?.updateToastDescription("RealDebrid cloud fetch error: \(error)") await sendDebridError(error, prefix: "RealDebrid cloud fetch error")
print("RealDebrid cloud fetch error: \(error)")
} }
} }
} }
@ -580,12 +570,11 @@ public class DebridManager: ObservableObject {
// Bypass TTL to get current RD values // Bypass TTL to get current RD values
await fetchRdCloud(bypassTTL: true) await fetchRdCloud(bypassTTL: true)
} catch { } catch {
toastModel?.updateToastDescription("RealDebrid download delete error: \(error)") await sendDebridError(error, prefix: "RealDebrid download delete error")
print("RealDebrid download delete error: \(error)")
} }
} }
func deleteRdTorrent(torrentID: String? = nil) async { func deleteRdTorrent(torrentID: String? = nil, presentError: Bool = true) async {
do { do {
if let torrentID = torrentID { if let torrentID = torrentID {
try await realDebrid.deleteTorrent(debridID: torrentID) try await realDebrid.deleteTorrent(debridID: torrentID)
@ -595,8 +584,7 @@ public class DebridManager: ObservableObject {
throw RealDebrid.RDError.FailedRequest(description: "No torrent ID was provided") throw RealDebrid.RDError.FailedRequest(description: "No torrent ID was provided")
} }
} catch { } catch {
toastModel?.updateToastDescription("RealDebrid torrent delete error: \(error)") await sendDebridError(error, prefix: "RealDebrid torrent delete error", presentError: presentError)
print("RealDebrid torrent delete error: \(error)")
} }
} }
@ -629,13 +617,7 @@ public class DebridManager: ObservableObject {
downloadUrl = unlockedLink downloadUrl = unlockedLink
} catch { } catch {
let error = error as NSError await sendDebridError(error, prefix: "AllDebrid download error", cancelString: "Download cancelled")
switch error.code {
case -999:
toastModel?.updateToastDescription("Download cancelled", newToastType: .info)
default:
toastModel?.updateToastDescription("AllDebrid download error: \(error)")
}
} }
} }
@ -659,8 +641,7 @@ public class DebridManager: ObservableObject {
try await premiumize.createTransfer(magnet: premiumizeItem.magnet) try await premiumize.createTransfer(magnet: premiumizeItem.magnet)
} }
} catch { } catch {
toastModel?.updateToastDescription("Premiumize download error: \(error)") await sendDebridError(error, prefix: "Premiumize download error", cancelString: "Download or transfer cancelled")
print("Premiumize download error: \(error)")
} }
} }
@ -676,8 +657,10 @@ public class DebridManager: ObservableObject {
// 5 minutes // 5 minutes
premiumizeCloudTTL = Date().timeIntervalSince1970 + 300 premiumizeCloudTTL = Date().timeIntervalSince1970 + 300
} catch { } catch {
toastModel?.updateToastDescription("Premiumize cloud fetch error: \(error)") let error = error as NSError
print("Premiumize cloud fetch error: \(error)") if error.code != -999 {
await sendDebridError(error, prefix: "Premiumize cloud fetch error")
}
} }
} }
} }
@ -689,8 +672,7 @@ public class DebridManager: ObservableObject {
// Bypass TTL to get current RD values // Bypass TTL to get current RD values
await fetchPmCloud(bypassTTL: true) await fetchPmCloud(bypassTTL: true)
} catch { } catch {
toastModel?.updateToastDescription("Premiumize cloud delete error: \(error)") await sendDebridError(error, prefix: "Premiumize cloud delete error")
print("Premiumize cloud delete error: \(error)")
} }
} }
} }

View file

@ -157,7 +157,9 @@ struct SettingsView: View {
url: debridManager.authUrl ?? URL(string: "https://google.com")!, url: debridManager.authUrl ?? URL(string: "https://google.com")!,
callbackURLScheme: "ferrite" callbackURLScheme: "ferrite"
) { callbackURL, error in ) { callbackURL, error in
debridManager.handleCallback(url: callbackURL, error: error) Task {
await debridManager.handleCallback(url: callbackURL, error: error)
}
} }
.prefersEphemeralWebBrowserSession(false) .prefersEphemeralWebBrowserSession(false)
} }