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