1
1
mirror of https://github.com/danbee/persephone synced 2025-03-04 08:39:11 +00:00

Handle errors connecting to MPD

This commit is contained in:
Daniel Barber 2020-02-24 08:23:53 -05:00
parent 41120dd16c
commit 063b8da202
Signed by: danbarber
GPG Key ID: 931D8112E0103DD8
8 changed files with 116 additions and 26 deletions

View File

@ -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 = "<group>"; };
E4B11BC12275EE410075461B /* AlbumListActions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AlbumListActions.swift; sourceTree = "<group>"; };
E4B3DF6423D66A4400728F6B /* QueueSongCoverView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = QueueSongCoverView.swift; sourceTree = "<group>"; };
E4B46F8E2402E89800152157 /* MPDError.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MPDError.swift; sourceTree = "<group>"; };
E4B5AE7D22F4C49600CCEC65 /* MPDServerDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MPDServerDelegate.swift; sourceTree = "<group>"; };
E4BB7F8E23E5E7BC00906E2F /* MPDAlbumArtImageDataProvider.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MPDAlbumArtImageDataProvider.swift; sourceTree = "<group>"; };
E4BB7F9223E9150A00906E2F /* CoverArtService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CoverArtService.swift; sourceTree = "<group>"; };
@ -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 */,

View File

