anilist/api_error.go
2023-10-09 13:48:09 +02:00

33 lines
516 B
Go

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