1
0
Fork 0
mirror of https://github.com/diamondburned/arikawa.git synced 2025-03-22 18:09:21 +00:00

utils/httputil: HTTPError print JSON error if possible

This commit is contained in:
diamondburned 2021-11-29 12:28:51 -08:00
parent 8d78221de0
commit 1e14369fc0
No known key found for this signature in database
GPG key ID: D78C4471CE776659

View file

@ -3,8 +3,11 @@ package httputil
import (
"fmt"
"strconv"
"github.com/diamondburned/arikawa/v3/utils/json"
)
// JSONError is returned if the request responds with an invalid JSON.
type JSONError struct {
err error
}
@ -17,6 +20,8 @@ func (j JSONError) Unwrap() error {
return j.err
}
// RequestError is returned if the request fails to be done, i.e. the server is
// never reached.
type RequestError struct {
err error
}
@ -29,16 +34,22 @@ func (r RequestError) Unwrap() error {
return r.err
}
// HTTPError is returned if the server responds successfully with an error of
// any kind.
type HTTPError struct {
Status int `json:"-"`
Body []byte `json:"-"`
Code ErrorCode `json:"code"`
Errors json.Raw `json:"errors,omitempty"`
Message string `json:"message,omitempty"`
}
func (err HTTPError) Error() string {
switch {
case err.Errors != "":
return fmt.Sprintf("Discord %d error: %s: %s", err.Status, err.Message, err.Errors)
case err.Message != "":
return fmt.Sprintf("Discord %d error: %s", err.Status, err.Message)