From f01ac7b716b34f8023a9d5dd58325fe48af2bf7c Mon Sep 17 00:00:00 2001 From: Elara Musayelyan Date: Wed, 4 Oct 2023 21:08:55 -0700 Subject: [PATCH] Merge HTTPError into Error --- errors.go | 25 ------------------------- lemmy.go | 17 ++++++++++++++++- 2 files changed, 16 insertions(+), 26 deletions(-) delete mode 100644 errors.go diff --git a/errors.go b/errors.go deleted file mode 100644 index 8331082..0000000 --- a/errors.go +++ /dev/null @@ -1,25 +0,0 @@ -package lemmy - -import ( - "fmt" - "net/http" -) - -// HTTPError represents an error caused by a non-200 HTTP status code -type HTTPError struct { - Code int -} - -func (he HTTPError) Error() string { - return fmt.Sprintf("%d %s", he.Code, http.StatusText(he.Code)) -} - -// Error represents an error returned by the Lemmy API -type Error struct { - ErrStr string - Code int -} - -func (le Error) Error() string { - return fmt.Sprintf("%d %s: %s", le.Code, http.StatusText(le.Code), le.ErrStr) -} diff --git a/lemmy.go b/lemmy.go index 538d91c..ce20e28 100644 --- a/lemmy.go +++ b/lemmy.go @@ -5,6 +5,7 @@ import ( "context" "encoding/json" "errors" + "fmt" "io" "net/http" "net/url" @@ -139,6 +140,20 @@ func (c *Client) getReq(ctx context.Context, method string, path string, data, r return res, nil } +// Error represents an error returned by the Lemmy API +type Error struct { + ErrStr string + Code int +} + +func (le Error) Error() string { + if le.ErrStr != "" { + return fmt.Sprintf("%d %s: %s", le.Code, http.StatusText(le.Code), le.ErrStr) + } else { + return fmt.Sprintf("%d %s", le.Code, http.StatusText(le.Code)) + } +} + // emptyResponse is a response without any fields. // It has an Error field to capture any errors. type emptyResponse struct { @@ -153,7 +168,7 @@ func resError(res *http.Response, err Optional[string]) error { ErrStr: errstr, } } else if res.StatusCode != http.StatusOK { - return HTTPError{ + return Error{ Code: res.StatusCode, } } else {