@ -34,7 +34,7 @@ class AppDelegate: NSObject,
_ = App.userNotificationsController
App.mpdServerController.connect()
_ = App.mpdServerController
}
func applicationWillTerminate(_ aNotification: Notification) {

View File

@ -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 {

View File

@ -208,7 +208,7 @@
<rect key="frame" x="0.0" y="14" width="153" height="24"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<segmentedCell key="cell" borderStyle="border" alignment="left" style="texturedRounded" trackingMode="momentary" id="EBk-sD-nG7">
<font key="font" metaFont="system"/>
<font key="font" metaFont="label" size="13"/>
<segments>
<segment image="prevTrackButton" width="32" enabled="NO"/>
<segment image="playButton" width="48" enabled="NO" tag="1"/>
@ -230,7 +230,7 @@
<rect key="frame" x="16" y="14" width="55" height="17"/>
<autoresizingMask key="autoresizingMask"/>
<textFieldCell key="cell" lineBreakMode="clipping" alignment="right" placeholderString="8:88:88" id="g0c-k5-wCA">
<font key="font" metaFont="system"/>
<font key="font" metaFont="label" size="13"/>
<color key="textColor" name="tertiaryLabelColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="textBackgroundColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
@ -257,7 +257,7 @@
<rect key="frame" x="16" y="14" width="60" height="17"/>
<autoresizingMask key="autoresizingMask"/>
<textFieldCell key="cell" lineBreakMode="clipping" alignment="left" placeholderString="-8:88:88" id="XUa-pD-s5c">
<font key="font" metaFont="system"/>
<font key="font" metaFont="label" size="13"/>
<color key="textColor" name="tertiaryLabelColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="textBackgroundColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
@ -279,7 +279,7 @@
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<buttonCell key="cell" type="roundTextured" bezelStyle="texturedRounded" image="shuffleButton" imagePosition="overlaps" alignment="center" lineBreakMode="truncatingTail" borderStyle="border" inset="2" id="YNb-hd-ax8">
<behavior key="behavior" pushIn="YES" changeContents="YES" lightByContents="YES"/>
<font key="font" metaFont="system"/>
<font key="font" metaFont="label" size="13"/>
</buttonCell>
<connections>
<action selector="handleShuffleButton:" target="B8D-0N-5wS" id="THd-0g-fmb"/>
@ -295,7 +295,7 @@
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<buttonCell key="cell" type="roundTextured" bezelStyle="texturedRounded" image="repeatButton" imagePosition="only" alignment="center" lineBreakMode="truncatingTail" borderStyle="border" inset="2" id="1bu-vK-3Hb">
<behavior key="behavior" pushIn="YES" changeContents="YES" lightByContents="YES"/>
<font key="font" metaFont="system"/>
<font key="font" metaFont="label" size="13"/>
</buttonCell>
<connections>
<action selector="handleRepeatButton:" target="B8D-0N-5wS" id="EN2-u4-DNl"/>
@ -308,7 +308,7 @@
<rect key="frame" x="0.0" y="14" width="96" height="22"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<searchFieldCell key="cell" scrollable="YES" lineBreakMode="clipping" selectable="YES" editable="YES" borderStyle="bezel" usesSingleLineMode="YES" bezelStyle="round" id="F3N-3P-tS3">
<font key="font" metaFont="system"/>
<font key="font" metaFont="label" size="13"/>
<color key="textColor" name="controlTextColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="textBackgroundColor" catalog="System" colorSpace="catalog"/>
</searchFieldCell>
@ -324,7 +324,7 @@
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<buttonCell key="cell" type="roundTextured" bezelStyle="texturedRounded" image="speakerHigh" imagePosition="overlaps" alignment="center" borderStyle="border" imageScaling="proportionallyDown" inset="2" id="bJh-X9-7q0">
<behavior key="behavior" pushIn="YES" lightByBackground="YES" lightByGray="YES"/>
<font key="font" metaFont="system"/>
<font key="font" metaFont="label" size="13"/>
</buttonCell>
<connections>
<action selector="showVolumeControl:" target="B8D-0N-5wS" id="UoW-fa-jBM"/>
@ -426,7 +426,7 @@
<tabView key="tabView" type="noTabsNoBorder" id="6dC-M0-oC5">
<rect key="frame" x="0.0" y="0.0" width="418" height="300"/>
<autoresizingMask key="autoresizingMask"/>
<font key="font" metaFont="system"/>
<font key="font" metaFont="label" size="13"/>
<connections>
<outlet property="delegate" destination="zhe-qh-Mal" id="LUL-qN-JlP"/>
</connections>
@ -464,7 +464,7 @@
<rect key="frame" x="78" y="59" width="271" height="18"/>
<buttonCell key="cell" type="check" title="Fetch missing artwork from MusicBrainz" bezelStyle="regularSquare" imagePosition="left" enabled="NO" inset="2" id="LpD-Ew-HMd">
<behavior key="behavior" changeContents="YES" doesNotDimImage="YES" lightByContents="YES"/>
<font key="font" metaFont="system"/>
<font key="font" metaFont="label" size="13"/>
</buttonCell>
<connections>
<action selector="updateFetchMissingArtworkFromInternet:" target="3C9-vU-zjZ" id="I7x-9V-xJr"/>
@ -476,7 +476,7 @@
<rect key="frame" x="74" y="19" width="279" height="32"/>
<buttonCell key="cell" type="push" title="Clear album art cache..." bezelStyle="rounded" alignment="center" borderStyle="border" imageScaling="proportionallyDown" inset="2" id="l81-SG-7mf">
<behavior key="behavior" pushIn="YES" lightByBackground="YES" lightByGray="YES"/>
<font key="font" metaFont="system"/>
<font key="font" metaFont="label" size="13"/>
</buttonCell>
<connections>
<action selector="clearAlbumArtCache:" target="3C9-vU-zjZ" id="tXg-rz-lvh"/>
@ -512,7 +512,7 @@
<subviews>
<tabView type="noTabsNoBorder" initialItem="XgS-cX-SDH" translatesAutoresizingMaskIntoConstraints="NO" id="ARv-cj-xlz">
<rect key="frame" x="0.0" y="0.0" width="478" height="558"/>
<font key="font" metaFont="system"/>
<font key="font" metaFont="label" size="13"/>
<tabViewItems>
<tabViewItem label="Albums" identifier="" id="XgS-cX-SDH">
<view key="view" id="hB7-hN-SbB">
@ -571,7 +571,7 @@
<textField key="contentView" translatesAutoresizingMaskIntoConstraints="NO" id="kvB-99-zwY">
<rect key="frame" x="78" y="64" width="165" height="17"/>
<textFieldCell key="cell" lineBreakMode="clipping" alignment="right" title="Server Host:" id="AVi-g9-Irz">
<font key="font" metaFont="system"/>
<font key="font" metaFont="label" size="13"/>
<color key="textColor" name="labelColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="textBackgroundColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
@ -594,7 +594,7 @@
<textField key="contentView" translatesAutoresizingMaskIntoConstraints="NO" id="AU9-wN-kbU">
<rect key="frame" x="78" y="29" width="165" height="16"/>
<textFieldCell key="cell" lineBreakMode="clipping" alignment="right" title="Server Port:" id="DgA-xT-2ir">
<font key="font" metaFont="system"/>
<font key="font" metaFont="label" size="13"/>
<color key="textColor" name="labelColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="textBackgroundColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
@ -608,7 +608,7 @@
<real key="minimum" value="0.0"/>
<real key="maximum" value="65535"/>
</numberFormatter>
<font key="font" metaFont="system"/>
<font key="font" metaFont="label" size="13"/>
<color key="textColor" name="controlTextColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="textBackgroundColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
@ -634,7 +634,7 @@
</viewController>
<customObject id="lzf-yO-5pP" userLabel="First Responder" customClass="NSResponder" sceneMemberID="firstResponder"/>
<textFieldCell lineBreakMode="clipping" alignment="right" title="Server Port:" id="22M-hh-h8g">
<font key="font" metaFont="system"/>
<font key="font" metaFont="label" size="13"/>
<color key="textColor" name="labelColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="textBackgroundColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
@ -669,7 +669,7 @@
<color key="backgroundColor" name="headerColor" catalog="System" colorSpace="catalog"/>
</tableHeaderCell>
<textFieldCell key="dataCell" lineBreakMode="truncatingTail" selectable="YES" editable="YES" title="Text Cell" id="99v-Rb-3kv">
<font key="font" metaFont="system"/>
<font key="font" metaFont="label" size="13"/>
<color key="textColor" name="controlTextColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="controlBackgroundColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
@ -714,7 +714,7 @@
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
</tableHeaderCell>
<textFieldCell key="dataCell" lineBreakMode="truncatingTail" selectable="YES" editable="YES" alignment="left" title="Text Cell" id="zb2-QK-DhK">
<font key="font" metaFont="system"/>
<font key="font" metaFont="label" size="13"/>
<color key="textColor" name="controlTextColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="controlBackgroundColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
@ -735,7 +735,7 @@
<textField verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="i0h-bn-auJ" userLabel="Song Title View">
<rect key="frame" x="1" y="23" width="217" height="16"/>
<textFieldCell key="cell" lineBreakMode="truncatingTail" sendsActionOnEndEditing="YES" title="Song Title" id="ei8-1e-ErK">
<font key="font" metaFont="system"/>
<font key="font" metaFont="label" size="13"/>
<color key="textColor" name="controlTextColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="controlColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
@ -762,7 +762,7 @@
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
</tableHeaderCell>
<textFieldCell key="dataCell" lineBreakMode="truncatingTail" selectable="YES" editable="YES" alignment="left" title="Text Cell" id="JOa-Mc-ceQ">
<font key="font" metaFont="system"/>
<font key="font" metaFont="label" size="13"/>
<color key="textColor" name="controlTextColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="controlBackgroundColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
@ -775,7 +775,7 @@
<textField verticalHuggingPriority="750" horizontalCompressionResistancePriority="250" translatesAutoresizingMaskIntoConstraints="NO" id="ukg-c0-XVS">
<rect key="frame" x="11" y="13" width="41" height="16"/>
<textFieldCell key="cell" lineBreakMode="truncatingTail" sendsActionOnEndEditing="YES" alignment="right" title="88:88" id="JnJ-sF-vCP">
<font key="font" metaFont="system"/>
<font key="font" metaFont="label" size="13"/>
<color key="textColor" name="secondaryLabelColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="controlColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>

View File

@ -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)

View File

@ -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

View File

@ -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)
}
}

View File

@ -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)