Improve error handling for the API

This commit is contained in:
Alex Ballas
2022-09-27 20:57:14 +03:00
parent 86c441abe2
commit 7b4571fc8f
10 changed files with 53 additions and 16 deletions

View File

@@ -34,6 +34,9 @@ var (
transcodePtr = flag.Bool("tc", false, "Use ffmpeg to transcode input video file.")
listPtr = flag.Bool("l", false, "List all available UPnP/DLNA Media Renderer models and URLs.")
versionPtr = flag.Bool("version", false, "Print version.")
ErrNoCombi = errors.New("can't combine -l with other flags")
ErrFailtoList = errors.New("failed to list devices")
)
type dummyScreen struct {
@@ -194,12 +197,12 @@ func listFlagFunction() error {
})
if flagsEnabled > 1 {
return errors.New("can't combine -l with other flags")
return ErrNoCombi
}
deviceList, err := devices.LoadSSDPservices(1)
if err != nil {
return errors.New("failed to list devices")
return ErrFailtoList
}
fmt.Println()

View File

@@ -36,6 +36,9 @@ var (
transcodePtr = flag.Bool("tc", false, "Use ffmpeg to transcode input video file.")
listPtr = flag.Bool("l", false, "List all available UPnP/DLNA Media Renderer models and URLs.")
versionPtr = flag.Bool("version", false, "Print version.")
ErrNoCombi = errors.New("can't combine -l with other flags")
ErrFailtoList = errors.New("failed to list devices")
)
type flagResults struct {
@@ -188,12 +191,12 @@ func listFlagFunction() error {
})
if flagsEnabled > 1 {
return errors.New("can't combine -l with other flags")
return ErrNoCombi
}
deviceList, err := devices.LoadSSDPservices(1)
if err != nil {
return errors.New("failed to list devices")
return ErrFailtoList
}
fmt.Println()

View File

@@ -9,7 +9,11 @@ import (
"github.com/pkg/errors"
)
var ErrNoDeviceAvailable = errors.New("loadSSDPservices: No available Media Renderers")
var (
ErrNoDeviceAvailable = errors.New("loadSSDPservices: No available Media Renderers")
ErrDeviceNotAvailable = errors.New("devicePicker: Requested device not available")
ErrSomethingWentWrong = errors.New("devicePicker: Something went terribly wrong")
)
// LoadSSDPservices returns a map with all the devices that support the
// AVTransport service.
@@ -45,7 +49,7 @@ func LoadSSDPservices(delay int) (map[string]string, error) {
// DevicePicker will pick the nth device from the devices input map.
func DevicePicker(devices map[string]string, n int) (string, error) {
if n > len(devices) || len(devices) == 0 || n <= 0 {
return "", errors.New("devicePicker: Requested device not available")
return "", ErrDeviceNotAvailable
}
keys := make([]string, 0)
@@ -60,5 +64,6 @@ func DevicePicker(devices map[string]string, n int) (string, error) {
return devices[k], nil
}
}
return "", errors.New("devicePicker: Something went terribly wrong")
return "", ErrSomethingWentWrong
}

View File

