Log more playback/queue errors
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
05a8ed380a
commit
6b4b100719
61
api.go
61
api.go
|
@ -3,6 +3,7 @@ package main
|
|||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"git.gammaspectra.live/S.O.N.G/Kirika/audio/queue"
|
||||
"io"
|
||||
|
@ -36,15 +37,15 @@ func (a *API) Wait() {
|
|||
a.wg.Wait()
|
||||
}
|
||||
|
||||
func (a *API) getQueueEntryFromBody(body []byte) *QueueTrackEntry {
|
||||
func (a *API) getQueueEntryFromBody(body []byte) (*QueueTrackEntry, error) {
|
||||
entry := &QueueTrackEntry{}
|
||||
err := json.Unmarshal(body, &entry.original)
|
||||
if err != nil {
|
||||
return nil
|
||||
return nil, err
|
||||
}
|
||||
err = json.Unmarshal(body, &entry.Metadata)
|
||||
if err != nil {
|
||||
return nil
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var val interface{}
|
||||
|
@ -62,30 +63,30 @@ func (a *API) getQueueEntryFromBody(body []byte) *QueueTrackEntry {
|
|||
}
|
||||
|
||||
if len(entry.Path) > 0 {
|
||||
return entry
|
||||
return entry, nil
|
||||
}
|
||||
|
||||
return nil
|
||||
return nil, errors.New("could not create queue entry")
|
||||
}
|
||||
|
||||
func (a *API) getFallbackTrack() *QueueTrackEntry {
|
||||
func (a *API) getFallbackTrack() (*QueueTrackEntry, error) {
|
||||
m := make(map[string]interface{})
|
||||
m["path"] = a.config.Queue.FallbackPath
|
||||
return &QueueTrackEntry{
|
||||
Path: a.config.Queue.FallbackPath,
|
||||
original: m,
|
||||
}
|
||||
}, nil
|
||||
}
|
||||
func (a *API) getRandomTrack() *QueueTrackEntry {
|
||||
func (a *API) getRandomTrack() (*QueueTrackEntry, error) {
|
||||
response, err := http.DefaultClient.Get(a.config.Queue.RandomSongApi)
|
||||
if err != nil {
|
||||
return nil
|
||||
return nil, err
|
||||
}
|
||||
|
||||
defer response.Body.Close()
|
||||
body, err := io.ReadAll(response.Body)
|
||||
if err != nil {
|
||||
return nil
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return a.getQueueEntryFromBody(body)
|
||||
|
@ -111,20 +112,28 @@ func (a *API) handleQueue() {
|
|||
defer close(a.queue.QueueEmpty)
|
||||
//TODO: close properly
|
||||
for {
|
||||
if e := a.getRandomTrack(); e != nil {
|
||||
if e, err := a.getRandomTrack(); e != nil {
|
||||
//preload
|
||||
if err := e.Load(); err != nil {
|
||||
if err = e.Load(); err != nil {
|
||||
log.Printf("random track loading error for %s: \"%s\"", e.Path, err)
|
||||
continue
|
||||
}
|
||||
a.setNowRandom(e)
|
||||
a.queue.QueueEmpty <- e
|
||||
} else if e = a.getFallbackTrack(); e != nil {
|
||||
//preload
|
||||
if err := e.Load(); err != nil {
|
||||
continue
|
||||
} else {
|
||||
log.Printf("random track error: \"%s\"", err)
|
||||
|
||||
if e, err = a.getFallbackTrack(); e != nil {
|
||||
//preload
|
||||
if err = e.Load(); err != nil {
|
||||
log.Printf("fallback track loading error for %s: \"%s\"", e.Path, err)
|
||||
continue
|
||||
}
|
||||
a.setNowRandom(e)
|
||||
a.queue.QueueEmpty <- e
|
||||
} else {
|
||||
log.Printf("fallback track error: \"%s\"", err)
|
||||
}
|
||||
a.setNowRandom(e)
|
||||
a.queue.QueueEmpty <- e
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
@ -233,15 +242,19 @@ func (a *API) listen() {
|
|||
if request.Method == "POST" {
|
||||
result := queueResultResponse{}
|
||||
if body, err := io.ReadAll(request.Body); err == nil {
|
||||
if e := a.getQueueEntryFromBody(body); e != nil {
|
||||
if e, err := a.getQueueEntryFromBody(body); e != nil {
|
||||
if err = a.queue.AddTrack(e, false); err == nil {
|
||||
result.Success = true
|
||||
result.QueueId = e.QueueIdentifier
|
||||
} else {
|
||||
resultErr := err.Error()
|
||||
result.Reason = &resultErr
|
||||
log.Printf("track addition error: \"%s\"", err)
|
||||
log.Printf("track addition error for %s: \"%s\"", e.Path, err)
|
||||
}
|
||||
} else {
|
||||
resultErr := err.Error()
|
||||
result.Reason = &resultErr
|
||||
log.Printf("track addition error: \"%s\"", err)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -265,15 +278,19 @@ func (a *API) listen() {
|
|||
if request.Method == "POST" {
|
||||
result := queueResultResponse{}
|
||||
if body, err := io.ReadAll(request.Body); err == nil {
|
||||
if e := a.getQueueEntryFromBody(body); e != nil {
|
||||
if e, err := a.getQueueEntryFromBody(body); e != nil {
|
||||
if err = a.queue.AddTrack(e, true); err == nil {
|
||||
result.Success = true
|
||||
result.QueueId = e.QueueIdentifier
|
||||
} else {
|
||||
resultErr := err.Error()
|
||||
result.Reason = &resultErr
|
||||
log.Printf("track addition error: \"%s\"", err)
|
||||
log.Printf("track addition error for %s: \"%s\"", e.Path, err)
|
||||
}
|
||||
} else {
|
||||
resultErr := err.Error()
|
||||
result.Reason = &resultErr
|
||||
log.Printf("track addition error: \"%s\"", err)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue