From 063b8da2029f3253545e70f5fd5a24c22661bb96 Mon Sep 17 00:00:00 2001 From: Daniel Barber Date: Mon, 24 Feb 2020 08:23:53 -0500 Subject: [PATCH] Handle errors connecting to MPD --- Persephone.xcodeproj/project.pbxproj | 4 ++ Persephone/AppDelegate.swift | 2 +- .../Components/Shared/MPDServerDelegate.swift | 23 ++++++++- .../Window/Base.lproj/Main.storyboard | 40 +++++++-------- .../Extensions/MPDClient+Connection.swift | 11 ++-- .../Extensions/MPDClient+Error.swift | 10 ++++ Persephone/MPDClient/Models/MPDError.swift | 50 +++++++++++++++++++ Persephone/MPDClient/Protocols/Delegate.swift | 2 + 8 files changed, 116 insertions(+), 26 deletions(-) create mode 100644 Persephone/MPDClient/Models/MPDError.swift diff --git a/Persephone.xcodeproj/project.pbxproj b/Persephone.xcodeproj/project.pbxproj index d6828eb..f4af39f 100644 --- a/Persephone.xcodeproj/project.pbxproj +++ b/Persephone.xcodeproj/project.pbxproj @@ -101,6 +101,7 @@ E4B11BC02275EE150075461B /* QueueActions.swift in Sources */ = {isa = PBXBuildFile; fileRef = E4B11BBF2275EE150075461B /* QueueActions.swift */; }; E4B11BC22275EE410075461B /* AlbumListActions.swift in Sources */ = {isa = PBXBuildFile; fileRef = E4B11BC12275EE410075461B /* AlbumListActions.swift */; }; E4B3DF6523D66A4400728F6B /* QueueSongCoverView.swift in Sources */ = {isa = PBXBuildFile; fileRef = E4B3DF6423D66A4400728F6B /* QueueSongCoverView.swift */; }; + E4B46F8F2402E89800152157 /* MPDError.swift in Sources */ = {isa = PBXBuildFile; fileRef = E4B46F8E2402E89800152157 /* MPDError.swift */; }; E4B5AE7E22F4C49600CCEC65 /* MPDServerDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = E4B5AE7D22F4C49600CCEC65 /* MPDServerDelegate.swift */; }; E4BB7F8F23E5E7BC00906E2F /* MPDAlbumArtImageDataProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = E4BB7F8E23E5E7BC00906E2F /* MPDAlbumArtImageDataProvider.swift */; }; E4BB7F9323E9150A00906E2F /* CoverArtService.swift in Sources */ = {isa = PBXBuildFile; fileRef = E4BB7F9223E9150A00906E2F /* CoverArtService.swift */; }; @@ -300,6 +301,7 @@ E4B11BBF2275EE150075461B /* QueueActions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = QueueActions.swift; sourceTree = ""; }; E4B11BC12275EE410075461B /* AlbumListActions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AlbumListActions.swift; sourceTree = ""; }; E4B3DF6423D66A4400728F6B /* QueueSongCoverView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = QueueSongCoverView.swift; sourceTree = ""; }; + E4B46F8E2402E89800152157 /* MPDError.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MPDError.swift; sourceTree = ""; }; E4B5AE7D22F4C49600CCEC65 /* MPDServerDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MPDServerDelegate.swift; sourceTree = ""; }; E4BB7F8E23E5E7BC00906E2F /* MPDAlbumArtImageDataProvider.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MPDAlbumArtImageDataProvider.swift; sourceTree = ""; }; E4BB7F9223E9150A00906E2F /* CoverArtService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CoverArtService.swift; sourceTree = ""; }; @@ -707,6 +709,7 @@ children = ( E4EB237A220F7CF1008C70C0 /* MPDAlbum.swift */, E45962C52241A78500FC1A1E /* MPDCommand.swift */, + E4B46F8E2402E89800152157 /* MPDError.swift */, E4C8B53D22349002009A20F3 /* MPDIdle.swift */, E4EB2378220F10B8008C70C0 /* MPDPair.swift */, E4E8CC9922075D370024217A /* MPDSong.swift */, @@ -974,6 +977,7 @@ E450AD7E222620A10091BED3 /* Album.swift in Sources */, E4DA820623D6236200C1EE58 /* NSSize.swift in Sources */, E408D3B6220DD8970006D9BE /* Notification.swift in Sources */, + E4B46F8F2402E89800152157 /* MPDError.swift in Sources */, E43AC1F822C7065A001E483C /* AlbumCoverButton.swift in Sources */, E45962C62241A78500FC1A1E /* MPDCommand.swift in Sources */, E408D3B9220DE98F0006D9BE /* NSUserInterfaceItemIdentifier.swift in Sources */, diff --git a/Persephone/AppDelegate.swift b/Persephone/AppDelegate.swift index 745739c..142ec6e 100644 --- a/Persephone/AppDelegate.swift +++ b/Persephone/AppDelegate.swift @@ -34,7 +34,7 @@ class AppDelegate: NSObject, _ = App.userNotificationsController - App.mpdServerController.connect() + _ = App.mpdServerController } func applicationWillTerminate(_ aNotification: Notification) { diff --git a/Persephone/Components/Shared/MPDServerDelegate.swift b/Persephone/Components/Shared/MPDServerDelegate.swift index 8c38114..0dabdb2 100644 --- a/Persephone/Components/Shared/MPDServerDelegate.swift +++ b/Persephone/Components/Shared/MPDServerDelegate.swift @@ -6,7 +6,7 @@ // Copyright © 2019 Dan Barber. All rights reserved. // -import Foundation +import AppKit class MPDServerDelegate: MPDClientDelegate { func didConnect(mpdClient: MPDClient) { @@ -16,6 +16,27 @@ class MPDServerDelegate: MPDClientDelegate { func willDisconnect(mpdClient: MPDClient) { NotificationCenter.default.post(name: .willDisconnect, object: nil) } + + func didRaiseError(mpdClient: MPDClient, error: MPDClient.MPDError) { + DispatchQueue.main.async { + let alert = NSAlert(error: error) + switch error { + case .success: + break; + case .outOfMemory(let message), + .argument(let message), + .state(let message), + .timeout(let message), + .system(let message), + .resolver(let message), + .malformed(let message), + .closed(let message), + .server(let message): + alert.messageText = message + alert.runModal() + } + } + } func didUpdateStatus(mpdClient: MPDClient, status: MPDClient.MPDStatus) { DispatchQueue.main.async { diff --git a/Persephone/Components/Window/Base.lproj/Main.storyboard b/Persephone/Components/Window/Base.lproj/Main.storyboard index 38911c6..3c71fee 100644 --- a/Persephone/Components/Window/Base.lproj/Main.storyboard +++ b/Persephone/Components/Window/Base.lproj/Main.storyboard @@ -208,7 +208,7 @@ - + @@ -230,7 +230,7 @@ - + @@ -257,7 +257,7 @@ - + @@ -279,7 +279,7 @@ - + @@ -295,7 +295,7 @@ - + @@ -308,7 +308,7 @@ - + @@ -324,7 +324,7 @@ - + @@ -426,7 +426,7 @@ - + @@ -464,7 +464,7 @@ - + @@ -476,7 +476,7 @@ - + @@ -512,7 +512,7 @@ - + @@ -571,7 +571,7 @@ - + @@ -594,7 +594,7 @@ - + @@ -608,7 +608,7 @@ - + @@ -634,7 +634,7 @@ - + @@ -669,7 +669,7 @@ - + @@ -714,7 +714,7 @@ - + @@ -735,7 +735,7 @@ - + @@ -762,7 +762,7 @@ - + @@ -775,7 +775,7 @@ - + diff --git a/Persephone/MPDClient/Extensions/MPDClient+Connection.swift b/Persephone/MPDClient/Extensions/MPDClient+Connection.swift index 959cde1..39cbb80 100644 --- a/Persephone/MPDClient/Extensions/MPDClient+Connection.swift +++ b/Persephone/MPDClient/Extensions/MPDClient+Connection.swift @@ -11,16 +11,19 @@ import mpdclient extension MPDClient { func createConnection(host: String, port: Int) { - guard let connection = mpd_connection_new(host, UInt32(port), 10000), - mpd_connection_get_error(connection) == MPD_ERROR_SUCCESS - else { return } + connection = mpd_connection_new(host, UInt32(port), 10000) + let error = mpd_connection_get_error(connection) + + guard error == MPD_ERROR_SUCCESS else { + handleError(mpdError: error) + return + } self.isConnected = true guard let status = mpd_run_status(connection) else { return } - self.connection = connection self.status = MPDStatus(status) delegate?.didConnect(mpdClient: self) diff --git a/Persephone/MPDClient/Extensions/MPDClient+Error.swift b/Persephone/MPDClient/Extensions/MPDClient+Error.swift index 2c97286..44b13b9 100644 --- a/Persephone/MPDClient/Extensions/MPDClient+Error.swift +++ b/Persephone/MPDClient/Extensions/MPDClient+Error.swift @@ -10,6 +10,16 @@ import Foundation import mpdclient extension MPDClient { + func handleError(mpdError: mpd_error) { + guard let errorMessage = mpd_connection_get_error_message(connection) + else { return } + + let message = String(cString: errorMessage) + + let error = MPDError(mpdError: mpdError, message: message) + delegate?.didRaiseError(mpdClient: self, error: error) + } + func getLastErrorMessage() -> String? { if mpd_connection_get_error(connection) == MPD_ERROR_SUCCESS { return nil diff --git a/Persephone/MPDClient/Models/MPDError.swift b/Persephone/MPDClient/Models/MPDError.swift new file mode 100644 index 0000000..3324bb7 --- /dev/null +++ b/Persephone/MPDClient/Models/MPDError.swift @@ -0,0 +1,50 @@ +// +// MPDError.swift +// Persephone +// +// Created by Daniel Barber on 2/23/20. +// Copyright © 2020 Dan Barber. All rights reserved. +// + +import Foundation +import mpdclient + +extension MPDClient { + enum MPDError: Error { + init(mpdError: mpd_error, message: String) { + switch mpdError { + case MPD_ERROR_OOM: + self = .outOfMemory(message) + case MPD_ERROR_ARGUMENT: + self = .argument(message) + case MPD_ERROR_STATE: + self = .state(message) + case MPD_ERROR_TIMEOUT: + self = .timeout(message) + case MPD_ERROR_SYSTEM: + self = .system(message) + case MPD_ERROR_RESOLVER: + self = .resolver(message) + case MPD_ERROR_MALFORMED: + self = .malformed(message) + case MPD_ERROR_CLOSED: + self = .closed(message) + case MPD_ERROR_SERVER: + self = .server(message) + default: + self = .success + } + } + + case success + case outOfMemory(String) + case argument(String) + case state(String) + case timeout(String) + case system(String) + case resolver(String) + case malformed(String) + case closed(String) + case server(String) + } +} diff --git a/Persephone/MPDClient/Protocols/Delegate.swift b/Persephone/MPDClient/Protocols/Delegate.swift index 3e55d58..95ad910 100644 --- a/Persephone/MPDClient/Protocols/Delegate.swift +++ b/Persephone/MPDClient/Protocols/Delegate.swift @@ -11,6 +11,8 @@ import Foundation protocol MPDClientDelegate { func didConnect(mpdClient: MPDClient) func willDisconnect(mpdClient: MPDClient) + + func didRaiseError(mpdClient: MPDClient, error: MPDClient.MPDError) func didUpdateStatus(mpdClient: MPDClient, status: MPDClient.MPDStatus)