From ad5b38c838338190f47ac656ce615842e0b83667 Mon Sep 17 00:00:00 2001 From: milarin Date: Mon, 6 Mar 2023 11:21:19 +0100 Subject: [PATCH] proper error handling --- api.go | 13 ++++++++----- errors.go | 11 +++++++++++ go.mod | 1 + go.sum | 2 ++ novelupdates.go | 11 +++++------ 5 files changed, 27 insertions(+), 11 deletions(-) create mode 100644 errors.go diff --git a/api.go b/api.go index e953d57..25f9615 100644 --- a/api.go +++ b/api.go @@ -9,6 +9,7 @@ import ( "strings" "time" + "git.milar.in/milarin/adverr" "git.milar.in/milarin/slices" "github.com/PuerkitoBio/goquery" ) @@ -28,7 +29,7 @@ func NewApi(cookie string) *Api { func (api *Api) GetReadingList(ctx context.Context, listIndex int) (*ReadingList, error) { doc, err := api.GetWithCookie(ctx, fmt.Sprintf("https://www.novelupdates.com/reading-list/?list=%d", listIndex)) if err != nil { - return nil, err + return nil, ErrApiRequestFailed.Wrap(err) } listID := ReadingListID(doc.Find("#cssmenu ul li.active a").Text()) @@ -78,7 +79,7 @@ func (api *Api) GetReadingList(ctx context.Context, listIndex int) (*ReadingList func (api *Api) GetNovelByID(novelID NovelID) (*Novel, error) { doc, err := api.Get(context.Background(), fmt.Sprintf("https://www.novelupdates.com/series/%s/", novelID)) if err != nil { - return nil, err + return nil, ErrApiRequestFailed.Wrap(err) } title := doc.Find(".seriestitlenu").Text() @@ -87,7 +88,7 @@ func (api *Api) GetNovelByID(novelID NovelID) (*Novel, error) { associatedNamesHtml, err := doc.Find("#editassociated").Html() if err != nil { - return nil, err + return nil, ErrApiElementNotFound.Wrap(err, "#editassociated") } associatedNames := strings.Split(strings.TrimSpace(associatedNamesHtml), "
") @@ -142,7 +143,7 @@ func (api *Api) GetChapterEntriesByNovelID(novelID NovelID) *Cursor[NovelChapter pageCount, err := strconv.ParseInt(doc.Find(".digg_pagination a:nth-child(5)").Text(), 10, 64) if err != nil { - fmt.Fprintln(os.Stderr, err) + adverr.Println(ErrApiElementNotFound.Wrap(err, ".digg_pagination a:nth-child(5)")) return } @@ -153,6 +154,7 @@ func (api *Api) GetChapterEntriesByNovelID(novelID NovelID) *Cursor[NovelChapter entries, err := api.getChapterEntriesByPageIndex(ctx, novelID, pageIndex) if err != nil { + fmt.Fprintln(os.Stderr, err) return } @@ -173,7 +175,7 @@ func (api *Api) GetChapterEntriesByNovelID(novelID NovelID) *Cursor[NovelChapter func (api *Api) getChapterEntriesByPageIndex(ctx context.Context, novelID NovelID, pageIndex int) ([]NovelChapterEntry, error) { doc, err := api.Get(ctx, fmt.Sprintf("https://www.novelupdates.com/series/%s/?pg=%d", novelID, pageIndex)) if err != nil { - return nil, err + return nil, ErrApiRequestFailed.Wrap(err) } entryElems := doc.Find("#myTable tbody tr") @@ -187,6 +189,7 @@ func (api *Api) getChapterEntriesByPageIndex(ctx context.Context, novelID NovelI date, err := time.Parse("01/02/06", strings.TrimSpace(s.Find("td:first-child").Text())) if err != nil { + adverr.Println(ErrApiElementNotFound.Wrap(err, "td:first-child")) return } diff --git a/errors.go b/errors.go new file mode 100644 index 0000000..2f1a7dd --- /dev/null +++ b/errors.go @@ -0,0 +1,11 @@ +package nuapi + +import "git.milar.in/milarin/adverr" + +var ( + ErrNoCookieSet = adverr.NewErrTmpl("ErrNoCookieSet", "no API cookie set") + ErrCurlRequestFailed = adverr.NewErrTmpl("ErrCurlRequestFailed", "curl request failed for url: '%s'") + ErrInvalidGzipData = adverr.NewErrTmpl("ErrInvalidGzipData", "gzip encoded data expected") + ErrApiRequestFailed = adverr.NewErrTmpl("ErrApiRequestFailed", "Data retrieval from NU failed") + ErrApiElementNotFound = adverr.NewErrTmpl("ErrApiElementNotFound", "element not found: '%ss'") +) diff --git a/go.mod b/go.mod index 26d5142..9090927 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module git.milar.in/milarin/nu-api go 1.20 require ( + git.milar.in/milarin/adverr v1.1.0 git.milar.in/milarin/slices v0.0.6 github.com/PuerkitoBio/goquery v1.8.1 ) diff --git a/go.sum b/go.sum index 6fe43c5..ea996f4 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,5 @@ +git.milar.in/milarin/adverr v1.1.0 h1:jD9WnOvs40lfMhvqQ7cllOaRJNBMWr1f07/s9jAadp0= +git.milar.in/milarin/adverr v1.1.0/go.mod h1:joU9sBb7ySyNv4SpTXB0Z4o1mjXsArBw4N27wjgzj9E= git.milar.in/milarin/slices v0.0.6 h1:AQoSarZ58WHYol9c6woWJSe8wFpPC2RC4cvIlZpfg9s= git.milar.in/milarin/slices v0.0.6/go.mod h1:NOr53AOeur/qscu/FBj3lsFR262PNYBccLYSTCAXRk4= github.com/PuerkitoBio/goquery v1.8.1 h1:uQxhNlArOIdbrH1tr0UXwdVFgDcZDrZVdcpygAcwmWM= diff --git a/novelupdates.go b/novelupdates.go index 9da214b..0546feb 100644 --- a/novelupdates.go +++ b/novelupdates.go @@ -4,7 +4,6 @@ import ( "bytes" "compress/gzip" "context" - "errors" "fmt" "os/exec" @@ -13,7 +12,7 @@ import ( func (api *Api) GetWithCookie(ctx context.Context, url string) (*goquery.Document, error) { if api.Cookie == "" { - return nil, errors.New("no API cookie set") + return nil, ErrNoCookieSet.New() } curl := exec.CommandContext(ctx, "curl", @@ -25,12 +24,12 @@ func (api *Api) GetWithCookie(ctx context.Context, url string) (*goquery.Documen data, err := curl.Output() if err != nil { - return nil, err + return nil, ErrCurlRequestFailed.New(err, url) } r, err := gzip.NewReader(bytes.NewReader(data)) if err != nil { - return nil, err + return nil, ErrInvalidGzipData.New(err) } return goquery.NewDocumentFromReader(r) @@ -45,12 +44,12 @@ func (api *Api) Get(ctx context.Context, url string) (*goquery.Document, error) data, err := curl.Output() if err != nil { - return nil, err + return nil, ErrCurlRequestFailed.New(err, url) } r, err := gzip.NewReader(bytes.NewReader(data)) if err != nil { - return nil, err + return nil, ErrInvalidGzipData.New(err) } return goquery.NewDocumentFromReader(r)