Improved error handling

This commit is contained in:
ronaldheft committed 2022-08-25 15:42:37 -04:00
1 parent 01678f2c91
commit 8c87b31e56
14 files changed
+220 -141

No files matched your search

+7 -9
View File
@@ -81,9 +81,9 @@ public class AbsAudioPlayer: CAPPlugin {
NSLog("Failed to get local playback session")
return call.resolve([:])
}
playbackSession.save()
do {
try playbackSession.save()
try self.startPlaybackSession(playbackSession, playWhenReady: playWhenReady, playbackRate: playbackRate)
call.resolve(try playbackSession.asDictionary())
} catch(let exception) {
@@ -93,8 +93,8 @@ public class AbsAudioPlayer: CAPPlugin {
}
} else { // Playing from the server
ApiClient.startPlaybackSession(libraryItemId: libraryItemId!, episodeId: episodeId, forceTranscode: false) { session in
session.save()
do {
try session.save()
try self.startPlaybackSession(session, playWhenReady: playWhenReady, playbackRate: playbackRate)
call.resolve(try session.asDictionary())
} catch(let exception) {
@@ -122,7 +122,7 @@ public class AbsAudioPlayer: CAPPlugin {
@objc func setPlaybackSpeed(_ call: CAPPluginCall) {
let playbackRate = call.getFloat("value", 1.0)
let settings = PlayerSettings.main()
settings.update {
try? settings.update {
settings.playbackRate = playbackRate
}
PlayerHandler.setPlaybackSpeed(speed: settings.playbackRate)
@@ -244,17 +244,15 @@ public class AbsAudioPlayer: CAPPlugin {
// If direct playing then fallback to transcode
ApiClient.startPlaybackSession(libraryItemId: libraryItemId, episodeId: episodeId, forceTranscode: true) { session in
session.save()
PlayerHandler.startPlayback(sessionId: session.id, playWhenReady: self.initialPlayWhenReady, playbackRate: PlayerSettings.main().playbackRate)
do {
try session.save()
PlayerHandler.startPlayback(sessionId: session.id, playWhenReady: self.initialPlayWhenReady, playbackRate: PlayerSettings.main().playbackRate)
self.sendPlaybackSession(session: try session.asDictionary())
self.sendMetadata()
} catch(let exception) {
NSLog("failed to convert session to json")
NSLog("Failed to start transcoded session")
debugPrint(exception)
}
self.sendMetadata()
}
} else {
self.notifyListeners("onPlaybackFailed", data: [
+30 -24
View File
@@ -139,7 +139,7 @@ public class AbsDatabase: CAPPlugin {
call.reject("localMediaProgressId not specificed")
return
}
Database.shared.removeLocalMediaProgress(localMediaProgressId: localMediaProgressId)
try? Database.shared.removeLocalMediaProgress(localMediaProgressId: localMediaProgressId)
call.resolve()
}
@@ -171,14 +171,14 @@ public class AbsDatabase: CAPPlugin {
return call.reject("localLibraryItemId or localMediaProgressId must be specified")
}
let localMediaProgress = LocalMediaProgress.fetchOrCreateLocalMediaProgress(localMediaProgressId: localMediaProgressId, localLibraryItemId: localLibraryItemId, localEpisodeId: localEpisodeId)
let localMediaProgress = try LocalMediaProgress.fetchOrCreateLocalMediaProgress(localMediaProgressId: localMediaProgressId, localLibraryItemId: localLibraryItemId, localEpisodeId: localEpisodeId)
guard let localMediaProgress = localMediaProgress else {
call.reject("Local media progress not found or created")
return
}
NSLog("syncServerMediaProgressWithLocalMediaProgress: Saving local media progress")
localMediaProgress.updateFromServerMediaProgress(serverMediaProgress)
try localMediaProgress.updateFromServerMediaProgress(serverMediaProgress)
call.resolve(try localMediaProgress.asDictionary())
} catch {
@@ -195,30 +195,36 @@ public class AbsDatabase: CAPPlugin {
NSLog("updateLocalMediaProgressFinished \(localMediaProgressId ?? "Unknown") | Is Finished: \(isFinished)")
let localMediaProgress = LocalMediaProgress.fetchOrCreateLocalMediaProgress(localMediaProgressId: localMediaProgressId, localLibraryItemId: localLibraryItemId, localEpisodeId: localEpisodeId)
guard let localMediaProgress = localMediaProgress else {
call.resolve(["error": "Library Item not found"])
return
}
do {
let localMediaProgress = try LocalMediaProgress.fetchOrCreateLocalMediaProgress(localMediaProgressId: localMediaProgressId, localLibraryItemId: localLibraryItemId, localEpisodeId: localEpisodeId)
guard let localMediaProgress = localMediaProgress else {
call.resolve(["error": "Library Item not found"])
return
}
// Update finished status
localMediaProgress.updateIsFinished(isFinished)
// Build API response
let progressDictionary = try? localMediaProgress.asDictionary()
var response: [String: Any] = ["local": true, "server": false, "localMediaProgress": progressDictionary ?? ""]
// Send update to the server if logged in
let hasLinkedServer = localMediaProgress.serverConnectionConfigId != nil
let loggedIntoServer = Store.serverConfig?.id == localMediaProgress.serverConnectionConfigId
if hasLinkedServer && loggedIntoServer {
response["server"] = true
let payload = ["isFinished": isFinished]
ApiClient.updateMediaProgress(libraryItemId: localMediaProgress.libraryItemId!, episodeId: localEpisodeId, payload: payload) {
// Update finished status
try localMediaProgress.updateIsFinished(isFinished)
// Build API response
let progressDictionary = try? localMediaProgress.asDictionary()
var response: [String: Any] = ["local": true, "server": false, "localMediaProgress": progressDictionary ?? ""]
// Send update to the server if logged in
let hasLinkedServer = localMediaProgress.serverConnectionConfigId != nil
let loggedIntoServer = Store.serverConfig?.id == localMediaProgress.serverConnectionConfigId
if hasLinkedServer && loggedIntoServer {
response["server"] = true
let payload = ["isFinished": isFinished]
ApiClient.updateMediaProgress(libraryItemId: localMediaProgress.libraryItemId!, episodeId: localEpisodeId, payload: payload) {
call.resolve(response)
}
} else {
call.resolve(response)
}
} else {
call.resolve(response)
} catch {
debugPrint(error)
call.resolve(["error": "Failed to mark as complete"])
return
}
}
+5 -5
View File
@@ -28,7 +28,7 @@ public class AbsDownloader: CAPPlugin, URLSessionDownloadDelegate {
public func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
handleDownloadTaskUpdate(downloadTask: downloadTask) { downloadItem, downloadItemPart in
let realm = try! Realm()
let realm = try Realm()
try realm.write {
downloadItemPart.progress = 100
downloadItemPart.completed = true
@@ -139,7 +139,7 @@ public class AbsDownloader: CAPPlugin, URLSessionDownloadDelegate {
}
self.handleDownloadTaskCompleteFromDownloadItem(item)
if let item = Database.shared.getDownloadItem(downloadItemId: item.id!) {
item.delete()
try? item.delete()
}
}
@@ -181,7 +181,7 @@ public class AbsDownloader: CAPPlugin, URLSessionDownloadDelegate {
}
} else {
localLibraryItem = LocalLibraryItem(libraryItem, localUrl: localDirectory, server: Store.serverConfig!, files: files, coverPath: coverFile)
Database.shared.saveLocalLibraryItem(localLibraryItem: localLibraryItem!)
try? Database.shared.saveLocalLibraryItem(localLibraryItem: localLibraryItem!)
}
statusNotification["localLibraryItem"] = try? localLibraryItem.asDictionary()
@@ -189,7 +189,7 @@ public class AbsDownloader: CAPPlugin, URLSessionDownloadDelegate {
if let progress = libraryItem.userMediaProgress {
let episode = downloadItem.media?.episodes.first(where: { $0.id == downloadItem.episodeId })
let localMediaProgress = LocalMediaProgress(localLibraryItem: localLibraryItem!, episode: episode, progress: progress)
Database.shared.saveLocalMediaProgress(localMediaProgress)
try? localMediaProgress.save()
statusNotification["localMediaProgress"] = try? localMediaProgress.asDictionary()
}
@@ -276,7 +276,7 @@ public class AbsDownloader: CAPPlugin, URLSessionDownloadDelegate {
}
// Persist in the database before status start coming in
Database.shared.saveDownloadItem(downloadItem)
try Database.shared.saveDownloadItem(downloadItem)
// Start all the downloads
for task in tasks {
+20 -15
View File
@@ -70,7 +70,7 @@ public class AbsFileSystem: CAPPlugin {
do {
if let localLibraryItemId = localLibraryItemId, let item = Database.shared.getLocalLibraryItem(localLibraryItemId: localLibraryItemId) {
try FileManager.default.removeItem(at: item.contentDirectory!)
item.delete()
try item.delete()
success = true
}
} catch {
@@ -89,24 +89,29 @@ public class AbsFileSystem: CAPPlugin {
var success = false
if let localLibraryItemId = localLibraryItemId, let trackLocalFileId = trackLocalFileId, let item = Database.shared.getLocalLibraryItem(localLibraryItemId: localLibraryItemId) {
item.update {
do {
if let fileIndex = item.localFiles.firstIndex(where: { $0.id == trackLocalFileId }) {
try FileManager.default.removeItem(at: item.localFiles[fileIndex].contentPath)
item.realm?.delete(item.localFiles[fileIndex])
if item.isPodcast, let media = item.media {
if let episodeIndex = media.episodes.firstIndex(where: { $0.audioTrack?.localFileId == trackLocalFileId }) {
media.episodes.remove(at: episodeIndex)
do {
try item.update {
do {
if let fileIndex = item.localFiles.firstIndex(where: { $0.id == trackLocalFileId }) {
try FileManager.default.removeItem(at: item.localFiles[fileIndex].contentPath)
item.realm?.delete(item.localFiles[fileIndex])
if item.isPodcast, let media = item.media {
if let episodeIndex = media.episodes.firstIndex(where: { $0.audioTrack?.localFileId == trackLocalFileId }) {
media.episodes.remove(at: episodeIndex)
}
item.media = media
}
item.media = media
call.resolve(try item.asDictionary())
success = true
}
call.resolve(try item.asDictionary())
success = true
} catch {
NSLog("Failed to delete \(error)")
success = false
}
} catch {
NSLog("Failed to delete \(error)")
success = false
}
} catch {
NSLog("Failed to delete \(error)")
success = false
}
}