From 1e14369fc08bd23f3586f89471d34b3be5e7391b Mon Sep 17 00:00:00 2001 From: diamondburned Date: Mon, 29 Nov 2021 12:28:51 -0800 Subject: [PATCH] utils/httputil: HTTPError print JSON error if possible --- utils/httputil/errors.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/utils/httputil/errors.go b/utils/httputil/errors.go index 19fe015..a9cfb8c 100644 --- a/utils/httputil/errors.go +++ b/utils/httputil/errors.go @@ -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)