mirror of
https://github.com/crunchy-labs/crunchy-cli.git
synced 2026-01-21 04:02:00 -06:00
Added custom error type
This commit is contained in:
parent
2f5d3ea07b
commit
a28002445a
1 changed files with 41 additions and 0 deletions
|
|
@ -7,6 +7,23 @@ import (
|
|||
"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.
|
||||
// With it, and all other structures the api usage can be simplified magnificent
|
||||
type FormatStructure struct {
|
||||
|
|
@ -40,6 +57,7 @@ func newFormatStructure(parentStructure *StreamStructure) *FormatStructure {
|
|||
defer wg.Done()
|
||||
f, err := stream.Formats()
|
||||
if err != nil {
|
||||
errors.As(err, &StructureError{})
|
||||
return
|
||||
}
|
||||
lock.Lock()
|
||||
|
|
@ -271,6 +289,7 @@ func newStreamStructure(structure VideoStructure) *StreamStructure {
|
|||
defer wg.Done()
|
||||
s, err := episode.Streams()
|
||||
if err != nil {
|
||||
errors.As(err, &StructureError{})
|
||||
return
|
||||
}
|
||||
lock.Lock()
|
||||
|
|
@ -302,6 +321,7 @@ func newStreamStructure(structure VideoStructure) *StreamStructure {
|
|||
defer wg.Done()
|
||||
s, err := movieListing.Streams()
|
||||
if err != nil {
|
||||
errors.As(err, &StructureError{})
|
||||
return
|
||||
}
|
||||
lock.Lock()
|
||||
|
|
@ -397,6 +417,7 @@ func newEpisodeStructure(structure *SeasonStructure) *EpisodeStructure {
|
|||
defer wg.Done()
|
||||
e, err := season.Episodes()
|
||||
if err != nil {
|
||||
errors.As(err, &StructureError{})
|
||||
return
|
||||
}
|
||||
lock.Lock()
|
||||
|
|
@ -481,6 +502,26 @@ func (es *EpisodeStructure) GetEpisodeByFormat(format *crunchyroll.Format) (*cru
|
|||
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) {
|
||||
episodes, err := es.Episodes()
|
||||
if err != nil {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue