proper error handling

This commit is contained in:
milarin 2023-03-06 11:21:19 +01:00
parent 14c6c79839
commit ad5b38c838
5 changed files with 27 additions and 11 deletions

13
api.go
View File

@ -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), "<br/>")
@ -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
}

11
errors.go Normal file
View File

@ -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'")
)

1
go.mod
View File

@ -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
)

2
go.sum
View File

@ -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=

View File

@ -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)