Compare commits

...

4 Commits
v1.5.0 ... main

Author SHA1 Message Date
3b208aeeaf Update go.mod 2024-12-16 19:49:05 +01:00
milarin
bfc0e5c417 removed lint warning 2023-10-09 13:49:37 +02:00
milarin
e6183ddfa3 handle error messages 2023-10-09 13:48:09 +02:00
Timon Ringwald
1591816684 fixed nil pointer when no API entry found 2022-11-14 18:07:54 +01:00
5 changed files with 43 additions and 2 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
api_test.go

8
api.go
View File

@ -49,6 +49,10 @@ func request[T any](api *Api, query string, vars map[string]interface{}, respObj
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return handleError(resp)
}
//data, _ := ioutil.ReadAll(resp.Body)
//fmt.Println(string(data))
@ -90,6 +94,10 @@ func requestPaged[R any](api *Api, ctx context.Context, query string, vars map[s
return
}
if resp.Data.Page == nil {
return
}
for _, value := range resp.Data.Page.Data() {
value := value
select {

32
api_error.go Normal file
View File

@ -0,0 +1,32 @@
package anilist
import (
"encoding/json"
"errors"
"net/http"
)
type apiError struct {
Message string `json:"message"`
Status int `json:"status"`
}
type apiErrResp struct {
Errors []apiError `json:"errors"`
}
func handleError(resp *http.Response) error {
respObj := &apiErrResp{}
dec := json.NewDecoder(resp.Body)
err := dec.Decode(respObj)
if err != nil {
return err
}
if len(respObj.Errors) == 0 {
return errors.New("unknown API error")
}
return errors.New(respObj.Errors[0].Message)
}

2
go.mod
View File

@ -1,3 +1,3 @@
module git.milar.in/milarin/anilist
module git.tordarus.net/tordarus/anilist
go 1.18

View File

@ -18,7 +18,7 @@ func addValue2InterfaceMap[K, T comparable](m map[K]interface{}, key K, value T)
}
func addSlice2InterfaceMap[K, T comparable](m map[K]interface{}, key K, value []T) {
if value != nil && len(value) > 0 {
if len(value) > 0 {
m[key] = value
}
}