Better Error Handling

This commit is contained in:
aditya-K2 2021-12-31 15:48:58 +05:30
parent f6800b0361
commit 719a659df6
2 changed files with 109 additions and 35 deletions

111
main.go
View File

@ -24,6 +24,8 @@ func main() {
var mpdConnectionError error var mpdConnectionError error
CONN, mpdConnectionError := mpd.Dial("tcp", "localhost:"+viper.GetString("MPD_PORT")) CONN, mpdConnectionError := mpd.Dial("tcp", "localhost:"+viper.GetString("MPD_PORT"))
if mpdConnectionError != nil { if mpdConnectionError != nil {
utils.Print("RED", "Could Not Connect to MPD Server\n")
utils.Print("GREEN", "Make Sure You Mention the Correct MPD Port in the config file.\n")
panic(mpdConnectionError) panic(mpdConnectionError)
} }
defer CONN.Close() defer CONN.Close()
@ -40,12 +42,16 @@ func main() {
// Connecting the Renderer to the Main UI // Connecting the Renderer to the Main UI
ui.ConnectRenderer(Renderer) ui.ConnectRenderer(Renderer)
c, _ := CONN.CurrentSong() if c, err := CONN.CurrentSong(); err != nil {
utils.Print("RED", "Could Not Retrieve the Current Song\n")
panic(err)
} else {
if len(c) != 0 { if len(c) != 0 {
Renderer.Start(c["file"]) Renderer.Start(c["file"])
} else { } else {
Renderer.Start("stop") Renderer.Start("stop")
} }
}
UI := ui.NewApplication() UI := ui.NewApplication()
@ -53,6 +59,11 @@ func main() {
notify.ConnectUI(UI) notify.ConnectUI(UI)
fileMap, err := CONN.ListAllInfo("/") fileMap, err := CONN.ListAllInfo("/")
if err != nil {
utils.Print("RED", "Could Not Generate the File Map\n")
utils.Print("GREEN", "Make Sure You Mention the Correct MPD Port in the config file.\n")
panic(err)
}
// Generating the Directory Tree for File Navigation. // Generating the Directory Tree for File Navigation.
dirTree := client.GenerateDirectoryTree(fileMap) dirTree := client.GenerateDirectoryTree(fileMap)
@ -60,13 +71,25 @@ func main() {
// Default View upon Opening is of Playlist. // Default View upon Opening is of Playlist.
client.UpdatePlaylist(UI.ExpandedView) client.UpdatePlaylist(UI.ExpandedView)
_v, _ := CONN.Status() var Volume int64
var Random, Repeat bool
if _v, err := CONN.Status(); err != nil {
utils.Print("RED", "Could Not Get the MPD Status\n")
panic(err)
} else {
// Setting Volume, Random and Repeat Values // Setting Volume, Random and Repeat Values
Volume, _ := strconv.ParseInt(_v["volume"], 10, 64) Volume, _ = strconv.ParseInt(_v["volume"], 10, 64)
Random, _ := strconv.ParseBool(_v["random"]) Random, _ = strconv.ParseBool(_v["random"])
Repeat, _ := strconv.ParseBool(_v["repeat"]) Repeat, _ = strconv.ParseBool(_v["repeat"])
}
ArtistTree, err := client.GenerateArtistTree() ArtistTree, err := client.GenerateArtistTree()
if err != nil {
utils.Print("RED", "Could Not Generate the ArtistTree\n")
utils.Print("GREEN", "Make Sure You Mention the Correct MPD Port in the config file.\n")
panic(err)
}
// Used for Fuzzy Searching // Used for Fuzzy Searching
ArtistTreeContent := utils.ConvertToArray(ArtistTree) ArtistTreeContent := utils.ConvertToArray(ArtistTree)
@ -106,8 +129,13 @@ func main() {
r, _ := UI.ExpandedView.GetSelection() r, _ := UI.ExpandedView.GetSelection()
ui.SetFocus("FileBrowser") ui.SetFocus("FileBrowser")
if len(dirTree.Children[r].Children) == 0 { if len(dirTree.Children[r].Children) == 0 {
id, _ := CONN.AddId(dirTree.Children[r].AbsolutePath, -1) if id, err := CONN.AddId(dirTree.Children[r].AbsolutePath, -1); err != nil {
CONN.PlayId(id) Notify.Send(fmt.Sprintf("Could not Add Song %s", dirTree.Children[r].Path))
} else {
if err := CONN.PlayId(id); err != nil {
Notify.Send(fmt.Sprintf("Could Not Play Song %s", dirTree.Children[r].Path))
}
}
} else { } else {
client.Update(dirTree.Children[r].Children, UI.ExpandedView) client.Update(dirTree.Children[r].Children, UI.ExpandedView)
dirTree = &dirTree.Children[r] dirTree = &dirTree.Children[r]
@ -115,7 +143,9 @@ func main() {
} }
} else if ui.HasFocus("Playlist") { } else if ui.HasFocus("Playlist") {
r, _ := UI.ExpandedView.GetSelection() r, _ := UI.ExpandedView.GetSelection()
CONN.Play(r) if err := CONN.Play(r); err != nil {
Notify.Send("Could Not Play the Song")
}
} else if ui.HasFocus("SearchView") { } else if ui.HasFocus("SearchView") {
r, _ := UI.ExpandedView.GetSelection() r, _ := UI.ExpandedView.GetSelection()
client.AddToPlaylist(SearchContentSlice[r], true) client.AddToPlaylist(SearchContentSlice[r], true)
@ -123,8 +153,13 @@ func main() {
r, _ := UI.ExpandedView.GetSelection() r, _ := UI.ExpandedView.GetSelection()
ui.SetFocus("FileBrowser") ui.SetFocus("FileBrowser")
if len(dirTree.Children[r].Children) == 0 { if len(dirTree.Children[r].Children) == 0 {
id, _ := CONN.AddId(dirTree.Children[Matches[r].Index].AbsolutePath, -1) if id, err := CONN.AddId(dirTree.Children[Matches[r].Index].AbsolutePath, -1); err != nil {
CONN.PlayId(id) Notify.Send(fmt.Sprintf("Could Not add the Song %s to the Playlist", dirTree.Children[Matches[r].Index].AbsolutePath))
} else {
if err := CONN.PlayId(id); err != nil {
Notify.Send("Could not Play the Song")
}
}
} else { } else {
client.Update(dirTree.Children[Matches[r].Index].Children, UI.ExpandedView) client.Update(dirTree.Children[Matches[r].Index].Children, UI.ExpandedView)
dirTree = &dirTree.Children[Matches[r].Index] dirTree = &dirTree.Children[Matches[r].Index]
@ -135,7 +170,9 @@ func main() {
} }
}, },
"togglePlayBack": func() { "togglePlayBack": func() {
client.TogglePlayBack() if err := client.TogglePlayBack(); err != nil {
Notify.Send("Could not Toggle Play Back")
}
}, },
"showParentContent": func() { "showParentContent": func() {
if ui.HasFocus("FileBrowser") { if ui.HasFocus("FileBrowser") {
@ -146,43 +183,49 @@ func main() {
} }
}, },
"nextSong": func() { "nextSong": func() {
CONN.Next() if err := CONN.Next(); err != nil {
Notify.Send("Could not Select the Next Song")
}
}, },
"clearPlaylist": func() { "clearPlaylist": func() {
CONN.Clear() if err := CONN.Clear(); err != nil {
Notify.Send("Could not Clear the Playlist")
} else {
Notify.Send("Playlist Cleared") Notify.Send("Playlist Cleared")
}
}, },
"previousSong": func() { "previousSong": func() {
CONN.Previous() if err := CONN.Previous(); err != nil {
Notify.Send("Could Not Select the Previous Song")
}
}, },
"addToPlaylist": func() { "addToPlaylist": func() {
if ui.HasFocus("FileBrowser") { if ui.HasFocus("FileBrowser") {
r, _ := UI.ExpandedView.GetSelection() r, _ := UI.ExpandedView.GetSelection()
CONN.Add(dirTree.Children[r].AbsolutePath) if err := CONN.Add(dirTree.Children[r].AbsolutePath); err != nil {
Notify.Send(fmt.Sprintf("Could not add %s to the Playlist", dirTree.Children[r].Path))
}
} else if ui.HasFocus("SearchView") { } else if ui.HasFocus("SearchView") {
r, _ := UI.ExpandedView.GetSelection() r, _ := UI.ExpandedView.GetSelection()
client.AddToPlaylist(SearchContentSlice[r], false) client.AddToPlaylist(SearchContentSlice[r], false)
} else if ui.HasFocus("BuffSearchView") { } else if ui.HasFocus("BuffSearchView") {
r, _ := UI.ExpandedView.GetSelection() r, _ := UI.ExpandedView.GetSelection()
ui.SetFocus("FileBrowser") if err := CONN.Add(dirTree.Children[Matches[r].Index].AbsolutePath); err != nil {
err := CONN.Add(dirTree.Children[Matches[r].Index].AbsolutePath)
if err != nil {
Notify.Send(fmt.Sprintf("Could Not Add URI %s to the Playlist", dirTree.Children[Matches[r].Index].Path)) Notify.Send(fmt.Sprintf("Could Not Add URI %s to the Playlist", dirTree.Children[Matches[r].Index].Path))
} else { } else {
ui.SetFocus("FileBrowser")
Notify.Send(fmt.Sprintf("URI Added %s to the Playlist", dirTree.Children[Matches[r].Index].Path)) Notify.Send(fmt.Sprintf("URI Added %s to the Playlist", dirTree.Children[Matches[r].Index].Path))
}
ui.SetFocus("BuffSearchView") ui.SetFocus("BuffSearchView")
} }
}
}, },
"toggleRandom": func() { "toggleRandom": func() {
err := CONN.Random(!Random) if err := CONN.Random(!Random); err == nil {
if err == nil {
Random = !Random Random = !Random
} }
}, },
"toggleRepeat": func() { "toggleRepeat": func() {
err := CONN.Repeat(!Repeat) if err := CONN.Repeat(!Repeat); err == nil {
if err == nil {
Repeat = !Repeat Repeat = !Repeat
} }
}, },
@ -192,7 +235,9 @@ func main() {
} else { } else {
Volume -= 10 Volume -= 10
} }
CONN.SetVolume(int(Volume)) if err := CONN.SetVolume(int(Volume)); err != nil {
Notify.Send("Could Not Decrease the Volume")
}
}, },
"increaseVolume": func() { "increaseVolume": func() {
if Volume >= 100 { if Volume >= 100 {
@ -200,7 +245,9 @@ func main() {
} else { } else {
Volume += 10 Volume += 10
} }
CONN.SetVolume(int(Volume)) if err := CONN.SetVolume(int(Volume)); err != nil {
Notify.Send("Could Not Increase the Volume")
}
}, },
"navigateToFiles": func() { "navigateToFiles": func() {
ui.SetFocus("FileBrowser") ui.SetFocus("FileBrowser")
@ -229,20 +276,26 @@ func main() {
} }
}, },
"stop": func() { "stop": func() {
CONN.Stop() if err := CONN.Stop(); err != nil {
Notify.Send("Could not Stop the Playback")
} else {
Notify.Send("Playback Stopped") Notify.Send("Playback Stopped")
}
}, },
"updateDB": func() { "updateDB": func() {
_, err = CONN.Update("") _, err = CONN.Update("")
if err != nil { if err != nil {
panic(err) Notify.Send("Could Not Update the Database")
} } else {
Notify.Send("Database Updated") Notify.Send("Database Updated")
}
}, },
"deleteSongFromPlaylist": func() { "deleteSongFromPlaylist": func() {
if ui.HasFocus("Playlist") { if ui.HasFocus("Playlist") {
r, _ := UI.ExpandedView.GetSelection() r, _ := UI.ExpandedView.GetSelection()
CONN.Delete(r, -1) if err := CONN.Delete(r, -1); err != nil {
Notify.Send("Could not Remove the Song from Playlist")
}
} }
}, },
"FocusSearch": func() { "FocusSearch": func() {

21
utils/colors.go Normal file
View File

@ -0,0 +1,21 @@
package utils
import "fmt"
var (
COLORS map[string]string = map[string]string{
"RESET": "\033[0m",
"RED": "\033[31m",
"GREEN": "\033[32m",
"YELLOW": "\033[33m",
"BLUE": "\033[34m",
"PURPLE": "\033[35m",
"CYAN": "\033[36m",
"GRAY": "\033[37m",
"WHITE": "\033[97m",
}
)
func Print(color, text string) {
fmt.Print(COLORS[color] + text + COLORS["RESET"])
}