Skip to content
Snippets Groups Projects
Commit 46b373a9 authored by Christian Pointner's avatar Christian Pointner
Browse files

store: refactor playlist BeforeSave Error handling

parent 52f14a64
No related branches found
No related tags found
No related merge requests found
......@@ -48,6 +48,8 @@ func statusCodeFromError(err error) (code int, response ErrorResponse) {
response.Detail = err
case store.ErrInvalidMetadataField:
code = http.StatusBadRequest
case store.ErrInvalidPlaylistEntry:
code = http.StatusBadRequest
case *importer.JobSourceResult:
response.Detail = err.(*importer.JobSourceResult).Log
default:
......
......@@ -33,29 +33,36 @@ import (
"github.com/jinzhu/gorm"
)
func generateFileURI(file *File) string {
uri := url.URL{Scheme: FileURIScheme, Host: file.ShowName, Path: strconv.FormatUint(file.ID, 10)}
return uri.String()
}
func (p *Playlist) BeforeSave() error {
for idx := range p.Entries {
p.Entries[idx].LineNum = uint(idx)
if p.Entries[idx].File != nil {
p.Entries[idx].URI = "" // this will be regenerated in AfterFind()
if p.Entries[idx].URI != "" && p.Entries[idx].URI != generateFileURI(p.Entries[idx].File) {
return ErrInvalidPlaylistEntry{idx, "File and Uri Parameter mismatch"}
}
} else if p.Entries[idx].URI != "" {
uri, err := url.Parse(p.Entries[idx].URI)
if err != nil {
return fmt.Errorf("playlist entry #%d is invalid: %v", idx, err)
return ErrInvalidPlaylistEntry{idx, err.Error()}
}
if uri.Scheme == FileURIScheme {
if uri.Host == "" || uri.Path == "" {
return fmt.Errorf("playlist entry #%d is invalid: uri must be in the format %s://<show>/<file-id>", idx, FileURIScheme)
return ErrInvalidPlaylistEntry{idx, "uri must be in the format " + FileURIScheme + "://<show>/<file-id>"}
}
fileID, err := strconv.ParseUint(strings.TrimPrefix(uri.Path, "/"), 10, 64)
if err != nil {
return fmt.Errorf("playlist entry #%d is invalid: %v", idx, err)
return ErrInvalidPlaylistEntry{idx, err.Error()}
}
p.Entries[idx].File = &File{ID: fileID, ShowName: uri.Host}
p.Entries[idx].URI = "" // this will be regenerated in AfterFind()
}
} else {
return fmt.Errorf("playlist entry #%d is invalid, entries must either contain a File or have a URI set", idx)
return ErrInvalidPlaylistEntry{idx, "entries must either contain a File or have a URI set"}
}
}
return nil
......@@ -68,10 +75,7 @@ func (p *Playlist) AfterSave(tx *gorm.DB) error {
func (p *Playlist) AfterFind() error {
for idx := range p.Entries {
if p.Entries[idx].File != nil {
urihost := p.Entries[idx].File.ShowName
uripath := strconv.FormatUint(p.Entries[idx].File.ID, 10)
uri := url.URL{Scheme: FileURIScheme, Host: urihost, Path: uripath}
p.Entries[idx].URI = uri.String()
p.Entries[idx].URI = generateFileURI(p.Entries[idx].File)
}
}
return nil
......
......@@ -64,6 +64,15 @@ func (e ErrInvalidMetadataField) Error() string {
return "invalid metadata field: " + string(e)
}
type ErrInvalidPlaylistEntry struct {
idx int
message string
}
func (e ErrInvalidPlaylistEntry) Error() string {
return fmt.Sprintf("playlist entry #%d is invalid: %s", e.idx, e.message)
}
//******* Logs
type LogLine struct {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment