Added custom error type

This commit is contained in:
bytedream 2021-09-13 09:35:07 +02:00
parent 2f5d3ea07b
commit a28002445a

View file

@ -7,6 +7,23 @@ import (
"sync" "sync"
) )
// StructureError is the error type which is thrown whenever a structure fails
// to receive information (formats, episodes, ...) from the api endpoint
type StructureError struct {
error
}
func (se *StructureError) Error() string {
return se.error.Error()
}
func IsStructureError(err error) (ok bool) {
if err != nil {
_, ok = err.(*StructureError)
}
return
}
// FormatStructure is the basic structure which every other structure implements. // FormatStructure is the basic structure which every other structure implements.
// With it, and all other structures the api usage can be simplified magnificent // With it, and all other structures the api usage can be simplified magnificent
type FormatStructure struct { type FormatStructure struct {
@ -40,6 +57,7 @@ func newFormatStructure(parentStructure *StreamStructure) *FormatStructure {
defer wg.Done() defer wg.Done()
f, err := stream.Formats() f, err := stream.Formats()
if err != nil { if err != nil {
errors.As(err, &StructureError{})
return return
} }
lock.Lock() lock.Lock()
@ -271,6 +289,7 @@ func newStreamStructure(structure VideoStructure) *StreamStructure {
defer wg.Done() defer wg.Done()
s, err := episode.Streams() s, err := episode.Streams()
if err != nil { if err != nil {
errors.As(err, &StructureError{})
return return
} }
lock.Lock() lock.Lock()
@ -302,6 +321,7 @@ func newStreamStructure(structure VideoStructure) *StreamStructure {
defer wg.Done() defer wg.Done()
s, err := movieListing.Streams() s, err := movieListing.Streams()
if err != nil { if err != nil {
errors.As(err, &StructureError{})
return return
} }
lock.Lock() lock.Lock()
@ -397,6 +417,7 @@ func newEpisodeStructure(structure *SeasonStructure) *EpisodeStructure {
defer wg.Done() defer wg.Done()
e, err := season.Episodes() e, err := season.Episodes()
if err != nil { if err != nil {
errors.As(err, &StructureError{})
return return
} }
lock.Lock() lock.Lock()
@ -481,6 +502,26 @@ func (es *EpisodeStructure) GetEpisodeByFormat(format *crunchyroll.Format) (*cru
return episode, nil return episode, nil
} }
// GetEpisodeByURL returns an episode by its url
func (es *EpisodeStructure) GetEpisodeByURL(url string) (*crunchyroll.Episode, error) {
_, title, ok := crunchyroll.MatchEpisode(url)
if !ok {
return nil, errors.New("invalid url")
}
episodes, err := es.Episodes()
if err != nil {
return nil, err
}
for _, episode := range episodes {
if episode.SlugTitle == title {
return episode, nil
}
}
return nil, errors.New("no episode could be found")
}
func (es *EpisodeStructure) OrderEpisodeByID() ([][]*crunchyroll.Episode, error) { func (es *EpisodeStructure) OrderEpisodeByID() ([][]*crunchyroll.Episode, error) {
episodes, err := es.Episodes() episodes, err := es.Episodes()
if err != nil { if err != nil {