@@ -12,6 +12,10 @@ import (
"github.com/pkg/errors"
)
var (
ErrSetMuteInput = errors.New("setMuteSoapBuild input error. Was expecting 0 or 1.")
)
type playEnvelope struct {
XMLName xml.Name `xml:"s:Envelope"`
Schema string `xml:"xmlns:s,attr"`
@@ -432,7 +436,7 @@ func pauseSoapBuild() ([]byte, error) {
func setMuteSoapBuild(m string) ([]byte, error) {
if m != "0" && m != "1" {
return nil, errors.New("setMuteSoapBuild input error. Was expecting 0 or 1.")
return nil, ErrSetMuteInput
}
d := setMuteEnvelope{

View File

@@ -25,7 +25,11 @@ type States struct {
ProcessStop bool
}
var log zerolog.Logger
var (
log zerolog.Logger
ErrNoMatchingFileType = errors.New("no matching file type")
ErrZombieCallbacks = errors.New("zombie callbacks, we should ignore those")
)
// TVPayload this is the heart of Go2TV. We pass that type to the
// webserver. We need to explicitly initialize it.
@@ -899,7 +903,7 @@ func (p *TVPayload) GetProcessStop(uuid string) (bool, error) {
return p.MediaRenderersStates[uuid].ProcessStop, nil
}
return true, errors.New("zombie callbacks, we should ignore those")
return true, ErrZombieCallbacks
}
func (p *TVPayload) Log() *zerolog.Logger {
@@ -945,5 +949,5 @@ func parseProtocolInfo(b []byte, mt string) error {
}
}
return errors.New("no matching file type")
return ErrNoMatchingFileType
}

View File

@@ -11,6 +11,10 @@ import (
"github.com/pkg/errors"
)
var (
ErrWrongDMR = errors.New("something broke somewhere - wrong DMR URL?")
)
type rootNode struct {
XMLName xml.Name `xml:"root"`
Device struct {
@@ -129,7 +133,7 @@ func DMRextractor(dmrurl string) (*DMRextracted, error) {
return ex, nil
}
return nil, errors.New("something broke somewhere - wrong DMR URL?")
return nil, ErrWrongDMR
}
// EventNotifyParser parses the Notify messages from the DMR device.

View File

@@ -41,6 +41,8 @@ var (
"image/jpeg": "JPEG_LRG",
"image/png": "PNG_LRG",
}
ErrInvalidSeekFlag = errors.New("invalid seek flag")
)
func defaultStreamingFlags() string {
@@ -76,7 +78,7 @@ func BuildContentFeatures(mediaType string, seek string, transcode bool) (string
case "11":
cf.WriteString("DLNA.ORG_OP=11;")
default:
return "", errors.New("invalid seek flag")
return "", ErrInvalidSeekFlag
}
switch transcode {

View File

@@ -9,6 +9,10 @@ import (
"os/exec"
)
var (
ErrInvalidInput = errors.New("invalid ffmpeg input")
)
// ServeTranscodedStream passes an input file or io.Reader to ffmpeg and writes the output directly
// to our io.Writer.
func ServeTranscodedStream(w io.Writer, input interface{}, ff *exec.Cmd) error {
@@ -22,7 +26,7 @@ func ServeTranscodedStream(w io.Writer, input interface{}, ff *exec.Cmd) error {
case io.Reader:
in = "pipe:0"
default:
return errors.New("invalid ffmpeg input")
return ErrInvalidInput
}
if ff != nil && ff.Process != nil {

View File

@@ -7,6 +7,10 @@ import (
"syscall"
)
var (
ErrInvalidInput = errors.New("invalid ffmpeg input")
)
// ServeTranscodedStream passes an input file or io.Reader to ffmpeg and writes the output directly
// to our io.Writer.
func ServeTranscodedStream(w io.Writer, input interface{}, ff *exec.Cmd) error {
@@ -20,7 +24,7 @@ func ServeTranscodedStream(w io.Writer, input interface{}, ff *exec.Cmd) error {
case io.Reader:
in = "pipe:0"
default:
return errors.New("invalid ffmpeg input")
return ErrInvalidInput
}
if ff != nil && ff.Process != nil {

View File

@@ -9,6 +9,10 @@ import (
"net/url"
)
var (
ErrBadStatus = errors.New("streamURL bad status code")
)
// StreamURL returns the response body for the input media URL.
func StreamURL(ctx context.Context, s string) (io.ReadCloser, error) {
_, err := url.ParseRequestURI(s)
@@ -28,7 +32,7 @@ func StreamURL(ctx context.Context, s string) (io.ReadCloser, error) {
}
if resp.StatusCode >= 400 {
return nil, errors.New("streamURL bad status code: " + resp.Status)
return nil, ErrBadStatus
}
body := resp.Body