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 {