anilist/api_error.go

33 lines
516 B
Go
Raw Permalink Normal View History

2023-10-09 13:48:09 +02:00
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)
}