Improve error handling for the API
This commit is contained in:
@@ -34,6 +34,9 @@ var (
|
|||||||
transcodePtr = flag.Bool("tc", false, "Use ffmpeg to transcode input video file.")
|
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.")
|
listPtr = flag.Bool("l", false, "List all available UPnP/DLNA Media Renderer models and URLs.")
|
||||||
versionPtr = flag.Bool("version", false, "Print version.")
|
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 {
|
type dummyScreen struct {
|
||||||
@@ -194,12 +197,12 @@ func listFlagFunction() error {
|
|||||||
})
|
})
|
||||||
|
|
||||||
if flagsEnabled > 1 {
|
if flagsEnabled > 1 {
|
||||||
return errors.New("can't combine -l with other flags")
|
return ErrNoCombi
|
||||||
}
|
}
|
||||||
|
|
||||||
deviceList, err := devices.LoadSSDPservices(1)
|
deviceList, err := devices.LoadSSDPservices(1)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.New("failed to list devices")
|
return ErrFailtoList
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println()
|
fmt.Println()
|
||||||
|
@@ -36,6 +36,9 @@ var (
|
|||||||
transcodePtr = flag.Bool("tc", false, "Use ffmpeg to transcode input video file.")
|
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.")
|
listPtr = flag.Bool("l", false, "List all available UPnP/DLNA Media Renderer models and URLs.")
|
||||||
versionPtr = flag.Bool("version", false, "Print version.")
|
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 {
|
type flagResults struct {
|
||||||
@@ -188,12 +191,12 @@ func listFlagFunction() error {
|
|||||||
})
|
})
|
||||||
|
|
||||||
if flagsEnabled > 1 {
|
if flagsEnabled > 1 {
|
||||||
return errors.New("can't combine -l with other flags")
|
return ErrNoCombi
|
||||||
}
|
}
|
||||||
|
|
||||||
deviceList, err := devices.LoadSSDPservices(1)
|
deviceList, err := devices.LoadSSDPservices(1)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.New("failed to list devices")
|
return ErrFailtoList
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println()
|
fmt.Println()
|
||||||
|
@@ -9,7 +9,11 @@ import (
|
|||||||
"github.com/pkg/errors"
|
"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
|
// LoadSSDPservices returns a map with all the devices that support the
|
||||||
// AVTransport service.
|
// 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.
|
// DevicePicker will pick the nth device from the devices input map.
|
||||||
func DevicePicker(devices map[string]string, n int) (string, error) {
|
func DevicePicker(devices map[string]string, n int) (string, error) {
|
||||||
if n > len(devices) || len(devices) == 0 || n <= 0 {
|
if n > len(devices) || len(devices) == 0 || n <= 0 {
|
||||||
return "", errors.New("devicePicker: Requested device not available")
|
return "", ErrDeviceNotAvailable
|
||||||
}
|
}
|
||||||
|
|
||||||
keys := make([]string, 0)
|
keys := make([]string, 0)
|
||||||
@@ -60,5 +64,6 @@ func DevicePicker(devices map[string]string, n int) (string, error) {
|
|||||||
return devices[k], nil
|
return devices[k], nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return "", errors.New("devicePicker: Something went terribly wrong")
|
|
||||||
|
return "", ErrSomethingWentWrong
|
||||||
}
|
}
|
||||||
|
@@ -12,6 +12,10 @@ import (
|
|||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
ErrSetMuteInput = errors.New("setMuteSoapBuild input error. Was expecting 0 or 1.")
|
||||||
|
)
|
||||||
|
|
||||||
type playEnvelope struct {
|
type playEnvelope struct {
|
||||||
XMLName xml.Name `xml:"s:Envelope"`
|
XMLName xml.Name `xml:"s:Envelope"`
|
||||||
Schema string `xml:"xmlns:s,attr"`
|
Schema string `xml:"xmlns:s,attr"`
|
||||||
@@ -432,7 +436,7 @@ func pauseSoapBuild() ([]byte, error) {
|
|||||||
|
|
||||||
func setMuteSoapBuild(m string) ([]byte, error) {
|
func setMuteSoapBuild(m string) ([]byte, error) {
|
||||||
if m != "0" && m != "1" {
|
if m != "0" && m != "1" {
|
||||||
return nil, errors.New("setMuteSoapBuild input error. Was expecting 0 or 1.")
|
return nil, ErrSetMuteInput
|
||||||
}
|
}
|
||||||
|
|
||||||
d := setMuteEnvelope{
|
d := setMuteEnvelope{
|
||||||
|
@@ -25,7 +25,11 @@ type States struct {
|
|||||||
ProcessStop bool
|
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
|
// TVPayload this is the heart of Go2TV. We pass that type to the
|
||||||
// webserver. We need to explicitly initialize it.
|
// 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 p.MediaRenderersStates[uuid].ProcessStop, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return true, errors.New("zombie callbacks, we should ignore those")
|
return true, ErrZombieCallbacks
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *TVPayload) Log() *zerolog.Logger {
|
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
|
||||||
}
|
}
|
||||||
|
@@ -11,6 +11,10 @@ import (
|
|||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
ErrWrongDMR = errors.New("something broke somewhere - wrong DMR URL?")
|
||||||
|
)
|
||||||
|
|
||||||
type rootNode struct {
|
type rootNode struct {
|
||||||
XMLName xml.Name `xml:"root"`
|
XMLName xml.Name `xml:"root"`
|
||||||
Device struct {
|
Device struct {
|
||||||
@@ -129,7 +133,7 @@ func DMRextractor(dmrurl string) (*DMRextracted, error) {
|
|||||||
return ex, nil
|
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.
|
// EventNotifyParser parses the Notify messages from the DMR device.
|
||||||
|
@@ -41,6 +41,8 @@ var (
|
|||||||
"image/jpeg": "JPEG_LRG",
|
"image/jpeg": "JPEG_LRG",
|
||||||
"image/png": "PNG_LRG",
|
"image/png": "PNG_LRG",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ErrInvalidSeekFlag = errors.New("invalid seek flag")
|
||||||
)
|
)
|
||||||
|
|
||||||
func defaultStreamingFlags() string {
|
func defaultStreamingFlags() string {
|
||||||
@@ -76,7 +78,7 @@ func BuildContentFeatures(mediaType string, seek string, transcode bool) (string
|
|||||||
case "11":
|
case "11":
|
||||||
cf.WriteString("DLNA.ORG_OP=11;")
|
cf.WriteString("DLNA.ORG_OP=11;")
|
||||||
default:
|
default:
|
||||||
return "", errors.New("invalid seek flag")
|
return "", ErrInvalidSeekFlag
|
||||||
}
|
}
|
||||||
|
|
||||||
switch transcode {
|
switch transcode {
|
||||||
|
@@ -9,6 +9,10 @@ import (
|
|||||||
"os/exec"
|
"os/exec"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
ErrInvalidInput = errors.New("invalid ffmpeg input")
|
||||||
|
)
|
||||||
|
|
||||||
// ServeTranscodedStream passes an input file or io.Reader to ffmpeg and writes the output directly
|
// ServeTranscodedStream passes an input file or io.Reader to ffmpeg and writes the output directly
|
||||||
// to our io.Writer.
|
// to our io.Writer.
|
||||||
func ServeTranscodedStream(w io.Writer, input interface{}, ff *exec.Cmd) error {
|
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:
|
case io.Reader:
|
||||||
in = "pipe:0"
|
in = "pipe:0"
|
||||||
default:
|
default:
|
||||||
return errors.New("invalid ffmpeg input")
|
return ErrInvalidInput
|
||||||
}
|
}
|
||||||
|
|
||||||
if ff != nil && ff.Process != nil {
|
if ff != nil && ff.Process != nil {
|
||||||
|
@@ -7,6 +7,10 @@ import (
|
|||||||
"syscall"
|
"syscall"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
ErrInvalidInput = errors.New("invalid ffmpeg input")
|
||||||
|
)
|
||||||
|
|
||||||
// ServeTranscodedStream passes an input file or io.Reader to ffmpeg and writes the output directly
|
// ServeTranscodedStream passes an input file or io.Reader to ffmpeg and writes the output directly
|
||||||
// to our io.Writer.
|
// to our io.Writer.
|
||||||
func ServeTranscodedStream(w io.Writer, input interface{}, ff *exec.Cmd) error {
|
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:
|
case io.Reader:
|
||||||
in = "pipe:0"
|
in = "pipe:0"
|
||||||
default:
|
default:
|
||||||
return errors.New("invalid ffmpeg input")
|
return ErrInvalidInput
|
||||||
}
|
}
|
||||||
|
|
||||||
if ff != nil && ff.Process != nil {
|
if ff != nil && ff.Process != nil {
|
||||||
|
@@ -9,6 +9,10 @@ import (
|
|||||||
"net/url"
|
"net/url"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
ErrBadStatus = errors.New("streamURL bad status code")
|
||||||
|
)
|
||||||
|
|
||||||
// StreamURL returns the response body for the input media URL.
|
// StreamURL returns the response body for the input media URL.
|
||||||
func StreamURL(ctx context.Context, s string) (io.ReadCloser, error) {
|
func StreamURL(ctx context.Context, s string) (io.ReadCloser, error) {
|
||||||
_, err := url.ParseRequestURI(s)
|
_, err := url.ParseRequestURI(s)
|
||||||
@@ -28,7 +32,7 @@ func StreamURL(ctx context.Context, s string) (io.ReadCloser, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if resp.StatusCode >= 400 {
|
if resp.StatusCode >= 400 {
|
||||||
return nil, errors.New("streamURL bad status code: " + resp.Status)
|
return nil, ErrBadStatus
|
||||||
}
|
}
|
||||||
|
|
||||||
body := resp.Body
|
body := resp.Body
|
||||||
|
Reference in New Issue
Block a user